Mysql5.7.13主从同步(复制)配置

时间:2021-12-22 12:08:20

主从同步是分布式mysql数据库相当重要的配置,现在我在虚拟机上完成主从配置,系统是CenterOS6.5,mysql版本是5.7.13

主服务器的ip是192.168.19.139 副服务器的ip是192.168.19.142

1.主服务器配置

(1)修改my.cnf(注意使用root)

 vim /etc/my.cnf

 sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
log-bin=mysqlbin-log
server_id=
basedir=/home/mysql
user=mysql
datadir=/home/mysql/data
port=
innodb_flush_log_at_trx_commit=
sync_binlog=
log-slave-updates

(2)进入mysql安装目录下的bin,启动mysql

cd /home/mysql/bin ./mysqld

(3)重开启mysql客户端,进入mysql安装目录下的bin,授权副服务器主从复制,用mysqld的好处是可以查看错误。

./mysql -uroot -p mysql> grant replication slave on *.* to test@192.168.19.142 identified by '';

(4)显示主服务器状态

 mysql> show master status;
+---------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------------+----------+--------------+------------------+-------------------+
| mysqlbin-log. | | | | |
+---------------------+----------+--------------+------------------+-------------------+

2.副服务器配置

(1)修改my.cnf(注意使用root)

 vim /etc/my.cnf

 sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLE
user=mysql
server_id =
replicate-do-db = sbfxd
master-info-file = master.info
relay-log = relay-relay-bin
relay-log-index = relay-relay-bin.index
relay-log-info-file=relay-relay-log.info

(2)进入mysql安装目录下的bin,启动mysql

1 cd /home/mysql/bin 2 ./mysqld

(3)重开启mysql客户端,进入mysql安装目录下的bin,设置slave属性并开始服务

 ./mysql -uroot -p
mysql> change master to master_host='192.168.19.139',master_port= , master_log_file='mysqlbin-log.000019', master_log_pos= , master_bind='', master_user='test',master_password='123';
mysql> start slave;

(4)显示副服务器的状态

 show slave status \G;

             Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.19.139
Master_User: test
Master_Port:
Connect_Retry:
Master_Log_File: mysqlbin-log.
Read_Master_Log_Pos:
Relay_Log_File: relay-relay-bin.
Relay_Log_Pos:
Relay_Master_Log_File: mysqlbin-log.
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: sbfxd
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno:
Last_Error:
Skip_Counter:
Exec_Master_Log_Pos:
Relay_Log_Space:
Until_Condition: None
Until_Log_File:
Until_Log_Pos:
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master:
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno:
Last_IO_Error:
Last_SQL_Errno:
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id:
Master_UUID: -5ab2-11e6-869b-000c293990c1
Master_Info_File: /home/mysql/data/master.info
SQL_Delay:
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count:
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position:
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:

出现两个yes表示slave可以跑了

3.注意事项:

(1)楼主的虚拟机是通过克隆产生的,所以不需要对mysql进行备份,然后在副服务器上恢复,这步对于数据库极其重要,否则就会出现如下错误:

 --17T03::.579193Z  [ERROR] Slave SQL for channel '': Error executing row event: 'Table 'sbfxd.sbtable' doesn't exist', Error_code: 1146
--17T03::.584268Z [Warning] Slave: Table 'sbfxd.sbtable' doesn't exist Error_code: 1146

(2)如果将主服务器的属性配错了需要stop slave,然后设置好之后start slave