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上执行
- mysql> stop slave;
- Query OK, 0 rows affected, 1 warning (0.00 sec)
-
- mysql> reset master;
- Query OK, 0 rows affected (0.01 sec)
2. master1 配置文件修改
vim /etc/my.cnf
- #主服务器唯一ID
- server-id=1
- #启用二进制日志
- log-bin=mysql-bin
- # 设置不要复制的数据库(可设置多个)
- binlog-ignore-db=mysql
- binlog-ignore-db=information_schema
- binlog-ignore-db=performance_schema
- binlog-ignore-db=sys
- #设置需要复制的数据库
- binlog-do-db=copydb2
- #设置logbin格式
- binlog_format=STATEMENT
- # 在作为从数据库的时候,有写入操作也要更新二进制日志文件
- log-slave-updates
- #表示自增长字段每次递增的量,指自增字段的起始值,其默认值是1,取值范围是1 .. 65535
- auto-increment-increment=2
- # 表示自增长字段从哪个数开始,指字段一次递增多少,他的取值范围是1 .. 65535
- auto-increment-offset=1
3. master2 配置文件修改
vim /etc/my.cnf
- #主服务器唯一ID
- server-id=3
- #启用二进制日志
- log-bin=mysql-bin
- # 设置不要复制的数据库(可设置多个)
- binlog-ignore-db=mysql
- binlog-ignore-db=information_schema
- binlog-ignore-db=performance_schema
- binlog-ignore-db=sys
- #设置需要复制的数据库
- binlog-do-db=copydb2
- #设置logbin格式
- binlog_format=STATEMENT
- # 在作为从数据库的时候,有写入操作也要更新二进制日志文件
- log-slave-updates
- #表示自增长字段每次递增的量,指自增字段的起始值,其默认值是1,取值范围是1 .. 65535
- auto-increment-increment=2
- # 表示自增长字段从哪个数开始,指字段一次递增多少,他的取值范围是1 .. 65535
- auto-increment-offset=2
4. slave1 配置文件修改
vim /etc/my.cnf
- #从服务器唯一ID
- server-id=2
- #启用中继日志
- relay-log=mysql-relay
5. slave2配置文件修改
vim /etc/my.cnf
- #从服务器唯一ID
- server-id=4 #启用中继日志
- relay-log=mysql-relay
6. 双主机、双从机重启 mysql 服务, 主机从机都关闭防火墙
- [root@shuidi01 etc]# systemctl restart mysqld
- [root@shuidi01 etc]# systemctl status mysqld
- ● mysqld.service - MySQL Server
- Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
- Active: active (running) since 一 2020-03-30 20:38:23 CST; 6s ago
- Docs: man:mysqld(8)
- http://dev.mysql.com/doc/refman/en/using-systemd.html
- Process: 9985 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
- Process: 9946 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
- Main PID: 9990 (mysqld)
- CGroup: /system.slice/mysqld.service
- └─9990 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
-
- 3月 30 20:38:22 shuidi01 systemd[1]: Starting MySQL Server...
- 3月 30 20:38:23 shuidi01 systemd[1]: Started MySQL Server.
- [root@shuidi02 etc]# systemctl restart mysqld
- [root@shuidi02 etc]# systemctl status mysqld
- ● mysqld.service - MySQL Server
- Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
- Active: active (running) since 一 2020-03-30 20:39:10 CST; 7s ago
- Docs: man:mysqld(8)
- http://dev.mysql.com/doc/refman/en/using-systemd.html
- Process: 868 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
- Process: 845 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
- Main PID: 873 (mysqld)
- CGroup: /system.slice/mysqld.service
- └─873 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
-
- 3月 30 20:39:09 shuidi02 systemd[1]: Starting MySQL Server...
- 3月 30 20:39:10 shuidi02 systemd[1]: Started MySQL Server.
- [root@shuidi03 etc]# systemctl restart mysqld
- [root@shuidi03 etc]# systemctl status mysqld
- ● mysqld.service - MySQL Server
- Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
- Active: active (running) since 一 2020-03-30 20:39:42 CST; 5s ago
- Docs: man:mysqld(8)
- http://dev.mysql.com/doc/refman/en/using-systemd.html
- Process: 8507 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
- Process: 8485 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
- Main PID: 8512 (mysqld)
- CGroup: /system.slice/mysqld.service
- └─8512 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
-
- 3月 30 20:39:41 shuidi03 systemd[1]: Starting MySQL Server...
- 3月 30 20:39:42 shuidi03 systemd[1]: Started MySQL Server.
- [root@shuidi04 etc]# systemctl restart mysqld
- [root@shuidi04 etc]# systemctl status mysqld
- ● mysqld.service - MySQL Server
- Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
- Active: active (running) since 一 2020-03-30 20:40:13 CST; 8s ago
- Docs: man:mysqld(8)
- http://dev.mysql.com/doc/refman/en/using-systemd.html
- Process: 8861 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
- Process: 8839 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
- Main PID: 8866 (mysqld)
- CGroup: /system.slice/mysqld.service
- └─8866 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
-
- 3月 30 20:40:12 shuidi04 systemd[1]: Starting MySQL Server...
- 3月 30 20:40:13 shuidi04 systemd[1]: Started MySQL Server.
7. 在两台主机上建立账户并授权 slave
master1:
- mysql> GRANT REPLICATION SLAVE ON *.* TO 'slave'@'%' IDENTIFIED BY 'Zan_666888';
- Query OK, 0 rows affected, 1 warning (0.00 sec)
- #查询Master1的状态
- mysql> show master status;
- +------------------+----------+--------------+-------------------------------------------------+-------------------+
- | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
- +------------------+----------+--------------+-------------------------------------------------+-------------------+
- | mysql-bin.000002 | 435 | copydb2 | mysql,information_schema,performance_schema,sys | |
- +------------------+----------+--------------+-------------------------------------------------+-------------------+
- 1 row in set (0.00 sec)
master2:
- mysql> GRANT REPLICATION SLAVE ON *.* TO 'slave'@'%' IDENTIFIED BY 'Zan_666888';
- Query OK, 0 rows affected, 1 warning (0.01 sec)
- #查询Master2的状态
- mysql> show master status;
- +------------------+----------+--------------+-------------------------------------------------+-------------------+
- | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
- +------------------+----------+--------------+-------------------------------------------------+-------------------+
- | mysql-bin.000001 | 435 | copydb2 | mysql,information_schema,performance_schema,sys | |
- +------------------+----------+--------------+-------------------------------------------------+-------------------+
- 1 row in set (0.00 sec)
#分别记录下File和Position的值
#执行完此步骤后不要再操作主服务器MYSQL,防止主服务器状态值变化
8. 在从机上配置需要复制的主机
Slava1 复制 Master1,Slava2 复制 Master2
- #复制主机的命令
- CHANGE MASTER TO MASTER_HOST='主机的IP地址',
- MASTER_USER='slave',
- MASTER_PASSword='123123',
- MASTER_LOG_FILE='mysql-bin.具体数字',MASTER_LOG_POS=具体值;
slave1:
- CHANGE MASTER TO MASTER_HOST='172.16.94.131',
- MASTER_USER='slave',
- MASTER_PASSWORD='Zan_666888',
- MASTER_LOG_FILE='mysql-bin.000002',MASTER_LOG_POS=435;
slave2:
- CHANGE MASTER TO MASTER_HOST='172.16.94.133',
- MASTER_USER='slave',
- MASTER_PASSWORD='Zan_666888',
- MASTER_LOG_FILE='mysql-bin.000001',MASTER_LOG_POS=435;
#启动两台从服务器复制功能
slave1:
- mysql> start slave;
- #查看从服务器状态
- show slave statusG;
slave2:
mysql> start slave; #查看从服务器状态 show slave statusG;
#下面两个参数都是Yes,则说明主从配置成功!
# Slave_IO_Running: Yes
# Slave_SQL_Running: Yes
9. 两个主机互相复制
Master2 复制 Master1,Master1 复制 Master2
master2:
- mysql> CHANGE MASTER TO MASTER_HOST='172.16.94.131',
- -> MASTER_USER='slave',
- -> MASTER_PASSWORD='Zan_666888',
- -> MASTER_LOG_FILE='mysql-bin.000002',MASTER_LOG_POS=435;
- Query OK, 0 rows affected, 2 warnings (0.02 sec)
-
- mysql> start slave;
- Query OK, 0 rows affected (0.00 sec)
-
- mysql> show slave statusG;
master1:
- mysql> CHANGE MASTER TO MASTER_HOST='172.16.94.133',
- -> MASTER_USER='slave',
- -> MASTER_PASSWORD='Zan_666888',
- -> MASTER_LOG_FILE='mysql-bin.000001',MASTER_LOG_POS=435;
- Query OK, 0 rows affected, 2 warnings (0.02 sec)
-
- mysql> start slave;
- Query OK, 0 rows affected (0.00 sec)
-
- mysql> show slave statusG;
#下面两个参数都是Yes,则说明主从配置成功!
# Slave_IO_Running: Yes
# Slave_SQL_Running: Yes
10. Master1 主机新建库、新建表、insert 记录,Master2 和从机复制
- mysql> CREATE DATABASE copydb2;
- Query OK, 1 row affected (0.00 sec)
-
- mysql> show databases;
- +--------------------+
- | Database |
- +--------------------+
- | information_schema |
- | copydb |
- | copydb2 |
- | mysql |
- | nacos_config |
- | performance_schema |
- | seata |
- | seata_account |
- | seata_order |
- | seata_storage |
- | sys |
- +--------------------+
- 11 rows in set (0.00 sec)
-
- mysql> use copydb2;
- Database changed
- mysql> CREATE TABLE mytbl(id INT,NAME VARCHAR(20));
- Query OK, 0 rows affected (0.01 sec)
-
- mysql> INSERT INTO mytbl VALUES(1,'zhang3');
- Query OK, 1 row affected (0.01 sec)
-
- mysql> select * from mytbl;
- +------+--------+
- | id | NAME |
- +------+--------+
- | 1 | zhang3 |
- +------+--------+
- 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
- <?xml version="1.0"?>
- <!DOCTYPE mycat:schema SYSTEM "schema.dtd">
- <mycat:schema xmlns:mycat="http://io.mycat/">
-
- <schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100" dataNode="dn1">
-
- </schema>
-
- <dataNode name="dn1" dataHost="host1" database="copydb2" />
-
- <dataHost name="host1" maxCon="1000" minCon="10" balance="1"
- writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
- <heartbeat>select user()</heartbeat>
-
- <writeHost host="hostM1" url="172.16.94.131:3306" user="root"
- password="Zan_123456">
-
- <readHost host="hostS1" url="172.16.94.132:3306" user="root" password="Zan_123456" />
- </writeHost>
-
- <writeHost host="hostM2" url="172.16.94.133:3306" user="root"
- password="Zan_123456">
-
- <readHost host="hostS2" url="172.16.94.134:3306" user="root" password="Zan_123456" />
- </writeHost>
-
- </dataHost>
-
- </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,验证读写分离
- [root@shuidi01 bin]# ./mycat stop
- Stopping Mycat-server...
- Mycat-server was not running.
- [root@shuidi01 bin]# ./mycat start
- Starting Mycat-server...
- #登录mycat
- mysql -umycat -pzan123456 -P 8066 -h 127.0.0.1
- #插入数据
- mysql> show databases;
- +----------+
- | DATABASE |
- +----------+
- | TESTDB |
- +----------+
- 1 row in set (0.00 sec)
-
- mysql> use TESTDB;
- 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> show tables;
- +-------------------+
- | Tables_in_copydb2 |
- +-------------------+
- | mytbl |
- +-------------------+
- 1 row in set (0.00 sec)
- mysql> INSERT INTO mytbl VALUES(2,@@hostname);
- Query OK, 1 row affected, 1 warning (0.00 sec)
-
- mysql> select * from mytbl;
- +------+----------+
- | id | NAME |
- +------+----------+
- | 1 | zhang3 |
- | 2 | shuidi03 |
- +------+----------+
- 2 rows in set (0.00 sec)
-
- mysql> select * from mytbl;
- +------+----------+
- | id | NAME |
- +------+----------+
- | 1 | zhang3 |
- | 2 | shuidi02 |
- +------+----------+
- 2 rows in set (0.00 sec)
- mysql> select * from mytbl;
- +------+----------+
- | id | NAME |
- +------+----------+
- | 1 | zhang3 |
- | 2 | shuidi04 |
- +------+----------+
- 2 rows in set (0.00 sec)
#在Mycat里查询mytbl表,可以看到查询语句在Master2(shuidi03)、Slava1(shuidi02)、Slava2(shuidi04)主从三个主机间切换
14.抗风险能力
#停止数据库Master1
- [root@shuidi01 logs]# systemctl stop mysqld
- [root@shuidi01 logs]# systemctl status mysqld
- ● mysqld.service - MySQL Server
- Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
- Active: inactive (dead) since 一 2020-03-30 21:31:28 CST; 4s ago
- Docs: man:mysqld(8)
- http://dev.mysql.com/doc/refman/en/using-systemd.html
- Process: 9985 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
- Process: 9946 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
- Main PID: 9990 (code=exited, status=0/SUCCESS)
-
- 3月 30 20:38:22 shuidi01 systemd[1]: Starting MySQL Server...
- 3月 30 20:38:23 shuidi01 systemd[1]: Started MySQL Server.
- 3月 30 21:31:16 shuidi01 systemd[1]: Stopping MySQL Server...
- 3月 30 21:31:28 shuidi01 systemd[1]: Stopped MySQL Server.
#在Mycat里插入数据依然成功,Master2自动切换为写主机
- mysql> INSERT INTO mytbl VALUES(3,@@hostname);
- Query OK, 1 row affected, 1 warning (0.00 sec)
- mysql> select * from mytbl;
- +------+----------+
- | id | NAME |
- +------+----------+
- | 1 | zhang3 |
- | 2 | shuidi04 |
- | 3 | shuidi04 |
- +------+----------+
- 3 rows in set (0.00 sec)
#启动数据库Master1
- [root@shuidi01 logs]# systemctl start mysqld
- [root@shuidi01 logs]# systemctl status mysqld
- ● mysqld.service - MySQL Server
- Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
- Active: active (running) since 一 2020-03-30 21:33:39 CST; 2s ago
- Docs: man:mysqld(8)
- http://dev.mysql.com/doc/refman/en/using-systemd.html
- Process: 3527 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
- Process: 3473 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
- Main PID: 3531 (mysqld)
- CGroup: /system.slice/mysqld.service
- └─3531 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
-
- 3月 30 21:33:38 shuidi01 systemd[1]: Starting MySQL Server...
- 3月 30 21:33:39 shuidi01 systemd[1]: Started MySQL Server.
- mysql> select * from mytbl;
- +------+----------+
- | id | NAME |
- +------+----------+
- | 1 | zhang3 |
- | 2 | shuidi01 |
- | 3 | shuidi01 |
- +------+----------+
- 3 rows in set (0.00 sec)
- mysql> select * from mytbl;
- +------+----------+
- | id | NAME |
- +------+----------+
- | 1 | zhang3 |
- | 2 | shuidi02 |
- | 3 | shuidi02 |
- +------+----------+
- 3 rows in set (0.00 sec)
- mysql> select * from mytbl;
- +------+----------+
- | id | NAME |
- +------+----------+
- | 1 | zhang3 |
- | 2 | shuidi04 |
- | 3 | shuidi04 |
- +------+----------+
- 3 rows in set (0.00 sec)
#在Mycat里查询mytbl表,可以看到查询语句在Master1(shuidi01)、Slava1(shuidi02)、Slava2(shuidi04)
主从三个主机间切换
Master1、Master2 互做备机,负责写的主机宕机,备机切换负责写操作,保证数据库读写分离高
可用性。
声明:本站部分内容及图片来自互联网,转载是出于传递更多信息之目的,内容观点仅代表作者本人,如有任何标注错误或版权侵犯请与我们联系(Email:2595517585@qq.com),我们将及时更正、删除,谢谢。