安装环境
操作系统 :CentOS Linux release 7.5.1804 (Core)
数据库版本:MySQL 5.7.24
主机A:192.168.1.11 (Master)
主机B:192.168.1.12 (Slave)
在Centos下安装mysql参考:https://blog.csdn.net/mameng1988/article/details/82762666
1 Master的配置
在Linux环境下MySQL的配置文件的位置是在 /etc/my.cnf ,在该文件下指定Master的配置如下:
[mysqld]
log-bin=mysql-bin
server-id=11
#binlog-ignore-db=information_schema
#binlog-ignore-db=cluster
#binlog-ignore-db=mysql
#binlog-do-db=ufind_db
这里的server-id用于标识唯一的数据库,这里设置为11(IP的最后两位),在设置从库的时候就需要设置为其他值。
log-bin用于打开二进制日志。
其他的都不是必需。
1.1 重启mysql
[root@localhost etc]# service mysqld restart
1.2 进入mysql
[root@localhost etc]# mysql -u root -p
1.3 赋予从库权限帐号
允许用户在主库上读取日志,赋予192.168.1.12也就是Slave机器有File权限,只赋予Slave机器有File权限还不行,还要给它REPLICATION SLAVE的权限才可以。
在Master数据库命令行中输入:
mysql>GRANT FILE ON *.* TO 'root'@'192.168.1.12' IDENTIFIED BY 'mysql password';
mysql>GRANT REPLICATION SLAVE ON *.* TO 'root'@'192.168.1.12' IDENTIFIED BY 'mysql password';
mysql>FLUSH PRIVILEGES;
这里使用的仍是 root 用户作为同步的时候使用到的用户,可以自己设定。
1.4 重启mysql
[root@localhost etc]# service mysqld restart
1.5 登录mysql
[root@localhost etc]# mysql -u root -p
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000002 | 154 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.01 sec)
这里的 File 、Position 是在配置Salve的时候要使用到的。Binlog_Do_DB表示要同步的数据库,Binlog_Ignore_DB 表示Ignore的数据库,这些都是空的,说明我们没有配置,说明它们不是必需的。
另外:如果执行这个步骤始终为Empty set(0.00 sec),那说明前面的my.cnf没配置对。
2 Slave的配置
2.1 修改/etc/my.cnf
[mysqld]
server-id=12
relay_log=mysql-relay-bin
read_only=1
# 如果此slave需要作为其他mysql的master,则需要把下边两行注释打开。
# log-bin=mysql-bin
# log_slave_updates=1
server_id 是必须的,也是唯一的,这里设置为12(IP的后两位),不能和master及其他slave一样。
relay_log 配置中继日志。
read_only 它防止改变数据(除了特殊的线程。
2.2 重启mysql
[root@localhost etc]# service mysqld restart
2.3 slave连接master
2.3.1 查看master的二进制日志文件名称及Position
在master数据库上:
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000002 | 2025 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.01 sec)
2.3.2 连接master
在slave数据库上:
mysql> change master to master_host='192.168.1.11',master_user='root',master_password='123456',master_log_file='mysql-bin.000002', master_log_pos=2025;
2.3.3 启动slave复制
在slave数据库上:
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)
2.3.4 主从同步状态检查
在slave数据库上:
mysql> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.11
Master_User: root
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 2025
Relay_Log_File: localhost-relay-bin.000002
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-bin.000002
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
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: 2025
Relay_Log_Space: 531
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: 45
Master_UUID: 240f86da-e69c-11e8-92e1-6c92bf135230
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 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.01 sec)
其中Slave_IO_Running 与 Slave_SQL_Running 的值都必须为YES,才表明状态正常。
3 验证主从复制效果
3.1 在master数据库上
mysql> create database test;
Query OK, 1 row affected (0.00 sec)
mysql> use test;
Database changed
mysql> create table t_user(id int NOT NULL AUTO_INCREMENT,name varchar(64), lastime datetime,PRIMARY KEY (`id`) USING BTREE) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
Query OK, 0 rows affected (0.00 sec)
mysql> insert into t_user(id,name,lastime) values(1,'Moxiao',now());
Query OK, 1 row affected (0.01 sec)
3.2 在slave数据库上
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.00 sec)
mysql> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select * from t_user;
+------+-------+----------------------------------+
| id | name | lastime |
+------+-------+----------------------------------+
| 1 | Moxiao| 2018-11-13 15:22:39 |
+------+-------+----------------------------------+
1 row in set (0.00 sec)
主从复制成功!