1.简介
MySQL作为世界上使用最为广泛的数据库之一,免费是其原因之一。但不可忽略的是它本身的功能的确很强大。随着技术的发展,在实际的生产环境中,由单台MySQL数据库服务器不能满足实际的需求。此时数据库集群就很好的解决了这个问题了。采用MySQL分布式集群,能够搭建一个高并发、负载均衡的集群服务器(这篇博客暂时不涉及)。在此之前我们必须要保证每台MySQL服务器里的数据同步。数据同步我们可以通过MySQL内部配置就可以轻松完成,主要有主从复制和主主复制。
2.环境说明
两台linux虚拟主机
Linux版本CentOS 7.2、MySQL 5.7
ip:192.168.178.198(主)、192.168.178.199(从)
3.主从复制
- 主服务器 开启 log-bin
#log_bin
log-bin=/var/lib/mysql/mysql-bin
server-id=1
- 从服务器 设置server-id
server-id=2
- 开始构建主从复制
主库操作
mysql> CREATE USER 'repl1'@'%' IDENTIFIED BY '123456';
mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl1'@'192.168.178.198' IDENTIFIED BY '123456';
mysql> FLUSH PRIVILEGES;
mysql> SHOW master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000003 | 2723 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
mysql> show master logs;
+------------------+-----------+
| Log_name | File_size |
+------------------+-----------+
| mysql-bin.000001 | 2079 |
| mysql-bin.000002 | 177 |
| mysql-bin.000003 | 2723 |
+------------------+-----------+
3 rows in set (0.00 sec)
mysql> FLUSH TABLE WITH READ LOCK; # 全库读锁
[root@localhost ~]# mysqldump -uroot -p123456 -S /var/lib/mysql/mysql.sock -A -B --events|gzip >/opt/rep.sql.gz # 数据量大
[root@localhost ~]# mysqldump -uroot -p123456 -S /var/lib/mysql/mysql.sock -A -B --events --master-data=2 >/opt/rep.sql
mysql> UNLOCK tables; # 解锁
从库先将主库的全备数据同步
[root@localhost ~]# mysql-uroot -p123456 -S /var/lib/mysql/mysql.sock >/opt/rep.sql
下面进行主从复制
mysql> CHANGE MASTER TO MASTER_HOST='192.168.178.198',MASTER_USER='repl1',MASTER_PASSWORD='123456',MASTER_LOG_FILE='mysql-bin.000003',MASTER_LOG_POS=2723;
Query OK, 0 rows affected, 2 warnings (0.01 sec)
mysql> START SLAVE;
Query OK, 0 rows affected (0.00 sec)
# 查看 master.info
root@localhost ~]# cat /var/lib/mysql/master.info
25
mysql-bin.000003
2723
192.168.178.198
repl1
123456
3306
60
0
0
30.000
0
86400
0
查看主从复制是否配置成功, 当看到Slave_IO_Running: YES、Slave_SQL_Running: YES才表明状态正常
mysql> SHOW SLAVE STATUS\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.178.198
Master_User: repl1
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000004
Read_Master_Log_Pos: 154
Relay_Log_File: localhost-relay-bin.000003
Relay_Log_Pos: 367
Relay_Master_Log_File: mysql-bin.000004
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: 154
Relay_Log_Space: 744
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: f6e6d2d0-cae8-11e7-8def-000c29462b91
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.00 sec)
- ok,现在主从复制的所有操作已经完成了,现在在主库进行数据库操作就会自动同步到从库