赞
踩
配置信息 | 说明 |
---|---|
linux系统版本 | CentOS7.4 |
内核 | ml-3.10.0 |
mysql版本 | 8.0.33 |
主mysqlIP地址 | 192.168.1.10 |
从mysqlIP地址 | 192.168.1.11 |
vim /etc/my.cnf
[mysqld]
server-id=1 #server-id表示主库的唯一标识
log-bin=mysql-bin #log-bin表示开启二进制日志
binlog-do-db=testdb #binlog-do-db表示需要同步的数据库名
systemctl restart mysqld
创建用户test并指定该地址在192.168.1.11上登录,授予REPLICATION SLAVE权限
mysql> CREATE USER 'test'@'192.168.1.11' IDENTIFIED BY 'Yrdy123!';
mysql> GRANT REPLICATION SLAVE ON *.* TO 'test'@'192.168.1.11';
查看主数据库服务器的二进制日志文件和位置信息
mysql> show master status;
±--------------------------±------------±-------------±--------------------------------±------------------------+
| File | Position| Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
±--------------------------±------------±-------------±--------------------------------±------------------------+
| mysql-bin.000001|718 | testdb | | |
±--------------------------±------------±-------------±--------------------------------±------------------------+
vim /etc/my.cnf
[mysqld]
server-id=2
log-bin=mysql-bin
binlog-do-db=testdb
重启数据库
systemctl restart mysqld
创建数据库
mysql> CREATE DATABASE testdb; #从数据库需要先创建与主数据库相同的数据库
连接主服务器
mysql> change master to master_host='192.168.1.10', master_user='test', master_password='Yrdy123!', master_port=3306, master_log_file='mysql-bin.000001', master_log_pos=718;
启动从服务器与主服务器的同步
mysql> start slave;
查看服务器的状态
mysql> show slave status\G;
在主数据表中新增列
mysql> ALTER TABLE 第99小学
ADD COLUMN 备注信息 VARCHAR(100) ;
从数据库查看是否新增成功
mysql> DESC 第99小学;
解决
发现设置连接主服务器的IP地址写错,关闭主从复制,在重新连接
停止 MySQL 的主从复制,在主数据库上先输入
STOP SLAVE;
主数据库上重置主从同步配置
RESET SLAVE;
从数据库上重置主从同步配置
RESET SLAVE;
重新连接主服务器(注意重新查看主服务器Position值)
mysql> change master to master_host='192.168.1.10', master_user='test', master_password='Yrdy123!', master_port=3306, master_log_file='mysql-bin.000001', master_log_pos=718;
Error connecting to source 'test@192.168.1.10:3306'. This was attempt 2/86400, with a delay of 60 seconds between attempts. Message: Authentication plugin 'caching_sha2_password' reported error: Authentication requires secure connection.
vim /etc/my.cnf
[mysqld]
default_authentication_plugin=mysql_native_password
修改完成后重启服务
systemctl restart mysqld
进入数据库删除已创建用户并在重新创建一次
mysql> DROP USER 'test'@'192.168.1.11'; #删除用户
mysql> CREATE USER 'test'@'192.168.1.11' IDENTIFIED BY 'Yrdy123!';
mysql> GRANT REPLICATION SLAVE ON *.* TO 'test'@'192.168.1.11';
再次查看主数据库的状态,File 和 Position数值可能会变化,其他在按上面进行一次操作
mysql> show master status;
Coordinator stopped because there were error(s) in the worker(s). The most recent failure being: Worker 1 failed executing transaction 'ANONYMOUS' at source log mysql-bin.000003, end_log_pos 1789. See error log and/or performance_schema.replication_applier_status_by_worker table for more details about this failure or others, if any.
解决
关闭主从同步后,在从数据库创建数据库,需要和主数据库保持一致。创建完毕后再次连接主服务器正常
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。