1.环境说明
centos7下最小安装环境,IP分配如下,默认yum安装mysql也会安装mariadb。
IP
mysql1
192.168.0.151
mysql2
192.168.0.152
2.yum直接安装mariadb
[root@localhost ~]# yum -y install mariadb mariadb-server #主从一样安装
3.配置mysql1
[root@localhost ~]# vim /etc/my.cnf
[mysql] 下添加如下
server-id=1 #任意自然数n,只要保证两台MySQL主机不重复就可以
log_bin=master-bin #开启二进制日志
auto_increment_increment=2 #步进值auto_imcrement。一般有n台主MySQL就填n
auto_increment_offset=1 #起始值。一般填第n台主MySQL。此时为第一台主MySQL
binlog-ignore=mysql #忽略mysql库
binlog-ignore=information_schema #忽略information_schema库
replicate-do-db=test #要同步的数据库,默认所有库
[root@localhost ~]# systemctl restart mariadb
[root@localhost ~]# systemctl enable mariadb
[root@localhost ~]# mysqladmin -u root password 123456 #默认没有密码,这里给root设置一个密码
[root@localhost ~]# mysql -u root -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 4
Server version: 5.5.60-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> GRANT REPLICATION SLAVE ON *.* to 'mysqlback'@'192.168.0.152' identified by '123456'; #给mysql2授权
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> show master status\G #查看mysql1的master状态,记录下 FILE 及 Position 的值,给MySQL2同步配置用
*************************** 1. row ***************************
File: master-bin.000003
Position: 617
Binlog_Do_DB:
Binlog_Ignore_DB:
1 row in set (0.00 sec)
MariaDB [(none)]>
4.配置mysql2
[root@localhost ~]# vim /etc/my.cnf
[mysqld]下添加如下
server-id=2 #任意自然数n,只要保证两台MySQL主机不重复就可以
log_bin=master-bin #开启二进制日志
auto_increment_increment=2 #步进值auto_imcrement。一般有n台主MySQL就填n
auto_increment_offset=2 #起始值。一般填第n台主MySQL。此时为第一台主MySQL
binlog-ignore=mysql #忽略mysql库
binlog-ignore=information_schema #忽略information_schema库
replicate-do-db=test #要同步的数据库,默认所有库
[root@localhost ~]# systemctl restart mariadb
[root@localhost ~]# systemctl enable mariadb
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.
[root@localhost ~]# mysqladmin -u root password 123456
[root@localhost ~]# mysql -u root -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 5.5.60-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> slave stop;
Query OK, 0 rows affected, 1 warning (0.00 sec)
MariaDB [(none)]> change master to master_host='192.168.0.151',master_user='mysqlback',master_password='123456',master_log_file='master-bin.000003',master_log_pos=617;
Query OK, 0 rows affected (0.01 sec)
MariaDB [(none)]> slave start;
Query OK, 0 rows affected (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.151
Master_User: mysqlback
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-bin.000003
Read_Master_Log_Pos: 617
Relay_Log_File: slave-relay-bin.000002
Relay_Log_Pos: 530
Relay_Master_Log_File: master-bin.000003
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 617
Relay_Log_Space: 824
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
1 row in set (0.00 sec)
-----
通常看slave状态下IO和SQL是否为yes来判定主从关系是否OK
SlaveIORunning: Yes
SlaveSQLRunning: Yes
-----
MariaDB [test]> GRANT REPLICATION SLAVE ON *.* to 'mysqlback'@'192.168.0.151' identified by '123456'; #给mysql1授权同步权限
Query OK, 0 rows affected (0.01 sec)
MariaDB [test]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
MariaDB [test]> show master status\G #查看mysql2的master状态,记录下 FILE 及 Position 的值,给MySQL1同步配置用
*************************** 1. row ***************************
File: master-bin.000001
Position: 1664
Binlog_Do_DB:
Binlog_Ignore_DB: mysql,information_schema
1 row in set (0.00 sec)
返回到MySQL1
MariaDB [test]> slave stop; #先停掉slave在做change
Query OK, 0 rows affected (0.01 sec)
MariaDB [test]> change master to master_host='192.168.0.152',master_user='mysqlback',master_password='123456',master_log_file='master-bin.000001',master_log_pos=1664;
ERROR 1201 (HY000): Could not initialize master info structure; more error messages can be found in the MariaDB error log
#报错处理如下
MariaDB [(none)]> reset slave;
Query OK, 0 rows affected (0.01 sec)
重置slave后再次change
MariaDB [test]> change master to master_host='192.168.0.152',master_user='mysqlback',master_password='123456',master_log_file='master-bin.000001',master_log_pos=1664;
Query OK, 0 rows affected (0.01 sec)
MariaDB [test]> slave start;
Query OK, 0 rows affected (0.00 sec)
MariaDB [test]> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.0.152
Master_User: mysqlback
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-bin.000001
Read_Master_Log_Pos: 1664
Relay_Log_File: mariadb-relay-bin.000002
Relay_Log_Pos: 530
Relay_Master_Log_File: master-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: test
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 1664
Relay_Log_Space: 826
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 2
1 row in set (0.00 sec)
到此,mariadb互为主从基本配置完成。
5.验证
我们只授权同步数据库test
mysql1操作如下:
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)
MariaDB [(none)]> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
MariaDB [test]> show tables;
Empty set (0.00 sec)
MariaDB [test]> create table user(id int not null,name CHAR(20) not null,phone CHAR(11) not null);
Query OK, 0 rows affected (0.00 sec)
mysql2操作如下
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)
MariaDB [(none)]> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
MariaDB [test]> show tables;
+----------------+
| Tables_in_test |
+----------------+
| user |
+----------------+
1 row in set (0.00 sec)
MariaDB [test]> insert into user values(1,'zhuo','13344445555');
Query OK, 1 row affected (0.01 sec)
mysql1再看
MariaDB [test]> select * from user;
+----+------+-------------+
| id | name | phone |
+----+------+-------------+
| 1 | zhuo | 13344445555 |
+----+------+-------------+
1 row in set (0.00 sec)
如此已经OK,mysql1和mysql2互为主从
6.主从失败处理
通常在Slave上查看IO和SQL是否为Yes
MariaDB [(none)]> show slave status\G
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
若IO和SQL出现NO,则表示Slave不同步
一.简单粗暴两板斧
a.忽略错误,继续同步
用于主从库数据相差不大,对数据要求不严格,允许数据不完全统一时
stop slave;
set global sql_slave_skip_counter =1; #跳过一步错误
start slave;
之后再查看slave:
MariaDB [(none)]> show slave status\G #看到IO和SQL为Yes
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
则主从同步OK
b.重做主从,完全同步
用于主从库数据相差较大,对数据要求严格,数据需要完全统一
1.进行主库锁表,防止数据写入
MariaDB [(none)]> flush tables with read lock; #锁定为只读状态
2.主库数据备份
[root@localhost ~]# mysqldump -uroot -p test>test.back.sql
3.查看master状态
MariaDB [(none)]> show master status\G #记录下file名称和Pos,同步从库时用
1. row
File: master-bin.000006
Position: 855
Binlog_Do_DB:
Binlog_Ignore_DB: mysql,information_schema
1 row in set (0.00 sec)
4.用主数据库备份文件恢复从库
[root@localhost ~]# scp test.back.sql 192.168.0.152:/root
5.停掉从库
MariaDB [(none)]> stop slave;
Query OK, 0 rows affected (0.00 sec)
6.导入数据备份
MariaDB [(none)]> source /root/test.back.sql
7.从库同步
MariaDB [(none)]> change master to master_host='192.168.0.151',master_user='mysqlback',master_password='123456',master_log_file='master-bin.000006',master_log_pos=855;
Query OK, 0 rows affected (0.01 sec)
8.重新开启从同步
MariaDB [(none)]> slave start;
Query OK, 0 rows affected (0.00 sec)
9.查看从库同步状态
MariaDB [(none)]> show slave status\G
1. row
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.0.151
Master_User: mysqlback
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-bin.000006
Read_Master_Log_Pos: 855
Relay_Log_File: mariadb-relay-bin.000002
Relay_Log_Pos: 530
Relay_Master_Log_File: master-bin.000006
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: test
IO和SQL都显示Yes则同步OK
10.给主库解锁
MariaDB [test]> unlock tables;
Query OK, 0 rows affected (0.00 sec)
此两种方法暴力直接,基本能解决所有同步问题,但有时候数据量大,也是一些小问题用不了大炮打蚊子的招数
1.机器重启后,事务回滚造成同步失败
2.从库未配置read_only=1,程序有可能在 slave 上进行了写操作
3.数据不一致:包括删除失败、主键重复、更新丢失、字符、时间等问题
……
第一板斧搞不定
第二板斧缩略版(不用备份还原数据,停掉slave,重新change到新的file名称和Pos,执行不成功则 reset slave后执行)