Mysql双主双从(实战干货,好文章!!)

共 14309字,需浏览 29分钟

 ·

2021-04-27 23:35

1.机器说明



因为主2,从2,都是通过同一个镜像生成的系统,所以mysql的 server-uuid 是一样的,在配置前,需要做以下操作

重命名 /var/lib/mysql/auto.cnf 文件

mv auto.cnf auto.cnf.bak

重启mysql 重新生成 auto.cnf 文件





因为之前配置过 ali-shuidi01,ali-shuidi02的主从关系,所以先将两者之前的主从关系取消,在master1上执行

 
  1. mysql> stop slave;

  2. Query OK, 0 rows affected, 1 warning (0.00 sec)


  3. mysql> reset master;

  4. Query OK, 0 rows affected (0.01 sec)


2. master1 配置文件修改

vim /etc/my.cnf


 
  1. #主服务器唯一ID

  2. server-id=1

  3. #启用二进制日志

  4. log-bin=mysql-bin

  5. # 设置不要复制的数据库(可设置多个)

  6. binlog-ignore-db=mysql

  7. binlog-ignore-db=information_schema

  8. binlog-ignore-db=performance_schema

  9. binlog-ignore-db=sys

  10. #设置需要复制的数据库

  11. binlog-do-db=copydb2

  12. #设置logbin格式

  13. binlog_format=STATEMENT

  14. # 在作为从数据库的时候,有写入操作也要更新二进制日志文件

  15. log-slave-updates

  16. #表示自增长字段每次递增的量,指自增字段的起始值,其默认值是1,取值范围是1 .. 65535

  17. auto-increment-increment=2

  18. # 表示自增长字段从哪个数开始,指字段一次递增多少,他的取值范围是1 .. 65535

  19. auto-increment-offset=1


3. master2 配置文件修改

vim /etc/my.cnf


 
  1. #主服务器唯一ID

  2. server-id=3

  3. #启用二进制日志

  4. log-bin=mysql-bin

  5. # 设置不要复制的数据库(可设置多个)

  6. binlog-ignore-db=mysql

  7. binlog-ignore-db=information_schema

  8. binlog-ignore-db=performance_schema

  9. binlog-ignore-db=sys

  10. #设置需要复制的数据库

  11. binlog-do-db=copydb2

  12. #设置logbin格式

  13. binlog_format=STATEMENT

  14. # 在作为从数据库的时候,有写入操作也要更新二进制日志文件

  15. log-slave-updates

  16. #表示自增长字段每次递增的量,指自增字段的起始值,其默认值是1,取值范围是1 .. 65535

  17. auto-increment-increment=2

  18. # 表示自增长字段从哪个数开始,指字段一次递增多少,他的取值范围是1 .. 65535

  19. auto-increment-offset=2


4. slave1 配置文件修改

vim /etc/my.cnf


 
  1. #从服务器唯一ID

  2. server-id=2

  3. #启用中继日志

  4. relay-log=mysql-relay


5. slave2配置文件修改


vim /etc/my.cnf


 
  1. #从服务器唯一ID

  2. server-id=4 #启用中继日志

  3. relay-log=mysql-relay


