MySQL实现主从复制最佳实践-亲试ok
准备工作:
-
两台虚拟机:我这里用的是CentOS6.8,IP地址分别是192.168.7.195 和192.168.4.94;
7.195 做主服务器,4.94 做从服务器(都已经安装相同版本的Mysql);
-
本机环境:Centos6.8 、MySQL5.7.23
原理:mysql要做到主从复制,其实依靠的是二进制日志,即:假设主服务器叫A,从服务器叫B;主从复制就是
B跟着A学,A做什么,B就做什么。那么B怎么同步A的动作呢?现在A有一个日志功能,把自己所做的增删改查的动作
全都记录在日志中,B只需要拿到这份日志,照着日志上面的动作施加到自己身上就可以了。这样就实现了主从复制。
扩展:MYSQL还有一种日志叫做:慢日志
可以设置一个时间,那么所有执行时间超过这个时间的SQL都会被记录下来。这样就可以通过慢日志快速的找到网站中SQL的瓶颈来进行优化。
实现步骤:
-
首先修改主库mysql的配置文件,使其支持二进制日志功能。
确认主服务器的mysql配置文件:my.conf
[root@myown_test ~]# cat /etc/my.cnf |grep log-bin
log-bin=/data/my3309/log/binlog/log-bin
log-bin-index=/data/my3309/log/binlog/log-bin.index
[root@myown_test ~]# cat /etc/my.cnf |grep binlog_format
binlog_format=row
[root@myown_test ~]# cat /etc/my.cnf |grep server-id
参数解释:
log-bin=mysql-bin 将mysql二进制日志取名为mysql-bin
binlog_format=mixed 二进制日志的格式,有三种:statement/row/mixed,
server-id=101 为服务器设置一个独一无二的id便于区分,这里使用ip地址的最后一位充当server-id
重启mysql:# service mysqld restart
同样的,进入从服务器,配置从服务器的my.cnf,重复步骤1即可,
唯一的区别是,server-id要改成从服务器的ip尾位,即server-id=105;
-
在主服务器上为从服务器分配一个账号,就像一把钥匙,从服务器拿着这个钥匙,才能到主服务器上来共享主服务器的日志文件。
在mysql操作界面下,输入下面一行命令:
mysql> grant replication slave on *.* to 'root'@'192.168.4.94' identified by '123456';
Query OK, 0 rows affected, 1 warning (0.00 sec)
参数解释:
replication slave 分配复制的权限
*.* 可以操作哪个数据库
'root' 登录的账户
'192.168.4.94' 可以在哪台机器上用 root账户登录
'123456' 密码
-
查看主服务器bin_log日志的信息(执行完之后记录下这两值,然后在配置完从服务器之前不要对主服务器进行任何操作,因为每次操作数据库时这两值会发生改变).
mysql> show master status \G;
+----------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+----------------+----------+--------------+------------------+-------------------+
| log-bin.000021 | 347| | | |
+----------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
-
设置从服务器
关闭slave(如果你以前配置过主从的话,一定要先关闭)
命令:stop slave;
开始配置:
输入下面代码即可:
mysql> Change master to
Master_host='192.168.7.195',
Master_port=3309,
Master_user='root',
Master_password='123456',
Master_log_file='log-bin.000002',
Master_log_pos=639;
Query OK, 0 rows affected, 2 warnings (0.05 sec)
参数解释:
MASTER_HOST : 设置要连接的主服务器的ip地址
MASTER_USER : 设置要连接的主服务器的用户名
MASTER_PASSWORD : 设置要连接的主服务器的密码
MASTER_LOG_FILE : 设置要连接的主服务器的bin_log的日志名称,即show master status;
MASTER_LOG_POS : 设置要连接的主服务器的bin_log的日志位置,即show master status;得到的信息
4.1 在从服务器配置完成,启动从服务器的slave进程。命令如下:
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
-
从库查看是否配置成功:
mysql> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Connecting to master
Master_Host: 192.168.7.195
Master_User: root
Master_Port: 3309
Connect_Retry: 60
Master_Log_File: log-bin
Read_Master_Log_Pos: 629
Relay_Log_File: think-PC-relay-bin.000006
Relay_Log_Pos: 4
Relay_Master_Log_File: log-bin
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: 154
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: 1130
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 0
Master_UUID: c7d073e0-acec-11e8-b705-0050568d4e69
Master_Info_File: C:\ProgramData\MySQL\MySQL Server 5.7\Data\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: 180910 11:18:46
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:
上面两项均为yes,说明配置成功,否则,请重复前面的步骤。
ok,到这里MySQL的主从复制就配置完了
创建时间:2021-12-22 09:31
넶浏览量:0