MYSQL 主从搭建详细步骤和测试

MySQL主备搭建

1.主库配置

以下所有操作均在主服务器上执行

1)创建用户并授权

create user slave identified with mysql_native_password by '123456'

mysql>GRANT REPLICATION SLAVE ON *.* to 'slave'@'%' identified by '123456';

mysql>FLUSH PRIVILEGES;

2)修改主库配置文件

开启binlog,并设置server-id,每次修改配置文件后都要重启mysql服务才会生效

vim /etc/my.cnf

#同步的日志路径及文件名,一定注意这个目录要是mysql有权限写入的

log-bin=/var/lib/mysql/binlog

#master端的id号,不唯一即可

server-id=1

#指定同步的数据库,如果不写,默认是同步所有数据库

binlog-do-db = 数据库

#指定不同步的数据库

binlog-ignore-db=mysql

#binlog日志的保留时间

expire_logs_days=7

#指定binlog日志的格式

binlog_format=ROW

改配置文件后,重启服务:

service mysqld restart

3)查看主服务器当前二进制日志名和偏移量

mysql> show master status;

+------------------+----------+--------------+------------------+-------------------+

| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |

+------------------+----------+--------------+------------------+-------------------+

| mysql-bin.000014 | 154 | | | |

+------------------+----------+--------------+------------------+-------------------+

1 row in set (0.00 sec)

到此主服务器已经配置好:

2.从库配置

以下所有操作均在从服务器上执行

1)修改从库配置文件

vim /etc/my.cnf

#master端的id号,不唯一即可

server-id=99

#同步的日志路径及文件名,一定注意这个目录要是mysql有权限写入的

log-bin=mysql-bin

#复制哪个库可以不用填写

replicate-do-db=webkit

replicate-do-db=dgp

replicate-do-db=dses_etldb

replicate-do-db=dses_metadb

replicate-do-db=dcf

replicate-do-db=etl

replicate-do-db=metadata

#不复制哪个库

replicate-ignore-db=mysql,information_schema,performance_schema

#这两个是启用relaylog的自动修复功能,避免由于网络之类的外因造成日志损坏,主从停止,保证数据写入的一致性

master_info_repository=table

relay_log_info_repository=table

改配置文件后,重启服务:

service mysqld restart