6. 双主机、双从机重启 mysql 服务, 主机从机都关闭防火墙


 
  1. [root@shuidi01 etc]# systemctl restart mysqld

  2. [root@shuidi01 etc]# systemctl status mysqld

  3. ● mysqld.service - MySQL Server

  4. Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)

  5. Active: active (running) since 一 2020-03-30 20:38:23 CST; 6s ago

  6. Docs: man:mysqld(8)

  7. http://dev.mysql.com/doc/refman/en/using-systemd.html

  8. Process: 9985 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)

  9. Process: 9946 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)

  10. Main PID: 9990 (mysqld)

  11. CGroup: /system.slice/mysqld.service

  12. └─9990 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid


  13. 3月 30 20:38:22 shuidi01 systemd[1]: Starting MySQL Server...

  14. 3月 30 20:38:23 shuidi01 systemd[1]: Started MySQL Server.


 
  1. [root@shuidi02 etc]# systemctl restart mysqld

  2. [root@shuidi02 etc]# systemctl status mysqld

  3. ● mysqld.service - MySQL Server

  4. Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)

  5. Active: active (running) since 一 2020-03-30 20:39:10 CST; 7s ago

  6. Docs: man:mysqld(8)

  7. http://dev.mysql.com/doc/refman/en/using-systemd.html

  8. Process: 868 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)

  9. Process: 845 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)

  10. Main PID: 873 (mysqld)

  11. CGroup: /system.slice/mysqld.service

  12. └─873 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid


  13. 3月 30 20:39:09 shuidi02 systemd[1]: Starting MySQL Server...

  14. 3月 30 20:39:10 shuidi02 systemd[1]: Started MySQL Server.


 
  1. [root@shuidi03 etc]# systemctl restart mysqld

  2. [root@shuidi03 etc]# systemctl status mysqld

  3. ● mysqld.service - MySQL Server

  4. Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)

  5. Active: active (running) since 一 2020-03-30 20:39:42 CST; 5s ago

  6. Docs: man:mysqld(8)

  7. http://dev.mysql.com/doc/refman/en/using-systemd.html

  8. Process: 8507 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)

  9. Process: 8485 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)

  10. Main PID: 8512 (mysqld)

  11. CGroup: /system.slice/mysqld.service

  12. └─8512 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid


  13. 3月 30 20:39:41 shuidi03 systemd[1]: Starting MySQL Server...

  14. 3月 30 20:39:42 shuidi03 systemd[1]: Started MySQL Server.


 
  1. [root@shuidi04 etc]# systemctl restart mysqld

  2. [root@shuidi04 etc]# systemctl status mysqld

  3. ● mysqld.service - MySQL Server

  4. Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)

  5. Active: active (running) since 一 2020-03-30 20:40:13 CST; 8s ago

  6. Docs: man:mysqld(8)

  7. http://dev.mysql.com/doc/refman/en/using-systemd.html

  8. Process: 8861 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)

  9. Process: 8839 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)

  10. Main PID: 8866 (mysqld)

  11. CGroup: /system.slice/mysqld.service

  12. └─8866 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid


  13. 3月 30 20:40:12 shuidi04 systemd[1]: Starting MySQL Server...

  14. 3月 30 20:40:13 shuidi04 systemd[1]: Started MySQL Server.


7. 在两台主机上建立账户并授权 slave

master1:

 
  1. mysql> GRANT REPLICATION SLAVE ON *.* TO 'slave'@'%' IDENTIFIED BY 'Zan_666888';

  2. Query OK, 0 rows affected, 1 warning (0.00 sec)

  3. #查询Master1的状态

  4. mysql> show master status;

  5. +------------------+----------+--------------+-------------------------------------------------+-------------------+

  6. | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |

  7. +------------------+----------+--------------+-------------------------------------------------+-------------------+

  8. | mysql-bin.000002 | 435 | copydb2 | mysql,information_schema,performance_schema,sys | |

  9. +------------------+----------+--------------+-------------------------------------------------+-------------------+

  10. 1 row in set (0.00 sec)

master2:

 
  1. mysql> GRANT REPLICATION SLAVE ON *.* TO 'slave'@'%' IDENTIFIED BY 'Zan_666888';

  2. Query OK, 0 rows affected, 1 warning (0.01 sec)

  3. #查询Master2的状态

  4. mysql> show master status;

  5. +------------------+----------+--------------+-------------------------------------------------+-------------------+

  6. | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |

  7. +------------------+----------+--------------+-------------------------------------------------+-------------------+

  8. | mysql-bin.000001 | 435 | copydb2 | mysql,information_schema,performance_schema,sys | |

  9. +------------------+----------+--------------+-------------------------------------------------+-------------------+

  10. 1 row in set (0.00 sec)

#分别记录下File和Position的值

#执行完此步骤后不要再操作主服务器MYSQL,防止主服务器状态值变化


8. 在从机上配置需要复制的主机

