首页 > 代码库 > 【备忘】mysql主从设置

【备忘】mysql主从设置

主(master)192.168.1.10机器设置:

[root@vm-vagrant mysql]# vi my.cnf

[mysqld]节点下添加以下配置
server-id=1
log-bin=mysql-bin #这个一定得设置,否则没有日志的话,从数据库上会报错

[root@vm-vagrant mysql]# /etc/init.d/mysql restart
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!
[root@vm-vagrant mysql]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.28-log Source distribution

Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.

给从库账号授权
mysql> GRANT REPLICATION SLAVE,RELOAD,SUPER ON *.* TO backup@‘192.168.1.11‘ IDENTIFIED BY ‘123456‘;
Query OK, 0 rows affected (0.03 sec)

mysql> UNLOCK TABLES;
Query OK, 0 rows affected (0.00 sec)

mysql> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 346 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

mysql>

 

从(master)192.168.1.11机器设置:
[root@vm-vagrant mysql]# vi my.cnf

[mysqld]节点下添加以下配置
log-bin = mysql-bin
server_id = 2


重启
[root@vm-vagrant mysql]# /etc/init.d/mysql restart
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!

配置master信息
mysql> CHANGE MASTER TO MASTER_HOST=‘192.168.1.10‘, MASTER_USER=‘backup‘, MASTER_PASSWORD=‘123456‘, MASTER_PORT=3306, MASTER_LOG_FILE=‘mysql-bin.000001‘, MASTER_LOG_POS=106,MASTER_CONNECT_RETRY=10;
Query OK, 0 rows affected, 2 warnings (0.04 sec)

mysql> start slave;
Query OK, 0 rows affected (0.03 sec)

可以查看slave的状态
mysql> show slave status;

发现报错了:
Got fatal error 1236 from master when reading data from binary log: ‘log event entry exceeded max_allowed_packet; Increase max_allowed_packet on master; the first event ‘mysql-bin.000001‘ at 106, the last event read from ‘./mysql-bin.000001‘ at 106, the last byte read from ‘./mysql-bin.000001‘ at 125.‘

将master机上的日志导出来
mysqlbinlog mysql-bin.000001 > test.txt

找到at的最后位置,然后修改上面的chang master语句 中的MASTER_LOG_POS=的值

mysql> change master to master_log_file=‘mysql-bin.000001‘,master_log_pos=1157;

然后发现简单主从配置已经实现(配置后续优化)


中间遇到的一个问题:
由于我是在vagrant上的环境,用的同一个.box文件,所以主从复制时造成uuid错误
Fatal error: The slave I/O thread stops because master and slave have equal MySQL server UUIDs; these UUIDs must be different for replication to work.
mysql版本如下
mysql> status;
--------------
mysql Ver 14.14 Distrib 5.6.28, for Linux (x86_64) using EditLine wrapper
这种情况只需要将mysql的data目录下的auto.cnf中server-uuid值改成master和slave互相不一致即可
[auto]
server-uuid=89009021-fadf-11e5-b16d-0800274fb806

【备忘】mysql主从设置