root@localhost:(none)\]\>CHANGE MASTER TO MASTER_HOST='10.153.119.5', -\> MASTER_PORT=3306, -\> MASTER_USER='slave', -\> MASTER_PASSWORD='123456', -\> MASTER_LOG_FILE='mysql-bin.000014', -\> MASTER_LOG_POS=154; Query OK, 0 rows affected, 2 warnings (0.03 sec) 注:MASTER_LOG_FILE和MASTER_LOG_POS的值为master节点中执行show master status查询到的信息 2)启动slave进程 mysql\> start slave; Query OK, 0 rows affected (0.04 sec) 3)查看slave状态 mysql\> show slave status\\G;(后面的分号不去的会报错,但无影响) 如果下面两项值为YES,则表示配置正确: Slave_IO_Running: No Slave_SQL_Running: Yes \[root@localhost:mytest\]\>show slave status\\G; \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* 1. row \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* Slave_IO_State: Master_Host: 10.153.119.5 Master_User: slave Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000014 Read_Master_Log_Pos: 154 Relay_Log_File: t3-dtpoc-dtpoc-web05-relay-bin.000001 Relay_Log_Pos: 4 Relay_Master_Log_File: mysql-bin.000014 Slave_IO_Running: No Slave_SQL_Running: Yes Replicate_Do_DB: Replicate_Ignore_DB: mysql,information_schema,performance_schema 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: 154 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: 1593 Last_IO_Error: Fatal error: The slave I/O thread stops because master and slave have equal MySQL server UUIDs; these UUIDs must be different for replication to work. Last_SQL_Errno: 0 Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 1 Master_UUID: Master_Info_File: mysql.slave_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: 230911 11:05:41 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) 我们可以看到如下报错: 注意:1、主从的server_id 一定不能重复; 2、主从的uuid 不能重复;SELECT UUID()/show variables like '%uuid'; 如果重复可以在/opt/module/mysql8/datas/mysql/atuo.cnf 删除或者修改重启服务; Last_IO_Errno: 1593 Last_IO_Error: Fatal error: The slave I/O thread stops because master and slave have equal MySQL server UUIDs; these UUIDs must be different for replication to work. 主: mysql\> show variables like '%uuid'; +---------------+--------------------------------------+ \| Variable_name \| Value \| +---------------+--------------------------------------+ \| server_uuid \| 6797f03c-2122-11ee-842b-00505695c6d5 \| +---------------+--------------------------------------+ 1 row in set (0.01 sec) 备: \[root@localhost:mytest\]\>show variables like '%uuid'; +---------------+--------------------------------------+ \| Variable_name \| Value \| +---------------+--------------------------------------+ \| server_uuid \| 6797f03c-2122-11ee-842b-00505695c6d5 \| +---------------+--------------------------------------+ 1 row in set (0.01 sec) vi /testdata/mysql/auto.cnf \[auto

server-uuid=6797f03c-2122-11ee-842b-00505695c6d5

应该是在做物理备份测试的时候把/testdata/mysql/所有文件都COPY过来了导致的

我们在备机把server-uuid=6797f03c-2122-11ee-842b-00505695c6d5这一行删掉重启服务就可以了

root@localhost:(none)\]\>show slave status\\G; \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* 1. row \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* Slave_IO_State: Waiting for master to send event Master_Host: 10.153.119.5 Master_User: slave Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000014 Read_Master_Log_Pos: 481 Relay_Log_File: t3-dtpoc-dtpoc-web05-relay-bin.000004 Relay_Log_Pos: 647 Relay_Master_Log_File: mysql-bin.000014 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: Replicate_Ignore_DB: mysql,information_schema,performance_schema 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: 481 Relay_Log_Space: 869 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: 6797f03c-2122-11ee-842b-00505695c6d5 Master_Info_File: mysql.slave_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) ERROR: No query specified 可以看到主从搭建成功了,我们在主库插入一条数据测试下: 主: mysql\> insert into t1 values(88888888,'test'); Query OK, 1 row affected (0.00 sec)、 备: \[root@localhost:mytest\]\>select \* from t1; +----------+----------+ \| id \| name \| +----------+----------+ \| 1 \| xiaoming \| \| 2 \| xiaohong \| \| 3 \| xiaoli \| \| 3 \| xiaoli \| \| 4 \| xiaozhao \| \| 88888888 \| test \| \| 88888888 \| test \| +----------+----------+ 7 rows in set (0.00 sec) 我们的搭建基础是在主备数据库和数据都一样的情况下,如果主库有数据库mytet,而备份搭建的时候并没有这个数据库,那么主库已有的数据库和数据会自动复制过来吗?我们来测试下 在备库删除Mytet数据库后重建复制: \[root@localhost:mytest\]\>drop database mytest; Query OK, 0 rows affected (0.00 sec) \[root@localhost:(none)\]\>CHANGE MASTER TO MASTER_HOST='10.153.119.5', -\> MASTER_PORT=3306, -\> MASTER_USER='slave', -\> MASTER_PASSWORD='123456', -\> MASTER_LOG_FILE='mysql-bin.000014', -\> MASTER_LOG_POS=154; Query OK, 0 rows affected, 2 warnings (0.02 sec) 发现报错Error executing row event: 'Table ',mytest.t1' doesn't exist',在执行插入mytest.t1 values(88888888,'test')sql时报错。 \[root@localhost:(none)\]\>start slave; Query OK, 0 rows affected (0.01 sec) \[root@localhost:(none)\]\>show slave status\\G; \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* 1. row \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* Slave_IO_State: Waiting for master to send event Master_Host: 10.153.119.5 Master_User: slave Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000014 Read_Master_Log_Pos: 808 Relay_Log_File: t3-dtpoc-dtpoc-web05-relay-bin.000002 Relay_Log_Pos: 320 Relay_Master_Log_File: mysql-bin.000014 Slave_IO_Running: Yes Slave_SQL_Running: No Replicate_Do_DB: Replicate_Ignore_DB: mysql,information_schema,performance_schema Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 1146 Last_Error: Error executing row event: 'Table 'mytest.t1' doesn't exist' Skip_Counter: 0 Exec_Master_Log_Pos: 154 Relay_Log_Space: 1196 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: 0 Last_IO_Error: Last_SQL_Errno: 1146 Last_SQL_Error: Error executing row event: 'Table 'mytest.t1' doesn't exist' Replicate_Ignore_Server_Ids: Master_Server_Id: 1 Master_UUID: 6797f03c-2122-11ee-842b-00505695c6d5 Master_Info_File: mysql.slave_master_info SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: Master_Retry_Count: 86400 Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: 230911 11:29:49 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) ERROR: 我们跳过这个sql,然后重建复制呢? 主库: mysql\> show master status; +------------------+----------+--------------+------------------+-------------------+ \| File \| Position \| Binlog_Do_DB \| Binlog_Ignore_DB \| Executed_Gtid_Set \| +------------------+----------+--------------+------------------+-------------------+ \| mysql-bin.000014 \| 808 \| \| \| \| +------------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec) 备库: \[root@localhost:(none)\]\>stop slave; Query OK, 0 rows affected (0.00 sec) \[root@localhost:(none)\]\>CHANGE MASTER TO MASTER_HOST='10.153.119.5', -\> MASTER_PORT=3306, -\> MASTER_USER='slave', -\> MASTER_PASSWORD='123456', -\> MASTER_LOG_FILE='mysql-bin.000014', -\> MASTER_LOG_POS=808; Query OK, 0 rows affected, 2 warnings (0.02 sec) \[root@localhost:(none)\]\>start slave; Query OK, 0 rows affected (0.00 sec) \[root@localhost:(none)\]\>show slave status\\G; \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* 1. row \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* Slave_IO_State: Waiting for master to send event Master_Host: 10.153.119.5 Master_User: slave Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000014 Read_Master_Log_Pos: 808 Relay_Log_File: t3-dtpoc-dtpoc-web05-relay-bin.000002 Relay_Log_Pos: 320 Relay_Master_Log_File: mysql-bin.000014 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: Replicate_Ignore_DB: mysql,information_schema,performance_schema 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: 808 Relay_Log_Space: 542 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: 6797f03c-2122-11ee-842b-00505695c6d5 Master_Info_File: mysql.slave_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.01 sec) 我们才测试下,原库创建新库新表,然后插入新数据,发现建库建表DDL和插入数据都复制过去了 原库: mysql\> create database mytest1; Query OK, 1 row affected (0.00 sec) mysql\> use mytest1; Database changed mysql\> create table t1(id int,name varchar(20)); Query OK, 0 rows affected (0.01 sec) mysql\> insert into t1 values(8888,'tsss'); Query OK, 1 row affected (0.00 sec) 备库: \[root@localhost:(none)\]\>use mytest1; 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 \[root@localhost:mytest1\]\>select \* from t1; +------+------+ \| id \| name \| +------+------+ \| 8888 \| tsss \| +------+------+ 1 row in set (0.00 sec) \[root@localhost:mytest1\]\>

相关推荐
熬夜的咕噜猫3 小时前
MySQL 核心数据库操作
adb
Yang-Never5 小时前
ADB ->adb shell perfetto 抓取 trace 指令
android·开发语言·adb·android studio
轩情吖9 小时前
MySQL之事务管理
android·后端·mysql·adb·事务·隔离性·原子性
赶路人儿1 天前
常见的mcp配置
android·adb
ego.iblacat1 天前
MySQL 数据库操作
数据库·mysql·adb
路溪非溪1 天前
adb的安装和基本使用总结
adb
XDHCOM1 天前
MySQL报错LDAP认证初始化连接池失败,远程修复思路和故障排查分享
数据库·mysql·adb
闻哥1 天前
深入理解 MySQL InnoDB Buffer Pool 的 LRU 冷热数据机制
android·java·jvm·spring boot·mysql·adb·面试
炸炸鱼.1 天前
MySQL 数据库核心操作手册
数据库·adb·oracle
总要冲动一次2 天前
MySQL 5.7 全量 + 增量备份方案(本地执行 + 远程存储)
数据库·mysql·adb