Slava1 复制 Master1,Slava2 复制 Master2


 
  1. #复制主机的命令

  2. CHANGE MASTER TO MASTER_HOST='主机的IP地址',

  3. MASTER_USER='slave',

  4. MASTER_PASSWORD='123123',

  5. MASTER_LOG_FILE='mysql-bin.具体数字',MASTER_LOG_POS=具体值;

slave1:

 
  1. CHANGE MASTER TO MASTER_HOST='172.16.94.131',

  2. MASTER_USER='slave',

  3. MASTER_PASSWORD='Zan_666888',

  4. MASTER_LOG_FILE='mysql-bin.000002',MASTER_LOG_POS=435;

slave2:

 
  1. CHANGE MASTER TO MASTER_HOST='172.16.94.133',

  2. MASTER_USER='slave',

  3. MASTER_PASSWORD='Zan_666888',

  4. MASTER_LOG_FILE='mysql-bin.000001',MASTER_LOG_POS=435;

#启动两台从服务器复制功能

slave1:

 
  1. mysql> start slave;

  2. #查看从服务器状态

  3. show slave status\G;


slave2:

mysql> start slave; #查看从服务器状态 show slave status\G;


#下面两个参数都是Yes,则说明主从配置成功!

# Slave_IO_Running: Yes

# Slave_SQL_Running: Yes


9. 两个主机互相复制

Master2 复制 Master1,Master1 复制 Master2


master2:

 
  1. mysql> CHANGE MASTER TO MASTER_HOST='172.16.94.131',

  2. -> MASTER_USER='slave',

  3. -> MASTER_PASSWORD='Zan_666888',

  4. -> MASTER_LOG_FILE='mysql-bin.000002',MASTER_LOG_POS=435;

  5. Query OK, 0 rows affected, 2 warnings (0.02 sec)


  6. mysql> start slave;

  7. Query OK, 0 rows affected (0.00 sec)


  8. mysql> show slave status\G;



master1:


 
  1. mysql> CHANGE MASTER TO MASTER_HOST='172.16.94.133',

  2. -> MASTER_USER='slave',

  3. -> MASTER_PASSWORD='Zan_666888',

  4. -> MASTER_LOG_FILE='mysql-bin.000001',MASTER_LOG_POS=435;

  5. Query OK, 0 rows affected, 2 warnings (0.02 sec)


  6. mysql> start slave;

  7. Query OK, 0 rows affected (0.00 sec)


  8. mysql> show slave status\G;



#下面两个参数都是Yes,则说明主从配置成功!

# Slave_IO_Running: Yes

# Slave_SQL_Running: Yes


10. Master1 主机新建库、新建表、insert 记录,Master2 和从机复制

 
  1. mysql> CREATE DATABASE copydb2;

  2. Query OK, 1 row affected (0.00 sec)


  3. mysql> show databases;

  4. +--------------------+

  5. | Database |

  6. +--------------------+

  7. | information_schema |

  8. | copydb |

  9. | copydb2 |

  10. | mysql |

  11. | nacos_config |

  12. | performance_schema |

  13. | seata |

  14. | seata_account |

  15. | seata_order |

  16. | seata_storage |

  17. | sys |

  18. +--------------------+

  19. 11 rows in set (0.00 sec)


  20. mysql> use copydb2;

  21. Database changed

  22. mysql> CREATE TABLE mytbl(id INT,NAME VARCHAR(20));

  23. Query OK, 0 rows affected (0.01 sec)


  24. mysql> INSERT INTO mytbl VALUES(1,'zhang3');

  25. Query OK, 1 row affected (0.01 sec)


  26. mysql> select * from mytbl;

  27. +------+--------+

  28. | id | NAME |

  29. +------+--------+

  30. | 1 | zhang3 |

  31. +------+--------+

  32. 1 row in set (0.00 sec)



四个节点数据均实现了同步


11.如何停止从服务复制功能

stop slave;

如何重新配置主从

stop slave; reset master;


12.修改 Mycat 的配置文件
/usr/local/mycat/conf/schema.xml

修改<dataHost>的balance属性,通过此属性配置读写分离的类型


