mysql 5.6 主从同步配置_mysql5.6 主从同步配置

一:配置前说明

在centos 6环境下配置 mysql 5.6主从同步

准备两台测试的虚拟机,2台虚拟机上都安装mysql软件,并开启mysql服务

主master : 192.168.1.110

从slave : 192.168.1.109

二:配置主库

2.1: 授权给从数据库服务器

mysql> grant replication slave on *.* to 'rep1'@'192.168.1.109' identified by 'test123456';

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

Query OK, 0 rows affected (0.01 sec)

2.2: 配置主库配置文件

开启binlog,并设置server-id,每次修改配置文件后都要重启mysql服务才会生效

vi /usr/my.cnf

server-id=1log_bin=mysql_bin

binlog-do-db=student

binlog-ignore-db=mysqlbinlog_ignore_db=information_schemalog-error=/var/lib/mysql/mysql_error.log #错误日志配置

general_log=on #开启普通日志

general_log_file=/var/lib/mysql/mysql_general_log.log #普通日志配置

说明:

server-id:master端的ID号;

log-bin:同步的日志路径及文件名,一定注意这个目录要是mysql有权限写入的(我这里是偷懒了,直接放在了下面那个datadir下面);

binlog-do-db:要同步的数据库名

还可以显示 设置不同步的数据库:

binlog-ignore-db = mysql 不同步mysql库和test库

binlog-ignore-db = test

修改配置文件后,重启服务:

service mysqld restart

2.3: 查看主服务器当前二进制日志名和偏移量

这个操作的目的是为了在从数据库启动后,从这个点开始进行数据的恢复

mysql>show master status;+--------------------------+----------+------------------+-------------------------------------+-------------------+

| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |

+------------------------+----------+--------------------+--------------------------+-------------------+

| mysql_bin.000002 | 120 | student | mysql,information_schema | |

+------------------+----------+--------------+--------------------------+-------------------+

1 row in set (0.00 sec)

主库配置好了

三:配置从库

3.1:配置文件

vi /usr/my.cnf

server-id=2

#log_bin=/var/log/mysqlslave-bin.log #这里的log_bin和下面的log-bin是一个意思,配置其一就好了

log-bin=mysql-bin

master-host=192.168.1.110master-user=rep1

master-password=test123456

replicate-do-db=student

replicate-ignore-db=mysqlreplicate-ignore-db=infomation_schemalog-error=/var/lib/mysql/mysql_error.loggeneral_log=on

general_log_file=/var/lib/mysql/mysql_general_log.log

配置完成后,重启mysql,

[root@localhost mysql]#service mysql restart

Shutting down MySQL.... SUCCESS!StartingMySQL... ERROR! The server quit without updating PID file (/var/lib/mysql/localhost.localdomain.pid).

然后就报错了

查看vi /var/lib/mysql/mysql_error.log日志,发现2行错误信息

[ERROR] /usr/sbin/mysqld: unknown variable 'master-host=192.168.1.110'

[ERROR] Aborting

说明mysql不认识这些变量,网上搜索,原因是mysql5.5+版本主从复制不支持这些变量,需要在从库上用命令来设置,那我们就来设置了,先注释掉3个变量

master-host=192.168.1.110

master-user=rep1

master-password=test123456

重新启动mysql

[root@localhost mysql]#service mysql start

Starting MySQL. SUCCESS!

用root用户登录进mysql, 用下面的命令设置

mysql> change master to master_host='192.168.1.110', master_port=3306, master_user='rep1', master_password='test123456',master_log_file='mysql_bin.000002',master_log_pos=120;

Query OK, 0 rows affected, 2 warnings (0.02 sec)

3.2:启动slave进程

mysql>slave start;

ERROR1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'slave start' at line 1

报错了,查看错误日志:

[Note] /usr/sbin/mysqld: ready for connections.Version: '5.6.40-log' socket: '/var/lib/mysql/mysql.sock' port: 3306 MySQLCommunity Server (GPL)[Warning] Neither --relay-log nor --relay-log-index were used; so replication may break when this MySQL server acts as a slave and has his hostname changed!! Please use '--relay-log=localhost-relay-bin' to avoid this problem.

[Note] 'CHANGE MASTER TO executed'. Previous state master_host='', master_port= 3306, master_log_file='', master_log_pos= 4, master_bind=''. New state master_host='192.168.1.110', master_port= 3306, master_log_file='mysql_bin.000002', master_log_pos= 120, master_bind=''.

