MySQL-主从复制
MySQL-主从-主主配置
标签(空格分隔): mysql
环境准备
两台linux虚拟机:
A: 192.168.135.140 [主服务器]
B: 192.168.135.141 [从服务器]
主从复制
步骤-思路:
1 修改两台msyq配置文件 [vi /etc/my.cnf]
2 在主服务器[A]创建用户:
grant replication slave on *.* to '自定义用户名'@'从服务器IP[B]' identified by '自定义用户密码';
3 刷新权限
flush privileges;
4 查看主服务器[A]二进制日志名和位置
show master status;
5 在从服务器[B]告知二进制文件名与位置
slave stop; 关闭复制
change master to master_host='主服务器IP[A]', master_user='在服务器创建的用户', master_password='密码', master_log_file='主服务器的日志名', master_log_pos=主服务器的位置;
6 查看slave
slave start;
show slave status\G;
修改配置
依次修改A B 两台服务器MySQL配置文件
vi /etc/my.cnf
A :
log-bin=mysql-bin
binlog_format=mixed
server-id = 1
expire_logs_days = 10
B :
log-bin=mysql-bin
binlog_format=mixed
server-id = 2
expire_logs_days = 10
注意:二进制日志必须开启,因为数据的同步实质上就是其他的MySQL数据库服务器将这个数据变更的二进制日志在本机上再执行一遍。
开始主从复制
在A服务器中创建一个B服务器可以登陆的mysql用户
username: user2
password: user2
在A服务器:
mysql> grant replication slave on *.* to 'user2'@'192.168.135.141' identified by 'user2';
Query OK, 0 rows affected (0.01 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000019 | 338 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
在B服务:
mysql> slave stop;
mysql> change master to master_host='192.168.135.140', master_user='user2', master_password='user2', master_log_file='mysql-bin.000019', master_log_pos=338;
Query OK, 0 rows affected (0.04 sec)
mysql> slave start;
测试
mysql> slave start;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.135.140
Master_User: user2
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000009
Read_Master_Log_Pos: 796
Relay_Log_File: cherry-relay-bin.000002
Relay_Log_Pos: 253
Relay_Master_Log_File: mysql-bin.000009
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: yanweifeng
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: 796
Relay_Log_Space: 410
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_Running: Yes | Slave_SQL_Running: Yes 都是Yes时才配置成功
主主配置
思路和主从配置一样,就是把上边的操作反过来再执行一遍就可以。接着上面继续执行
在从服务器[B]创建用户,刷新权限,查看二进制文件名和位置,
关闭复制,在主服务器[A]告知二进制文件名与位置,开启复制,查看slave服务
从服务器:
mysql> grant replication slave on *.* to 'user1'@'192.168.135.140' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> show master status\G;
*************************** 1. row ***************************
File: mysql-bin.000016
Position: 432
Binlog_Do_DB:
Binlog_Ignore_DB:
1 row in set (0.00 sec)
ERROR:
No query specified
mysql>
----------------------------------------------------------------------------------------------
主服务器:
mysql> slave stop;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> change master to master_host='192.168.135.141', master_user='user2', master_password='123456', master_log_file='mysql-bin.000016', master_log_pos=432;
Query OK, 0 rows affected (0.02 sec)
mysql>
mysql> slave start;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.135.141
Master_User: user2
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000016
Read_Master_Log_Pos: 432
Relay_Log_File: cherry-relay-bin.000002
Relay_Log_Pos: 253
Relay_Master_Log_File: mysql-bin.000016
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: 432
Relay_Log_Space: 410
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)
ERROR:
No query specified
mysql>
测试
分别在两台服务器操作 看是否同步
常见出错点:
1 两台数据库都存在db数据库,而第一台MySQL db中有tab1,第二台MySQL db中没有tab1,那肯定不能成功。
2 已经获取了数据的二进制日志名和位置,又进行了数据操作,导致POS发生变更。在配置CHANGE MASTER时还是用到之前的POS。
3 stop slave后,数据变更,再start slave。出错。