负载均衡类型,目前的取值有4 种:

(1)balance="0", 不开启读写分离机制,所有读操作都发送到当前可用的 writeHost 上。

(2)balance="1",全部的 readHost 与 stand by writeHost 参与 select 语句的负载均衡,简单地说,当双主双从

模式(M1->S1,M2->S2,并且 M1 与 M2 互为主备),正常情况下,M2,S1,S2 都参与 select 语句的负载均衡。

(3)balance="2",所有读操作都随机的在 writeHost、readhost 上分发。

(4)balance="3",所有读请求随机的分发到 readhost 执行,writerHost 不负担读压力


为了双主双从读写分离balance设置为1


 
  1. <?xml version="1.0"?>

  2. <!DOCTYPE mycat:schema SYSTEM "schema.dtd">

  3. <mycat:schema xmlns:mycat="http://io.mycat/">


  4. <schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100" dataNode="dn1">


  5. </schema>


  6. <dataNode name="dn1" dataHost="host1" database="copydb2" />


  7. <dataHost name="host1" maxCon="1000" minCon="10" balance="1"

  8. writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">

  9. <heartbeat>select user()</heartbeat>


  10. <writeHost host="hostM1" url="172.16.94.131:3306" user="root"

  11. password="Zan_123456">


  12. <readHost host="hostS1" url="172.16.94.132:3306" user="root" password="Zan_123456" />

  13. </writeHost>


  14. <writeHost host="hostM2" url="172.16.94.133:3306" user="root"

  15. password="Zan_123456">


  16. <readHost host="hostS2" url="172.16.94.134:3306" user="root" password="Zan_123456" />

  17. </writeHost>


  18. </dataHost>


  19. </mycat:schema>

#balance="1": 全部的readHost与stand by writeHost参与select语句的负载均衡。

#writeType="0": 所有写操作发送到配置的第一个writeHost,第一个挂了切到还生存的第二个

#writeType="1",所有写操作都随机的发送到配置的 writeHost,1.5 以后废弃不推荐

#writeHost,重新启动后以切换后的为准,切换记录在配置文件中:dnindex.properties 。

#switchType="1": 1 默认值,自动切换。

# -1 表示不自动切换

# 2 基于 MySQL 主从同步的状态决定是否切换。


13.启动 Mycat,验证读写分离

 
  1. [root@shuidi01 bin]# ./mycat stop

  2. Stopping Mycat-server...

  3. Mycat-server was not running.

  4. [root@shuidi01 bin]# ./mycat start

  5. Starting Mycat-server...



 
  1. #登录mycat

  2. mysql -umycat -pzan123456 -P 8066 -h 127.0.0.1

  3. #插入数据

  4. mysql> show databases;

  5. +----------+

  6. | DATABASE |

  7. +----------+

  8. | TESTDB |

  9. +----------+

  10. 1 row in set (0.00 sec)


  11. mysql> use TESTDB;

  12. Reading table information for completion of table and column names

  13. You can turn off this feature to get a quicker startup with -A


  14. Database changed

  15. mysql> show tables;

  16. +-------------------+

  17. | Tables_in_copydb2 |

  18. +-------------------+

  19. | mytbl |

  20. +-------------------+

  21. 1 row in set (0.00 sec)

  22. mysql> INSERT INTO mytbl VALUES(2,@@hostname);

  23. Query OK, 1 row affected, 1 warning (0.00 sec)


  24. mysql> select * from mytbl;

  25. +------+----------+

  26. | id | NAME |

  27. +------+----------+

  28. | 1 | zhang3 |

  29. | 2 | shuidi03 |

  30. +------+----------+

  31. 2 rows in set (0.00 sec)


  32. mysql> select * from mytbl;

  33. +------+----------+

  34. | id | NAME |

  35. +------+----------+

  36. | 1 | zhang3 |

  37. | 2 | shuidi02 |

  38. +------+----------+

  39. 2 rows in set (0.00 sec)

  40. mysql> select * from mytbl;

  41. +------+----------+

  42. | id | NAME |

  43. +------+----------+

  44. | 1 | zhang3 |

  45. | 2 | shuidi04 |

  46. +------+----------+

  47. 2 rows in set (0.00 sec)