看错误日志信息,是前面的没有设置导致的mysql_port, master_log_file的值为空和后面的设置的相冲突了,重启mysql试试看 service mysql restart

又出错了

[Note] /usr/sbin/mysqld: ready for connections.Version: '5.6.40-log' socket: '/var/lib/mysql/mysql.sock' port: 3306 MySQLCommunity Server (GPL)[Warning] Storing MySQL user name or password information in the master info repository is not secure and is therefore not recommended. Please consider using the USER and PASSWORD connection options for START SLAVE; see the 'START SLAVE Syntax' in the MySQL Manual for more information.

[Warning] Slave SQL: If a crash happens this configuration does not guarantee that the relay log info will be consistent, Error_code: 0

[Note] Slave SQL thread initialized, starting replication in log 'mysql_bin.000002' at position 120, relay log './localhost-relay-bin.000001' position: 4

[Note] Slave I/O thread: connected to master 'rep1@192.168.1.110:3306',replication started in log 'mysql_bin.000002' at position 120

[ERROR] Slave I/O: 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. Error_code: 1593

[Note] Slave I/O thread exiting, read up to log 'mysql_bin.000002', position 120

上网搜索原因

参考:https://blog.csdn.net/cug_jiang126com/article/details/46846031

原因分析:

mysql 5.6的复制引入了uuid的概念,各个复制结构中的server_uuid得保证不一样,但是查看到直接copy data文件夹后server_uuid是相同的,show variables like '%server_uuid%';

解决方法:

找到data文件夹下的auto.cnf文件,修改里面的uuid值,保证各个db的uuid不一样,重启db即可

修改auto.cnf中的uuid配置

vi /var/lib/mysql/auto.cnf

修改server-uuid这个值,跟master主数据库的不同就好,重启mysql

[root@localhost mysql]#service mysql restart

Shutting down MySQL.. SUCCESS!StartingMySQL. SUCCESS!

进入mysql的, 运行 slave start; 命令

报错

mysql>slave start;

ERROR1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'slave start' at line 1

看日志,并没有发现error级别的错误, 而且复制的线程已经初始化了,所以应该是重启mysql之后,mysql会记住之前的slave start命令,把复制命令运行了

3.3:查看slave的状态

show slave status\G;

后面有个\G的结束符号,是可以格式化的来看信息,不然出来的信息就是一团乱的,

mysql> show slave status\G;

*************************** 1. row ***************************

Slave_IO_State: Waiting for master to send event

Master_Host: 192.168.1.110

Master_User: rep1

Master_Port: 3306

Connect_Retry: 60

Master_Log_File: mysql_bin.000002

Read_Master_Log_Pos: 120

Relay_Log_File: localhost-relay-bin.000004

Relay_Log_Pos: 283

Relay_Master_Log_File: mysql_bin.000002

Slave_IO_Running: Yes

Slave_SQL_Running: Yes

Replicate_Do_DB: student

Replicate_Ignore_DB: mysql,infomation_schema

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: 120

Relay_Log_Space: 460

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

Master_UUID: 9a672e1c-471e-11e8-a9e3-08002708e616

Master_Info_File: /var/lib/mysql/master.info

SQL_Delay: 0

SQL_Remaining_Delay: NULL

Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it

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

1 row in set (0.00 sec)

从库正在等待主库更新,Slave_IO_State: Waiting for master to send event

配置成功了

四:同步主库已有数据到从库

主库操作:

1、停止主库的数据更新操作mysql>flushtables with read lock;2、新开终端,生成主数据库的备份(导出数据库)

[root@zhoujietest~]#mysqldump -uroot -proot student > student.sql

3、将备份文件传到从库

[root@zhoujietest~]#scp student.sql root@192.168.1.109:/root/

4、主库解锁mysql>unlock tables;

从库操作:

1、停止从库slavemysql>slave stop;2、新建数据库studentmysql> create database student defaultcharset utf8;3、导入数据

[root@ops-dev ~]#mysql -uroot -proot student

4、查看从库已有该数据库和数据

mysql> show databases;

+--------------------+

| Database |

+--------------------+

| information_schema |

|student |

| mysql |

| performance_schema |

| test |

+--------------------+

此时主从库的数据完全一致,如果对主库进行增删改操作,从库会自动同步进行操作。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值