MySql主从复制配置

主机配置

server-id=1

#开启binlog日志
log-bin=mysql-bin

#忽略的库
binlog-ignore-db=mysql

#复制的库
binlog-do-db=test

#binlog格式:ROW(行模式,记录所有变动的行,存入binlog,缺点:当遇到批量修改的sql时,容易导致日志sql过多)

#                    STATEMENT(记录每条修改的SQL,存入binlog,缺点:当遇到now()这些函数时,会导致主从出现数据误差)

#                    MIXED(实现ROW和STATMENT切换,缺点:无法识别@@的系统变量,比如@@hostname)
binlog-format=STATEMENT

从机配置

server-id = 2

#开启relay log
relay-log = mysql-relay

 

重启两个MySQL

  • systemctl restart mysql或者systemctl restart mysqld(根据具体安装情况)
  • systemctl status mysqld 查看是否重启成功

登录主库,创建绑定账号

  • 登录:mysql -uroot -p 
  • 创建主从复制账号():GRANT REPLICATION SLAVE ON *.* TO 'slave'@'%' IDENTIFIED BY '123456';
    • 如果上面异常则这样分开执行:create user 'slave'@'%' IDENTIFIED BY '123456';         GRANT REPLICATION SLAVE ON *.* TO 'slave'@'%';
  • 查看主库状态:show master status;
  • 记录 File和Position的值

登录从库,配置主从绑定关系

  • 登录:mysql -uroot -p 
  • 复制下面的命令,整体执行;设置从库读取主库的服务器配置,分别为:主机IP、账号、密码、MASTER_LOG_FILE是上面查询的File,MASTER_LOG_POS是上面查询的Position

CHANGE MASTER TO MASTER_HOST="192.168.12.223",  

MASTER_USER='slave',
MASTER_PASSWORD='123456',
MASTER_LOG_FILE='mysql-bin.000001',
MASTER_LOG_POS=438;

  • 启动从库复制:start slave;
  • 查看从库复制状态:show slave status\G;
  • 如下图所示,证明启动成功

其他操作命令:

  • 停止主从复制:stop slave; 

  • 重置主机配置:reset master;

遇见的异常:

  • 配置好my.conf启动成功后登录时异常:[ERROR] unknown variable 'server-id=1'

    将新增的所有主从配置往上移就解决了

  • 执行重启命令systemctl restart mysqld;时异常:Failed to restart mysqld.service: Unit not found 

             MySQL在安装时没有创建名为mysqld的服务,cd  /etc/init.d查看mysql映射的服务名。

           

 

   我这里是mysql,所以修改重启命令为:systemctl restart mysql

 

  •  在从库设置主库配置时异常:ERROR 3021 (HY000): This operation cannot be performed with a running slave io thread; run STOP SLAVE IO_THREAD FOR CHANNEL '' first.

   这是因为从库已经启动了主从复制,先执行STOP SLAVE;停止主从复制,再执行配置即可

  • MySQL8.0在从库设置主库配置时异常:Last_IO_Error: error connecting to master 'slave@111.11.11.111:3306' - retry-time: 60 retries: 18 message: Authentication plugin 'caching_sha2_password' reported error: Authentication requires secure connection.

   修改主库slave账号的密码加密方式:alter user 'slave'@'%' identified with mysql_native_password by '123456';

 

 

posted @ 2022-05-07 10:00  LinYhy  阅读(973)  评论(0编辑  收藏  举报