#在Mycat里查询mytbl表,可以看到查询语句在Master2(shuidi03)、Slava1(shuidi02)、Slava2(shuidi04)主从三个主机间切换


14.抗风险能力

#停止数据库Master1

 
  1. [root@shuidi01 logs]# systemctl stop mysqld

  2. [root@shuidi01 logs]# systemctl status mysqld

  3. ● mysqld.service - MySQL Server

  4. Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)

  5. Active: inactive (dead) since 一 2020-03-30 21:31:28 CST; 4s ago

  6. Docs: man:mysqld(8)

  7. http://dev.mysql.com/doc/refman/en/using-systemd.html

  8. Process: 9985 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)

  9. Process: 9946 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)

  10. Main PID: 9990 (code=exited, status=0/SUCCESS)


  11. 3月 30 20:38:22 shuidi01 systemd[1]: Starting MySQL Server...

  12. 3月 30 20:38:23 shuidi01 systemd[1]: Started MySQL Server.

  13. 3月 30 21:31:16 shuidi01 systemd[1]: Stopping MySQL Server...

  14. 3月 30 21:31:28 shuidi01 systemd[1]: Stopped MySQL Server.


#在Mycat里插入数据依然成功,Master2自动切换为写主机

 
  1. mysql> INSERT INTO mytbl VALUES(3,@@hostname);

  2. Query OK, 1 row affected, 1 warning (0.00 sec)

  3. mysql> select * from mytbl;

  4. +------+----------+

  5. | id | NAME |

  6. +------+----------+

  7. | 1 | zhang3 |

  8. | 2 | shuidi04 |

  9. | 3 | shuidi04 |

  10. +------+----------+

  11. 3 rows in set (0.00 sec)


#启动数据库Master1

 
  1. [root@shuidi01 logs]# systemctl start mysqld

  2. [root@shuidi01 logs]# systemctl status mysqld

  3. ● mysqld.service - MySQL Server

  4. Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)

  5. Active: active (running) since 一 2020-03-30 21:33:39 CST; 2s ago

  6. Docs: man:mysqld(8)

  7. http://dev.mysql.com/doc/refman/en/using-systemd.html

  8. Process: 3527 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)

  9. Process: 3473 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)

  10. Main PID: 3531 (mysqld)

  11. CGroup: /system.slice/mysqld.service

  12. └─3531 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid


  13. 3月 30 21:33:38 shuidi01 systemd[1]: Starting MySQL Server...

  14. 3月 30 21:33:39 shuidi01 systemd[1]: Started MySQL Server.


 
  1. mysql> select * from mytbl;

  2. +------+----------+

  3. | id | NAME |

  4. +------+----------+

  5. | 1 | zhang3 |

  6. | 2 | shuidi01 |

  7. | 3 | shuidi01 |

  8. +------+----------+

  9. 3 rows in set (0.00 sec)

  10. mysql> select * from mytbl;

  11. +------+----------+

  12. | id | NAME |

  13. +------+----------+

  14. | 1 | zhang3 |

  15. | 2 | shuidi02 |

  16. | 3 | shuidi02 |

  17. +------+----------+

  18. 3 rows in set (0.00 sec)

  19. mysql> select * from mytbl;

  20. +------+----------+

  21. | id | NAME |

  22. +------+----------+

  23. | 1 | zhang3 |

  24. | 2 | shuidi04 |

  25. | 3 | shuidi04 |

  26. +------+----------+

  27. 3 rows in set (0.00 sec)


#在Mycat里查询mytbl表,可以看到查询语句在Master1(shuidi01)、Slava1(shuidi02)、Slava2(shuidi04)

主从三个主机间切换

Master1、Master2 互做备机,负责写的主机宕机,备机切换负责写操作,保证数据库读写分离高

可用性。


浏览 17
点赞
评论
收藏
分享

手机扫一扫分享

分享
举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

分享
举报