mysql主从复制配置
Mysql实现主从配置和多主多从配置我们现在模拟的是主从(1台主机、一台从机),其主从同步的原理,就是对bin-log二进制文件的同步,将这个文件的内容从主机同步到从机。
一、配置文件的修改1、主机配置文件修改配置
我们首先需要mysql主机(192.168.254.130)的/etc/my.cnf配置文件,添加如下配置:
|
#主机唯一ID server-id=1 #二进制日志 log-bin=mysql-bin #不需要同步的数据库 binlog-ignore-db=mysql binlog-ignore-db=information_schema #同步的数据库名称 binlog-do-db=mycat #二进制的格式 binlog_format=STATEMENT |
我们看下目前整个my.cnf文件
|
[root@localhost Desktop] # cat /etc/my.cnf [mysqld] datadir= /usr/local/mysql/data basedir= /usr/local/mysql socket= /usr/local/mysql/data/mysql .sock user=mysql # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 server- id =1 log-bin=mysql-bin binlog-ignore-db=mysql binlog-ignore-db=information_schema binlog- do -db=mycat binlog_format=STATEMENT [mysqld_safe] log-error= /usr/local/mysql/data/mysqld .log pid- file = /usr/local/mysql/data/mysqld/mysqld .pid [root@localhost Desktop] # |
修改主机的配置文件后,我们需要通过命令重启下服务:
|
[root@localhost support-files] # ls magic mysqld_multi.server mysql-log-rotate mysql.server [root@localhost support-files] # pwd /usr/local/mysql/support-files [root@localhost support-files] # ./mysql.server restart |
然后我们修改下从机(192.168.254.131)的配置文件。
2、从机的配置
从机的配置修改比较简单:
|
#从机机器唯一ID server-id=2 #中继日志 relay-log=mysql-relay |
同样修改配置后,我们重启下从机
二、mysql客户端命令操作下面我们可以通过命令连接到mysql的命令端:
|
[root@localhost bin] # [root@localhost bin] # pwd /usr/local/mysql/bin [root@localhost bin] # ./mysql -uroot -p |
1、主机操作
1)、创建同步用户
首先我们可以在主机创建一个专门用于主从同步用户,通过命令:
|
GRANT REPLICATION SLAVE ON *.* TO 'SLAVE' @ '%' IDENTIFIED BY '123456' ; |
2)、查看同步文件状态
然后我们通过show master status;查看主机的同步内容状态:
|
mysql> show master status; + ------------------+----------+--------------+--------------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | + ------------------+----------+--------------+--------------------------+-------------------+ | mysql-bin.000001 | 154 | mycat | mysql,information_schema | | + ------------------+----------+--------------+--------------------------+-------------------+ 1 row in set (0.00 sec) |
2、从机操作
1)、设置从机的主机
执行如下命令,这里设置了我们与主机建立同步的相关信息
|
CHANGE MASTER TO MASTER_HOST= '192.168.254.130' , MASTER_USER= 'SLAVE' , MASTER_PASSWORD= '123456' , MASTER_LOG_FILE= 'mysql-bin.000001' , MASTER_LOG_POS=430; |
这里如果提示已经设置了主机配置,可以通过stop slave&reset master进行重置。
2)、启动同步
下面我们再通过start slave开启同步:
就可以看到:
|
mysql> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.254.130 Master_User: SLAVE Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 592 Relay_Log_File: mysql-relay.000002 Relay_Log_Pos: 482 Relay_Master_Log_File: mysql-bin.000001 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: 592 Relay_Log_Space: 685 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: 0 Last_IO_Error: Last_SQL_Errno: 0 Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 1 Master_UUID: 74397a99-accf-11eb-ae0d-000c2912d302 Master_Info_File: /usr/ local /mysql/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: 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: 1 row in set (0.00 sec) mysql> |
这里我们可以看到Slave_IO_Running、Slave_SQL_Running都为YES,则成功了,如果是下面这种:
|
*************************** 1. row *************************** Slave_IO_State: Connecting to master Master_Host: 192.168.254.130 Master_User: slave Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 430 Relay_Log_File: mysql-relay.000001 Relay_Log_Pos: 4 Relay_Master_Log_File: mysql-bin.000001 Slave_IO_Running: Connecting 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: 430 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: NULL Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 1045 Last_IO_Error: error connecting to master 'slave@192.168.254.130:3306' - retry- time : 60 retries: 1 Last_SQL_Errno: 0 Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 0 Master_UUID: Master_Info_File: /usr/ local /mysql/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: 210505 00:18:08 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: 1 row in set (0.00 sec) |
我们可以看到Last_IO_Error这里有错误,我们就可以去看下日志报的什么问题了,目前我这个是因为同步用户写错了才不能同步,按上面说的先停止同步重置,修改后同步命令,再操作一遍就可以了。
三、主从同步测试1、主机创建库
我们先在主机创建我们前面设置的要同步的数据库mycat:
|
mysql> create database mycat; Query OK, 1 row affected (0.00 sec) mysql> use mycat; Database changed mysql> show databases; + --------------------+ | Database | + --------------------+ | information_schema | | mycat | | mysql | | performance_schema | | sys | + --------------------+ 5 rows in set (0.00 sec) mysql> |
2、从机查看库
然后我们就能在从机看到这个库了
|
mysql> show databases; + --------------------+ | Database | + --------------------+ | information_schema | | mycat | | mysql | | performance_schema | | sys | + --------------------+ 5 rows in set (0.00 sec) |
3、表数据的初始
下面我们进行表数据的测试
1)、主机
首先我们再主机建立表并插入数据