MySQL8.0.30一主两从复制与配置(一)_蜗牛杨哥的博客-CSDN博客
MySQL8.xx一主两从复制安装与配置
MySQL8.XX随未生成随机密码解决方案
本文主要对: MySQL8.xx安装与配置 的完善与补充
主机IP | 端口 | 节点 | 备注 |
192.168.1.100 | 3306 | master | |
192.168.1.101 | 3306 | slave | |
192.168.1.102 | 3306 | slave |
# 配置服务ID
server-id=100
# 配置二进制日志目录
log-bin=/usr/local/mysql8/mysql-8.0.30/binlog
#主从复制忽略的数据库
binlog-ignore-db=mysql
binlog-ignore-db=information_schema
#开启主从复制的数据库
binlog-do-db=tope-pay-user
# bin log 日志格式
#STATEMENT:记录主库执行的SQL复制到从库; 调用时间函数时会导致主从数据不一致
#ROW:记录主库每一行的变化;效率低
#MIXED:修复一些主从数据不一致情况;本地变量调用还会存在问题;@@hostname
binlog_format=statement
#二进制日志自动删除/过期的天数。默认值为0,表示不自动删除
expire_logs_days=7
#跳过主从复制中遇到的所有错误或指定类型的错误
slave_skip_errors=1062
规则很多,这里只罗列几个常规的:
binlog-do-db |
需要同步的库名 | 备注 |
binlog-ignore-db | 不需要同步的库名 | |
replication-do-table | 需要同步的表名,格式:库名.表名(test.t_table) | |
replication-ignore-table | 不需要同步的表名,格式:库名.表名(test.t_table) |
当然也可使用现有账号 例如账号:slave或者 replica: create user 'replica'@'%';
mysql> CREATE USER 'slave'@'%' IDENTIFIED WITH mysql_native_password BY 'mysql123456';
Query OK, 0 rows affected (0.01 sec)mysql> GRANT REPLICATION SLAVE,replication client ON *.* TO 'slave'@'%';
Query OK, 0 rows affected (0.00 sec)mysql>
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
2.4:配置完成后重启mysql服务器
[root@www ~]# systemctl restart mysql
[root@www ~]# systemctl status mysql
● mysql.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysql.service; enabled; vendor preset: disabled)
Active: active (running) since 二 2023-08-29 12:54:00 CST; 6s ago
Docs: http://dev.mysql.com/doc/refman/en/using-systemd.html
Main PID: 4550 (mysqld)
Tasks: 39
CGroup: /system.slice/mysql.service
└─4550 /usr/local/mysql8/mysql-8.0.30/bin/mysqld --defaults-file=/etc/my.cnf8月 29 12:54:00 www.yhchange.com systemd[1]: Started MySQL Server.
mysql> show master status;
+---------------+----------+---------------+--------------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------+----------+---------------+--------------------------+-------------------+
| binlog.000001 | 157 | tope-pay-user | mysql,information_schema | |
+---------------+----------+---------------+--------------------------+-------------------+
1 row in set (0.00 sec)
#192.168.1.101 节点
# 服务id
server-id=101
#启用中继日志
relay-log=mysql-relay
#192.168.1.102 节点
# 服务id
server-id=102
#启用中继日志
relay-log=mysql-relay
[root@www ~]# systemctl restart mysql
[root@www ~]# systemctl status mysql
● mysql.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysql.service; enabled; vendor preset: disabled)
Active: active (running) since 二 2023-08-29 13:12:37 CST; 18s ago
Docs: http://dev.mysql.com/doc/refman/en/using-systemd.html
Main PID: 4093 (mysqld)
Tasks: 39
CGroup: /system.slice/mysql.service
└─4093 /usr/local/mysql8/mysql-8.0.30/bin/mysqld --defaults-file=/etc/my.cnf8月 29 13:12:37 www.yhchange.com systemd[1]: Started MySQL Server.
两台全新的从服务数据库状况:
[root@www ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.30 MySQL Community Server - GPL
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.02 sec)
master:
mysql> show variables like '%server_id%';
+----------------+-------+
| Variable_name | Value |
+----------------+-------+
| server_id | 100 |
| server_id_bits | 32 |
+----------------+-------+
2 rows in set (0.06 sec)
slave:
mysql> show variables like '%server_id%';
+----------------+-------+
| Variable_name | Value |
+----------------+-------+
| server_id | 101 |
| server_id_bits | 32 |
+----------------+-------+
2 rows in set (0.04 sec)
mysql> show variables like '%server_id%';
+----------------+-------+
| Variable_name | Value |
+----------------+-------+
| server_id | 102 |
| server_id_bits | 32 |
+----------------+-------+
2 rows in set (0.02 sec)
mysql> change master to master_host='192.168.1.100',master_user='slave',master_password='mysql123456',master_log_file='binlog.000001',master_log_pos=157;
Query OK, 0 rows affected, 8 warnings (0.03 sec)
注意这里的
master_host为主服务器的ip
master_user为连接主服务器的用户名
master_password为连接主服务器的密码
master_log_file为要同步的日志文件file,即对应上面主服务器查看时的File字段
master_log_pos为要同步日志文件的位置,即对应上面主服务器查看时的Position字段
mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql> show slave status;
+----------------------------------+---------------+-------------+-------------+---------------+-----------------+---------------------+--------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+--------------------------------------+-------------------------+-----------+---------------------+----------------------------------------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+----------------------+--------------+--------------------+------------------------+-----------------------+-------------------+
| Slave_IO_State | Master_Host | Master_User | Master_Port | Connect_Retry | Master_Log_File | Read_Master_Log_Pos | Relay_Log_File | Relay_Log_Pos | Relay_Master_Log_File | Slave_IO_Running | Slave_SQL_Running | Replicate_Do_DB | Replicate_Ignore_DB | Replicate_Do_Table | Replicate_Ignore_Table | Replicate_Wild_Do_Table | Replicate_Wild_Ignore_Table | Last_Errno | Last_Error | Skip_Counter | Exec_Master_Log_Pos | Relay_Log_Space | Until_Condition | Until_Log_File | Until_Log_Pos | Master_SSL_Allowed | Master_SSL_CA_File | Master_SSL_CA_Path | Master_SSL_Cert | Master_SSL_Cipher | Master_SSL_Key | Seconds_Behind_Master | Master_SSL_Verify_Server_Cert | Last_IO_Errno | Last_IO_Error | Last_SQL_Errno | Last_SQL_Error | Replicate_Ignore_Server_Ids | Master_Server_Id | Master_UUID | Master_Info_File | SQL_Delay | SQL_Remaining_Delay | Slave_SQL_Running_State | Master_Retry_Count | Master_Bind | Last_IO_Error_Timestamp | Last_SQL_Error_Timestamp | Master_SSL_Crl | Master_SSL_Crlpath | Retrieved_Gtid_Set | Executed_Gtid_Set | Auto_Position | Replicate_Rewrite_DB | Channel_Name | Master_TLS_Version | Master_public_key_path | Get_master_public_key | Network_Namespace |
+----------------------------------+---------------+-------------+-------------+---------------+-----------------+---------------------+--------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+--------------------------------------+-------------------------+-----------+---------------------+----------------------------------------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+----------------------+--------------+--------------------+------------------------+-----------------------+-------------------+
| Waiting for source to send event | 192.168.1.100 | slave | 3306 | 60 | binlog.000001 | 157 | mysql-relay.000002 | 323 | binlog.000001 | Yes | Yes | | | | | | | 0 | | 0 | 157 | 529 | None | | 0 | No | | | | | | 0 | No | 0 | | 0 | | | 100 | d25ed6c6-3b31-11ee-8765-000c29dfccdb | mysql.slave_master_info | 0 | NULL | Replica has read all relay log; waiting for more updates | 86400 | | | | | | | | 0 | | | | | 0 | |
+----------------------------------+---------------+-------------+-------------+---------------+-----------------+---------------------+--------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+--------------------------------------+-------------------------+-----------+---------------------+----------------------------------------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+----------------------+--------------+--------------------+------------------------+-----------------------+-------------------+
1 row in set, 1 warning (0.00 sec)
mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)
看见 Slave_IO_Running: Yes Slave_SQL_Running: Yes 都为Yes则代表配置成功
delimiter $$
CREATE PROCEDURE pro_sys_index_sys_employer(num int)
BEGIN
DECLARE count int DEFAULT 0 ; -- 计数
DECLARE max int ; -- 插入数据条数 num 记录条数
SET max = num ;
-- 开始执行循环
WHILE count < max DO
start transaction;
INSERT INTO `sys_employer` ( `empno`, `ename`,`ejob`, `mgr`, `birthday`, `salary` , `compony`, `deptno`)
VALUES (( FLOOR(RAND()*805300 + RAND()*400530)),CONCAT('许总',(LAST_INSERT_ID()+1)),'恒大许老板',FLOOR(RAND()*100 + 205300),(date_add('1836-01-01', interval rand() * 31 day)),(RAND()*6530 + rand(1) * 10), CONCAT('北京市海淀区中关村创新文化交流中心',(LAST_INSERT_ID()+1)),(FLOOR(RAND()*300 + 6700)));
commit;
SET count = count + 1;
END WHILE;
END$$
delimiter ;
看看slave节点是否同步;先确认下从节点数据库数据为空;不未空则先删除
# 调用存储过程
call pro_sys_index_sys_employer(600);
看看数据条数:与预期符合
4.3: 在主节点创建sys_user表; 看看是否同步到slave节点
CREATE TABLE `sys_user` (
`userId` bigint NOT NULL AUTO_INCREMENT,
`username` varchar(50) DEFAULT NULL,
PRIMARY KEY (`userId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
slave节点: