主从同步是分布式mysql数据库相当重要的配置,现在我在虚拟机上完成主从配置,系统是CenterOS6.5,mysql版本是5.7.13
主服务器的ip是192.168.19.139 副服务器的ip是192.168.19.142
1.主服务器配置
(1)修改my.cnf(注意使用root)
1 vim /etc/my.cnf2
3
4 sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES5 log-bin=mysqlbin-log6 server_id=1
7 basedir=/home/mysql8 user=mysql9 datadir=/home/mysql/data10 port=3306
11 innodb_flush_log_at_trx_commit=1
12 sync_binlog=1
13 log-slave-updates
(2)进入mysql安装目录下的bin,启动mysql
1 cd /home/mysql/bin2 ./mysqld
(3)重开启mysql客户端,进入mysql安装目录下的bin,授权副服务器主从复制,用mysqld的好处是可以查看错误。
1 ./mysql -uroot -p2 mysql> grant replication slave on *.* to test@192.168.19.142 identified by '123';
(4)显示主服务器状态
1 mysql>show master status;2 +---------------------+----------+--------------+------------------+-------------------+
3 | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
4 +---------------------+----------+--------------+------------------+-------------------+
5 | mysqlbin-log.000019 | 1290 | | | |
6 +---------------------+----------+--------------+------------------+-------------------+
2.副服务器配置
(1)修改my.cnf(注意使用root)
1 vim /etc/my.cnf2
3 sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLE4 user=mysql5 server_id = 2
6 replicate-do-db =sbfxd7 master-info-file =master.info8 relay-log = relay-relay-bin9 relay-log-index = relay-relay-bin.index10 relay-log-info-file=relay-relay-log.info
(2)进入mysql安装目录下的bin,启动mysql
1 cd /home/mysql/bin 2 ./mysqld
(3)重开启mysql客户端,进入mysql安装目录下的bin,设置slave属性并开始服务
1 ./mysql -uroot -p2 mysql> change master to master_host='192.168.19.139',master_port= 3306, master_log_file='mysqlbin-log.000019', master_log_pos= 1290, master_bind='', master_user='test',master_password='123';3 mysql> start slave;
(4)显示副服务器的状态
1 show slave status \G;2
3 Slave_IO_State: Waiting for master to send event
4 Master_Host: 192.168.19.139
5 Master_User: test6 Master_Port: 3306
7 Connect_Retry: 60
8 Master_Log_File: mysqlbin-log.000019
9 Read_Master_Log_Pos: 1290
10 Relay_Log_File: relay-relay-bin.000002
11 Relay_Log_Pos: 323
12 Relay_Master_Log_File: mysqlbin-log.000019
13 Slave_IO_Running: Yes14 Slave_SQL_Running: Yes15 Replicate_Do_DB: sbfxd16 Replicate_Ignore_DB:17 Replicate_Do_Table:18 Replicate_Ignore_Table:19 Replicate_Wild_Do_Table:20 Replicate_Wild_Ignore_Table:21 Last_Errno: 0
22 Last_Error:23 Skip_Counter: 0
24 Exec_Master_Log_Pos: 1290
25 Relay_Log_Space: 530
26 Until_Condition: None27 Until_Log_File:28 Until_Log_Pos: 0
29 Master_SSL_Allowed: No30 Master_SSL_CA_File:31 Master_SSL_CA_Path:32 Master_SSL_Cert:33 Master_SSL_Cipher:34 Master_SSL_Key:35 Seconds_Behind_Master: 0
36 Master_SSL_Verify_Server_Cert: No37 Last_IO_Errno: 0
38 Last_IO_Error:39 Last_SQL_Errno: 0
40 Last_SQL_Error:41 Replicate_Ignore_Server_Ids:42 Master_Server_Id: 1
43 Master_UUID: 05408021-5ab2-11e6-869b-000c293990c144 Master_Info_File: /home/mysql/data/master.info45 SQL_Delay: 0
46 SQL_Remaining_Delay: NULL47 Slave_SQL_Running_State: Slave has read all relay log; waiting formore updates48 Master_Retry_Count: 86400
49 Master_Bind:50 Last_IO_Error_Timestamp:51 Last_SQL_Error_Timestamp:52 Master_SSL_Crl:53 Master_SSL_Crlpath:54 Retrieved_Gtid_Set:55 Executed_Gtid_Set:56 Auto_Position: 0
57 Replicate_Rewrite_DB:58 Channel_Name:59 Master_TLS_Version:
出现两个yes表示slave可以跑了
3.注意事项:
(1)楼主的虚拟机是通过克隆产生的,所以不需要对mysql进行备份,然后在副服务器上恢复,这步对于数据库极其重要,否则就会出现如下错误:
1 2016-08-17T03:11:27.579193Z 5 [ERROR] Slave SQL for channel '': Error executing row event: 'Table'sbfxd.sbtable'doesn't exist', Error_code: 1146
2 2016-08-17T03:11:27.584268Z 5 [Warning] Slave: Table 'sbfxd.sbtable' doesn't exist Error_code: 1146
(2)如果将主服务器的属性配错了需要stop slave,然后设置好之后start slave