mysql8主从及自恢复脚本
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql8主从及自恢复脚本相关的知识,希望对你有一定的参考价值。
参考技术A 主从需要安装相同的mysql8版本。在主机上创建用户
修改配置
vim /etc/my.cnf
重启
systemctl restart mysqld
查看主机的节点信息
show master status\G
修改配置
vim /etc/my.cnf
重启
systemctl restart mysqld
根据主机的节点信息修改以下指令
change master to master_host='192.168.1.81',master_user='lucien',master_password='123456',master_port=3306,master_log_file='lucien_logbin.000001',master_log_pos=34484,master_connect_retry=30;
启动
start slave
定时脚本,每1小时执行一次
vim /etc/cron.hourly/lucien_hour.sh
chmod +x lucien_hour.sh
自恢复脚本
简单的mysql主从搭建及恢复
mysql主从搭建:
怎么安装mysql数据库,这里不讲,只说它的主从复制,步骤如下:
1、主从服务器分别作以下操作:
1.1、版本一致
1.2、初始化表,并在后台启动mysql
1.3、修改root的密码
2、修改主服务器master:
#vi /etc/my.cnf
[mysqld]
log-bin=mysql-bin //[必须]启用二进制日志
server-id=1 //[必须]服务器唯一ID
3、修改从服务器slave:
#vi /etc/my.cnf
[mysqld]
log-bin=mysql-bin //[必须]启用二进制日志
server-id=2 //[必须]服务器唯一ID
4、重启两台服务器的mysql
/etc/init.d/mysql restart
5、在主服务器上建立帐户并授权slave:
#/usr/local/mysql/bin/mysql -uroot -p
mysql>GRANT REPLICATION SLAVE ON *.* to ‘test‘@‘%‘ identified by ‘123456‘; //一般不用root帐号,“%”表示所有客户端都可能连,只要帐号,密码正确,此处可用具体客户端IP代替,如192.168.0.146,加强安全。
6、登录主服务器的mysql,查询master的状态
mysql>show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000004 | 308 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
注:执行完此步骤后不要再操作主服务器MYSQL,防止主服务器状态值变化
7、配置从服务器Slave:
mysql>change master to master_host=‘192.168.0.144‘,master_user=‘test‘,master_password=‘123456‘,master_log_file=‘mysql-bin.000004‘,master_log_pos=308; //注意不要断开,“308”无单引号。
Mysql>start slave; //启动从服务器复制功能
8、检查从服务器复制功能状态:
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.0.144 //主服务器地址
Master_User: test //授权帐户名,尽量避免使用root
Master_Port: 3306 //数据库端口,部分版本没有此行
Connect_Retry: 60
Master_Log_File: mysql-bin.000004
Read_Master_Log_Pos: 600 //#同步读取二进制日志的位置,大于等于>=Exec_Master_Log_Pos
Relay_Log_File: ddte-relay-bin.000003
Relay_Log_Pos: 251
Relay_Master_Log_File: mysql-bin.000004
Slave_IO_Running: Yes //此状态必须YES
Slave_SQL_Running: Yes //此状态必须YES
......
注:Slave_IO及Slave_SQL进程必须正常运行,即YES状态,否则都是错误的状态(如:其中一个NO均属错误)。
以上操作过程,主从服务器配置完成。
9、主从服务器测试:
主服务器Mysql,建立数据库,并在这个库中建表插入一条数据:
mysql> create database hi_db;
Query OK, 1 row affected (0.00 sec)
mysql> use hi_db;
Database changed
mysql> create table hi_tb(id int(3),name char(10));
Query OK, 0 rows affected (0.00 sec)
mysql> insert into hi_tb values(001,‘bobu‘);
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| hi_db |
| mysql |
| test |
+--------------------+
4 rows in set (0.00 sec)
从服务器Mysql查询:
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| hi_db | //I‘M here,大家看到了吧
| mysql |
| test |
+--------------------+
4 rows in set (0.00 sec)
mysql> use hi_db
Database changed
mysql> select * from hi_tb; //可以看到在主服务器上新增的具体数据
+------+------+
| id | name |
+------+------+
| 1 | bobu |
+------+------+
1 row in set (0.00 sec)
mysql主从故障切换:
首先保证主从数据库都开启二进制日志,方法是在my.cnf中的[mysqld]节中加入log-bin=log-bin-name
然后查看是否开启
MariaDB [(none)]> show variables like ‘log_bin‘;
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_bin | ON |
+---------------+-------+
1 row in set (0.00 sec)
为了在切换时不会漏掉数据库的更新,必须将主数据库停掉,设为只读:set global read_only=1;
MariaDB [(none)]> set global read_only=1;
Query OK, 0 rows affected (0.00 sec)
show variables; 确认 read_only ON
MariaDB [(none)]> show variables like ‘read_only‘;
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| read_only | ON |
+---------------+-------+
1 row in set (0.00 sec)
然后在主库
mysql> flush logs; 刷新一下log-bin,在从库中查看mysql> show slave status \G 是否完全更新
主库:
MariaDB [(none)]> flush logs;
Query OK, 0 rows affected (0.34 sec)
MariaDB [(none)]> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000003 | 365 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
从库:
MariaDB [(none)]> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.0.146
Master_User: test
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 365
Relay_Log_File: jinbi2-relay-bin.000004
Relay_Log_Pos: 652
Relay_Master_Log_File: mysql-bin.000002
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
确认主库和从库的file和pos是一致的。
查看主库的状态mysql> show master status \G 是否还有数据更新,如果没有就可以停掉从库
MariaDB [(none)]> stop slave;
更改从库mysql的身份为master
MariaDB [(none)]> reset master;
Query OK, 0 rows affected (0.16 sec)
MariaDB [(none)]> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 | 312 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
这时可以关掉新主库的只读属性
mysql> set global read_only=0;
创建一个新的库,一会从库连上后查看是否同步:
MariaDB [(none)]> create database t1;
Query OK, 1 row affected (0.00 sec)
更改主库mysql的身份为slave
MariaDB [(none)]> reset slave;
Query OK, 0 rows affected (0.16 sec)
新从库改变连接的主库信息:
MariaDB [(none)]> change master to master_host=‘192.168.0.144‘, master_user=‘test‘, master_password=‘123456‘, master_log_file=‘mysql-bin.000001‘, master_log_pos=312;
Query OK, 0 rows affected (0.16 sec)
查看新从库状态:
MariaDB [(none)]> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.0.144
Master_User: test
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 429
Relay_Log_File: jinbi4-relay-bin.000002
Relay_Log_Pos: 652
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
查看刚才新主库创建的数据库是否已经同步过来:
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| bai |
| information_schema |
| mysql |
| performance_schema |
| t1 |
| test |
+--------------------+
主从切换完成!
本文出自 “baiyubao的博客” 博客,请务必保留此出处http://baiyubao.blog.51cto.com/2845008/1755120
以上是关于mysql8主从及自恢复脚本的主要内容,如果未能解决你的问题,请参考以下文章