4、读写分离---双主双从(mycat)
双主双从
#主服务器唯一ID server-id=1 ##启用二进制日志 log-bin=mysql-bin ## 设置不要复制的数据库(可设置多个) binlog-ignore-db=mysql ##binlog-ignore-db=information_schema ##设置需要复制的数据库 binlog-do-db=testdb ##设置logbin格式 binlog_format=STATEMENT
新加配置
# 在作为从数据库的时候,有写入操作也要更新二进制日志文件 log-slave-updates #表示自增长字段每次递增的量,指自增字段的起始值,其默认值是1,取值范围是1 .. 65535 auto-increment-increment=2 ## 表示自增长字段从哪个数开始,指字段一次递增多少,他的取值范围是1 .. 65535 auto-increment-offset=1
master2配置
/etc/my.cnf
#主服务器唯一ID server-id=3 #启用二进制日志 log-bin=mysql-bin # 设置不要复制的数据库(可设置多个) binlog-ignore-db=mysql binlog-ignore-db=information_schema #设置需要复制的数据库 binlog-do-db=testdb ##设置logbin格式 binlog_format=STATEMENT ## 在作为从数据库的时候,有写入操作也要更新二进制日志文件 log-slave-updates ##表示自增长字段每次递增的量,指自增字段的起始值,其默认值是1,取值范围是1 .. 65535 auto-increment-increment=2 ## 表示自增长字段从哪个数开始,指字段一次递增多少,他的取值范围是1 .. 65535 auto-increment-offset=2
slave1配置
#从服务器唯一ID server-id=2 ##启用中继日志 relay-log=mysql-relay
slave2配置
#从服务器唯一ID server-id=4
#启用中继日志 relay-log=mysql-relay
①、配置好哦之后进行mysql服务的重启
②、关闭防火墙
#在主机MySQL里执行授权命令 GRANT REPLICATION SLAVE ON *.* TO 'slave'@'%' IDENTIFIED BY '123123';
查看主机master1状态
mysql> show master status -> ; +------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+-------------------+ | mysql-bin.000010 | 120 | testdb | mysql | | +------------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec)
mysql> show master status -> ; +------------------+----------+--------------+--------------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+--------------------------+-------------------+ | mysql-bin.000002 | 120 | testdb | mysql,information_schema | | +------------------+----------+--------------+--------------------------+-------------------+ 1 row in set (0.00 sec)
在两台主机创建slave
④、在从机上配置需要复制的主机
slave1·
mysql> CHANGE MASTER TO MASTER_HOST='192.168.199.231', -> MASTER_USER='slave', -> MASTER_PASSWORD='123123', -> MASTER_LOG_FILE='mysql-bin.000010',MASTER_LOG_POS=120; Query OK, 0 rows affected, 2 warnings (0.01 sec) mysql> start slave; Query OK, 0 rows affected (0.01 sec)
slave2
mysql> CHANGE MASTER TO MASTER_HOST='192.168.199.120',
-> MASTER_USER='slave',
-> MASTER_PASSWORD='123123',
-> MASTER_LOG_FILE='mysql-bin.000002',MASTER_LOG_POS=120;
Query OK, 0 rows affected, 2 warnings (0.03 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
此时master1和slave1,master2和slave2的主从复制搭建完成
master1执行(即连接master2的主机IP地址)
mysql> CHANGE MASTER TO MASTER_HOST='192.168.199.120',
-> MASTER_USER='slave',
-> MASTER_PASSWORD='123123',
-> MASTER_LOG_FILE='mysql-bin.000002',MASTER_LOG_POS=120;
Query OK, 0 rows affected, 2 warnings (0.05 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
master2执行(即连接master1的主机IP地址)
mysql> CHANGE MASTER TO MASTER_HOST='192.168.199.231', -> MASTER_USER='slave', -> MASTER_PASSWORD='123123', -> MASTER_LOG_FILE='mysql-bin.000010',MASTER_LOG_POS=120; Query OK, 0 rows affected, 2 warnings (0.07 sec) mysql> start slave; Query OK, 0 rows affected (0.00 sec)
⑥、 Master1 主机新建库、新建表、insert 记录,Master2 和从机复制
创建数据库
master1
mysql> create database testdb; Query OK, 1 row affected (0.00 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | metastore | | mysql | | performance_schema | | test | | testdb | +--------------------+ 6 rows in set (0.00 sec)
slave2
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | metastore | | mysql | | performance_schema | | test | | testdb | +--------------------+ 6 rows in set (0.00 sec)
master2
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | metastore | | mysql | | performance_schema | | test | | testdb | +--------------------+ 6 rows in set (0.01 sec)
slave2
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | metastore | | mysql | | performance_schema | | test | | testdb | +--------------------+ 6 rows in set (0.00 sec)
mysql> use testdb; Database changed
mysql> create table tbl(id int,name varchar(40)); Query OK, 0 rows affected (0.03 sec) mysql> insert into tbl values (1,'mrchengs'); Query OK, 1 row affected (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> select * from tbl; +------+----------+ | id | name | +------+----------+ | 1 | mrchengs | +------+----------+ 1 row in set (0.00 sec)
master2
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> select * from tbl; +------+----------+ | id | name | +------+----------+ | 1 | mrchengs | +------+----------+ 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> select * from tbl; +------+----------+ | id | name | +------+----------+ | 1 | mrchengs | +------+----------+ 1 row in set (0.00 sec)
mycat的使用
mysql> select * from tbl; +------+-----------------------+ | id | name | +------+-----------------------+ | 1 | mrchengs | | 3 | localhost.localdomain | +------+-----------------------+ 2 rows in set (0.01 sec)
slave2
mysql> select * from tbl; +------+----------+ | id | name | +------+----------+ | 1 | mrchengs | | 3 | mycat03 | +------+----------+ 2 rows in set (0.00 sec)
mysql> select * from tbl; +------+----------+ | id | name | +------+----------+ | 1 | mrchengs | | 3 | mycat04 | +------+----------+ 2 rows in set (0.00 sec)
slave2
mysql> select * from tbl; +------+----------+ | id | name | +------+----------+ | 1 | mrchengs | | 3 | mycat05 | +------+----------+ 2 rows in set (0.00 sec)
[root@localhost ~]# mysql -umycat -p123456 -P 8066 -h 192.168.199.217
进行验证读写分离
mysql> select * from tbl; +------+----------+ | id | name | +------+----------+ | 1 | mrchengs | | 3 | mycat04 | +------+----------+ 2 rows in set (0.00 sec) mysql> select * from tbl; +------+----------+ | id | name | +------+----------+ | 1 | mrchengs | | 3 | mycat03 | +------+----------+ 2 rows in set (0.00 sec) mysql> select * from tbl; +------+----------+ | id | name | +------+----------+ | 1 | mrchengs | | 3 | mycat05 | +------+----------+ 2 rows in set (0.07 sec)
[root@localhost ~]# systemctl stop mysql [root@localhost ~]# systemctl status mysql ● mysql.service - LSB: start and stop MySQL Loaded: loaded (/etc/rc.d/init.d/mysql; bad; vendor preset: disabled) Active: inactive (dead) since Thu 2020-02-13 20:15:06 CST; 8s ago Docs: man:systemd-sysv-generator(8) Process: 1632 ExecStop=/etc/rc.d/init.d/mysql stop (code=exited, status=0/SUCCESS) Process: 1070 ExecStart=/etc/rc.d/init.d/mysql start (code=exited, status=0/SUCCESS) Feb 13 19:18:32 localhost.localdomain systemd[1]: Starting LSB: start and stop MySQL... Feb 13 19:18:33 localhost.localdomain mysql[1070]: Starting MySQL SUCCESS! Feb 13 19:18:33 localhost.localdomain systemd[1]: Started LSB: start and stop MySQL. Feb 13 20:14:50 localhost.localdomain systemd[1]: Stopping LSB: start and stop MySQL... Feb 13 20:15:06 localhost.localdomain mysql[1632]: Shutting down MySQL.............. SUCCESS! Feb 13 20:15:06 localhost.localdomain systemd[1]: Stopped LSB: start and stop MySQL.
mysql> INSERT INTO tbl VALUES(100,@@hostname); Query OK, 1 row affected, 1 warning (0.15 sec)
mysql> select * from tbl; +------+-----------------------+ | id | name | +------+-----------------------+ | 1 | mrchengs | | 3 | localhost.localdomain | | 100 | localhost.localdomain | +------+-----------------------+ 3 rows in set (0.00 sec)
在mycat上进行查询
mysql> select * from tbl; +------+----------+ | id | name | +------+----------+ | 1 | mrchengs | | 3 | mycat05 | | 100 | mycat05 | +------+----------+ 3 rows in set (0.00 sec) mysql> select * from tbl; +------+-----------------------+ | id | name | +------+-----------------------+ | 1 | mrchengs | | 3 | localhost.localdomain | | 100 | localhost.localdomain | +------+-----------------------+ 3 rows in set (0.00 sec) mysql> select * from tbl; +------+----------+ | id | name | +------+----------+ | 1 | mrchengs | | 3 | mycat03 | | 100 | mycat03 | +------+----------+ 3 rows in set (0.00 sec)