MySQL版本:MySQL5.7
Linux版本:CentOS7.0
主数据库A地址:192.168.1.11
主数据库B地址:192.168.1.12
找到主数据库的配置文件my.cnf,在[mysqld]部分添加如下内容:
[root@Mysql11 ~]# vim /etc/my.cnf
[mysqld]
.........
server-id=1 ### 服务器ID
log-bin=mysql-bin ### 开启binlog
binlog_format=mixed ### binlog模式为mixed
binlog_row_image=MINIMAL
binlog-rows-query-log_events=1
### 设置同步时需要忽略哪些数据库,一般同步不要同步mysql库,用户名和密码信息在mysql数据库中
binlog-ignore-db=mysql
binlog-ignore-db=sys
relay-log=mysql-relay-log ### 开启relay log
........
配置完成后重启mysql服务,查看binlog日志文件信息:
[root@Mysql11 ~]# ll /var/lib/mysql/mysql-bin*
-rw-r-----. 1 mysql mysql 1461 7月 5 21:58 /var/lib/mysql/mysql-bin.000001
-rw-r-----. 1 mysql mysql 19 7月 5 21:57 /var/lib/mysql/mysql-bin.index
mysql> grant replication slave on *.* to 'repl'@'192.168.1.%' identified by '123456';
Query OK, 0 rows affected, 1 warning (0.02 sec)
mysql> select user,host from mysql.user;
+---------------+-------------+
| user | host |
+---------------+-------------+
| wgx | % |
| repl | 192.168.1.% |
| mysql.session | localhost |
| mysql.sys | localhost |
| root | localhost |
+---------------+-------------+
5 rows in set (0.03 sec)
[root@Mysql11 ~]# mysqldump -uroot -p123456 --all-databases --master-data=2 --events > /tmp/all_bak.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@Mysql11 ~]# ls /tmp/
all_bak.sql
[root@Mysql11 ~]# scp /tmp/all_bak.sql 192.168.1.12:/tmp/
root@192.168.1.12's password:
all_bak.sql 100% 835KB 835.5KB/s 00:00
[root@Mysql11 ~]#
mysql> show master logs;
+------------------+-----------+
| Log_name | File_size |
+------------------+-----------+
| mysql-bin.000001 | 1461 |
+------------------+-----------+
1 row in set (0.01 sec)
找到从数据库的配置文件my.cnf,在[mysqld]部分添加如下内容:
[root@Mysql11 ~]# vim /etc/my.cnf
[mysqld]
.........
server-id=2 ### 服务器ID
log-bin=mysql-bin ### 开启binlog
binlog_format=mixed ### binlog模式为mixed
binlog_row_image=MINIMAL
binlog-rows-query-log_events=1
### 设置同步时需要忽略哪些数据库,一般同步不要同步mysql库,用户名和密码信息在mysql数据库中
binlog-ignore-db=mysql
binlog-ignore-db=sys
relay-log=mysql-relay-log ### 开启relay log
.......
配置完成后重启mysql服务,查看relay log文件信息:
[root@localhost ~]# ll /var/lib/mysql/mysql-bin*
-rw-r-----. 1 mysql mysql 154 7月 5 22:09 /var/lib/mysql/mysql-bin.000001
-rw-r-----. 1 mysql mysql 19 7月 5 22:09 /var/lib/mysql/mysql-bin.index
[root@localhost ~]# mysql -uroot -p123456 < /tmp/all_bak.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
在从服务器上执行如下命令:
change master to
master_host='192.168.1.11',
master_port=3306,
master_user='repl',
master_password='123456',
master_log_file='mysql-bin.000001',
master_log_pos=1461;
mysql> start slave;
Query OK, 0 rows affected (0.02 sec)
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.11
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 154
Relay_Log_File: mysql-relay-log.000003
Relay_Log_Pos: 367
Relay_Master_Log_File: mysql-bin.000002
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
找到主数据库的配置文件my.cnf,在[mysqld]部分添加如下内容:
# 开启二进制日志
log-bin=mysql-bin
# 二进制模式为混合模式
binlog-format=mixed
# row模式时,只记录更新的字段
binlog_row_image=MINIMAL
# 即使在row模式下,也保存SQL语句
binlog-rows-query-log_events=1
binlog-ignore-db=mysql
binlog-ignore-db=sys
重启mysql服务,查看日志文件信息:
[root@localhost mysql]# ll /var/lib/mysql/mysql-bin*
-rw-r-----. 1 mysql mysql 154 7月 5 23:34 /var/lib/mysql/mysql-bin.000001
-rw-r-----. 1 mysql mysql 19 7月 5 23:34 /var/lib/mysql/mysql-bin.index
mysql> set global validate_password_policy=low;
Query OK, 0 rows affected (0.00 sec)
mysql> set global validate_password_length=4;
Query OK, 0 rows affected (0.00 sec)
mysql> grant replication slave on *.* to 'repl1'@'192.168.1.%' identified by '123456';
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> select host,user from mysql.user;
+-------------+---------------+
| host | user |
+-------------+---------------+
| % | wgx |
| 192.168.1.% | repl1 |
| localhost | mysql.session |
| localhost | mysql.sys |
| localhost | root |
+-------------+---------------+
5 rows in set (0.00 sec)
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 154 | | mysql,sys | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
找到从数据库的配置文件my.cnf,在[mysqld]部分添加如下内容:
[root@Mysql11 ~]# vim /etc/my.cnf
[mysqld]
.........
relay-log=mysql-relay-log ### 开启relay log
........
在从服务器上执行如下命令:
change master to
master_host='192.168.1.12',
master_port=3306,
master_user='repl1',
master_password='123456',
master_log_file='mysql-bin.000001',
master_log_pos=154;
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.12
Master_User: repl1
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 315
Relay_Log_File: mysql-relay-log.000002
Relay_Log_Pos: 481
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
mysql> create database mydata;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| aaa |
| hist |
| mydata |
| mysql |
| performance_schema |
| sys |
| wanggx |
+--------------------+
8 rows in set (0.01 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| aaa |
| hist |
| mydata |
| mysql |
| performance_schema |
| sys |
| wanggx |
+--------------------+
8 rows in set (0.00 sec)
mysql> use mydata
Database changed
mysql> create table t222(id int);
Query OK, 0 rows affected (0.04 sec)
mysql> use mydata;
Database changed
mysql> show tables;
+------------------+
| Tables_in_mydata |
+------------------+
| t222 |
+------------------+
1 row in set (0.00 sec)