版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_24871519/article/details/84962557
mysql 主从模式配置
MySQL数据库自身提供的主从复制功能可以方便的实现数据的多处自动备份,实现数据库的拓展。多个数据备份不仅可以加强数据的安全性,通过实现读写分离还能进一步提升数据库的负载性能。
如下图可看到读写分离的实现:主机负责写入数据,从机负责读取
场景描述
当前有两台centos7.3 的服务器,两台都已经搭建好mysql 服务,我们希望搭建一主一从的模式实现从机自动备份。
主机:master 172.18.100.86
从机:slave 172.18.100.87
实现原理
Mysql 是基于二进制日志文件来进行数据复制的。主机开启二进制日志后,其数据库中的所有操作都会以事件的形式记录在二进制文件中,从机通过与主机保持通信,时刻监控着主机二进制日志的变化,若发生变化,则将变化复制到自己的中继日志中,然后主机会启用一些线程来处理中继日志中的事件,将其更新到自己的数据库中,从而实现了主从数据的一致性。
开始配置
前提
- 主从两台机器都已经搭建好mysql 服务
- 主从mysql 数据库版本最好一致
- 主从mysql 数据库内数据保持一致
主机配置
找到mysql的配置文件my.cnf, 我的是/etc/my.cnf,在【mysqld】部分加入两句:
[mysqld]
log-bin=mysql-bin
server-id=1
第一行表示开启二进制日志,第二行设置server-id
然后重启mysql 登录mysql shell,创建用于同步的用户账号:
systemctl restart mysqld
mysql -u root -p
mysql> CREATE USER 'rep'@'172.18.100.87' IDENTIFIED BY 'slavePass123';
mysql> GRANT REPLICATION SLAVE ON *.* TO 'rep'@'172.18.100.87';
mysql>flush privileges;
查看master 状态:记下二进制文件名(mysql-bin.000001)和位置(514):
mysql> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 514 | | | |
+------------------+----------+--------------+------------------+-------------------+
从机配置
找到mysql的配置文件my.cnf, 加入一句:
[mysqld]
server-id=2
此server-id 不可重复
然后重启mysql, 登录进入mysql shell:
mysql -u root -p
mysql> CHANGE MASTER TO
-> MASTER_HOST='172.18.100.86',
-> MASTER_USER='rep',
-> MASTER_PASSWORD='slavePass123',
-> MASTER_LOG_FILE='mysql-bin.000001',
-> MASTER_LOG_POS=514;
Query OK, 0 rows affected, 2 warnings (0.07 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.18.100.86
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 514
Relay_Log_File: mysqld-relay-bin.000002
Relay_Log_Pos: 283
Relay_Master_Log_File: mysql-bin.000001
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: 514
Relay_Log_Space: 457
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: 8f403c8c-fd09-11e8-8f8a-0050569f233f
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)
额外配置选项
master开启二进制日志后默认记录所有库所有表的操作,可以通过配置来指定只记录指定的数据库甚至指定的表的操作.
不同步哪些数据库:
binlog-ignore-db = mysql
binlog-ignore-db = user
binlog-ignore-db = information_schema
只同步哪些数据库,除此之外,其他不同步:
binlog-do-db = music
验证主从模式
在主机中建立数据库表,添加数据,然后在从机中都会更新这些记录。