MySQL5.7主从复制配置

1 my.cnf文件 配置

  binlog_format = ROW
  log_bin_trust_function_creators=1
  log-error = /usr/local/mysql/data/hostname.err
  log-bin = /usr/local/mysql/arch/mysql-bin
  expire_logs_days = 7

#server-id需要与master不一致

  server-id = 1739

2 在master主机上创建同步用户

grant replication slave on *.* to sync@'%' identified by 'Zj123456!';
flush privileges;

3 查看主机上master status

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000002 | 989 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

mysql>

扫描二维码关注公众号,回复: 4908723 查看本文章

4  从库上执行master

mysql>
change master to
master_host='192.168.56.100',
master_port=3306,
master_user='sync',
master_password='Zj123456!',
master_log_file='mysql-bin.000005',
master_log_pos=3820;
     
mysql> start slave;
 
mysql>show slave status\G;

*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.56.100
Master_User: sync
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000005
Read_Master_Log_Pos: 3820
Relay_Log_File: relay-log.000002
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-bin.000005
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table: omsprd.%,wmsb01.%,wmsb02.%,wmsb03.%,wmsb04.%,wmsb05.%,wmsb06.%,wmsb07.%,wmsb08.%,wmsb08.%,wmsb09.%,wmsb10.%,wmsb11.%,wmsb27.%,wmsb31.%,wmsb32.%,wmsb33.%,wmsb34.%,wmsb35.%
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 3820
Relay_Log_Space: 521
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: 1739
Master_UUID: 71f0e5b7-16f2-11e9-949d-0800271f440a
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)

ERROR:
No query specified

看到这两行,表示成功

Slave_IO_Running: Yes
Slave_SQL_Running: Yes

5 验证同步是否成功

查看my.cnf 设置同步的数据库与表

#need to sync tables
replicate-wild-do-table=omsprd.%
replicate_wild_do_table=wmsb01.%
replicate_wild_do_table=wmsb02.%
replicate_wild_do_table=wmsb03.%
replicate_wild_do_table=wmsb04.%
replicate_wild_do_table=wmsb05.%
replicate_wild_do_table=wmsb06.%
replicate_wild_do_table=wmsb07.%
replicate_wild_do_table=wmsb08.%
replicate_wild_do_table=wmsb08.%
replicate_wild_do_table=wmsb09.%
replicate_wild_do_table=wmsb10.%
replicate_wild_do_table=wmsb11.%
replicate_wild_do_table=wmsb27.%
replicate_wild_do_table=wmsb31.%
replicate_wild_do_table=wmsb32.%
replicate_wild_do_table=wmsb33.%
replicate_wild_do_table=wmsb34.%
replicate_wild_do_table=wmsb35.%

验证

主库上操作
mysql> create database omsprd;
Query OK, 1 row affected (0.00 sec)

mysql> use omsprd;
Database changed

mysql> CREATE TABLE `t_banner` (
-> `id` int(11) NOT NULL AUTO_INCREMENT,
-> `img` varchar(128) NOT NULL,
-> `url` varchar(128) NOT NULL,
-> `creater` varchar(10) NOT NULL,
-> `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
-> `name` varchar(64) NOT NULL,
-> `is_delete` int(11) NOT NULL DEFAULT '0',
-> PRIMARY KEY (`id`)
-> ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 COMMENT='广告';
Query OK, 0 rows affected (0.02 sec)

mysql> insert into t_banner(id,img,url,creater,name) values(1,'zhangsan', 'php001','zhangjin','test');
Query OK, 1 row affected (0.00 sec)

mysql>

从库查询


mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| omsprd |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)

mysql> use omsprd;
Database changed
mysql> show tables;
+------------------+
| Tables_in_omsprd |
+------------------+
| t_banner |
+------------------+
1 row in set (0.00 sec)

mysql> select * from t_banner;
+----+----------+--------+----------+---------------------+------+-----------+
| id | img | url | creater | create_time | name | is_delete |
+----+----------+--------+----------+---------------------+------+-----------+
| 1 | zhangsan | php001 | zhangjin | 2019-01-13 06:58:58 | test | 0 |
+----+----------+--------+----------+---------------------+------+-----------+
1 row in set (0.00 sec)

mysql>

期间遇到的问题

1 mysql从库启动不了,与主库的uuid一致,删除data文件,重新初始化搞定

2 Slave_IO_Running: NO 

  第一次 密码不对,修改后还是不可以

  第二次 重新刷新用户名赋予权限,远程登录可以,重新change to master后可以搞定。

猜你喜欢

转载自www.cnblogs.com/QuestionsZhang/p/10265589.html