Note: mysql master-slave replication only executes the content after access. That is, the main database and tables must be created after access.
1. Host configuration (master131)
配置文件:vim /etc/my.cnf
#主服务器唯一ID
server-id=1
#启用二进制日志
log-bin=mysql-bin
#设置不要复制的数据库(可设置多个,设置了需要复制的数据库就可以不设置此项)
binlog-ignore-db=mysql
#设置需要复制的数据库
binlog-do-db=需要复制的主数据库名字
#设置logbin格式
binlog_format=STATEMENT
Expansion:
Three formats of binlog log
1. Statement: Every sql that will modify data will be recorded in binlog
2. Row: does not record the context-related information of the SQL statement, only save which record is modified
3. Mixedlevel: Mixed use of the above two. General statement modification uses statment format to save binlog, such as some functions, statement cannot complete the operation of master-slave replication, then save binlog in row format
2. Slave configuration (master135)
配置文件: vim /etc/my.cnf
#从服务器唯一ID
server-id=2
#启用中继日志
relay-log=mysql-relay
Three, the host and slave restart the mysql service
systemctl restart mysqld
Four, both the host and the slave close the firewall
systemctl stop firewalld
Five, create an account on the host and authorize the slave
grant replication slave on *.* to 'slave'@'%'identified by '123456';
Error:
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
Reason: The password setting is too simple to conform to the mysql password setting specification
Solution:
1. View the complete password rules of mysql
show variables like 'validate_password%';
2. Change the password length
set global validate_password_length=4;
3. Set password verification strategy (medium[medium], low[low])
set global validate_password_policy=0;
Six, query master status
show master status;
Note: record the value of file and position. After performing this step, no longer operate the host mysql to prevent the host state value from changing.
Seven, configure the host to be copied on the slave
#复制主机的命令
change master to master_host='主机ip',
master_user='slave',
master_password='123456',
master_log_file='mysql-bin.具体数字',master_log_pos=具体值;
#启动从机复制功能
start slave;
#查看从机状态
show slave status\G;
Error:
ERROR 3021(HY000): This operation cannot be performed with a running slave io thread; run STOP SLAVE IO_THREAD FOR CHANNEL ' ' first.
Reason: If the slave is connected to the master, it needs to be reset
Solution:
stop slave;
reset master;
8. Verification
At this point, the master-slave replication of mysql is complete, we can verify it
1. Create a testdb database on the host and check whether the database is created successfully
create database testdb;
show databases;
2. The creation is completed on the host, let's go and take a look on the computer
Expansion:
1. Stop copying functions from the service
stop slave;
2. Reconfigure the master and slave
stop slave;
reset master;
3. Check if read and write separation
#主机写入 @@环境变量
insert into user values (1,@@hostname);
#在mycat里查询
select * from user;
We see that the content written on the host and the slave is the same, so it proves that the separation of read and write is not achieved.
Read and write separation settings
vim /usr/local/mycal/conf/schema.xml
#修改<dataHost>的 balance属性为3
<?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="testdb" />
<dataHost name="host1" maxCon="1000" minCon="10" balance="3"
writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<heartbeat>select user()</heartbeat>
<writeHost host="hostM1" url="192.168.157.133:3306" user="root"
password="root">
<readHost host="hostS1" url="192.168.157.135:3306" user="root" password="root"/>
</writeHost>
</dataHost>
</mycat:schema>
The balance attribute in <dataHost>: [load balancing type]
1), balance="0", do not open the read-write separation mechanism, all read operations are sent to the currently available writeHost.
2), balance="1", all readHost and stand by writeHost participate in the load balancing of the select statement, in simple terms, when the dual master dual slave mode (M1->S1, M2->S2, and M1 and M2 are each other Active and standby), under normal circumstances, M2, S1, S2 all participate in the load balancing of the select statement.
3), balance="2", all read operations are randomly distributed on writeHost and readhost.
4), balance="3", all read requests are randomly distributed to readhost for execution, writerHost does not bear the read pressure.
Ok, restart the mycat service and verify it
#重启mycat服务
mycat console
#在mycat里的mysql里
use TESTDB;
SELECT * FROM USER;
We see that read and write separation has been achieved