mysql主从复制配置

我这里用两台服务器,一个Linux、一个mac使用docker容器搭建的mysql 进行演示,IP分别是15(Master)【端口为3307】,222(Slave)【端口为3308】

注意:配置时,根据自己对应的端口进行配置,一般情况下,安装的默认都是3306端口,自行修改即可。 

备注(mysql允许外部访问):

mysql -u root -p 
#选择基础信息数据库
use mysql;
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '你的密码';
#清除缓存,重新加载权限
flush privileges;

 

Master配置

使用命令行进入mysql:

 

mysql -u root -p

 

接着输入root用户的密码(密码忘记的话就网上查一下重置密码吧~),然后创建用户:

//192.168.22.222是slave从机的IP
GRANT REPLICATION SLAVE ON *.* to 'root'@'192.168.22.222' identified by 'xiong@xiong';

 

 

创建的这两个用户在配置slave从机时要用到。

接下来在找到mysql的配置文件/etc/my.cnf,增加以下配置:

# 开启binlog
log-bin=mysql-bin
server-id=104
# 需要同步的数据库,如果不配置则同步全部数据库
binlog-do-db=test_db
# binlog日志保留的天数,清除超过10天的日志
# 防止日志文件过大,导致磁盘空间不足
expire-logs-days=10 

譬如:

 

 

配置完成后,重启mysql:

service mysql restart

 

然后继续进入mysql命令行执行:show master status\G;

 

 

Slave配置

Slave配置相对简单一点。从机肯定也是一台MySQL服务器,所以和Master一样,找到/etc/my.cnf配置文件,因为我这边是使用的docker的mysql搭建的服务,所以步骤如下:

# 因为我本机docker的端口已经被占用,所以设置成了3308
docker run -d -p 3308:3306 -e MYSQL_ROOT_PASSWORD=xiong --privileged=true --name  slave mysql:5.6

# 进入容器
docker exec -ti slave /bin/bash

# 修改my.cnf
vim /etc/mysql/my.cnf

配置以下信息:
[mysqld]
log-bin=mysql-bin
server-id=3308
expire-logs-days=10

 

 然后继续配置:

CHANGE MASTER TO 
MASTER_HOST='192.168.22.15',//主机IP
MASTER_USER='root',//之前创建的用户账号
MASTER_PASSWORD='xiong@xiong',//之前创建的用户密码
MASTER_LOG_FILE='mysql-bin.000004',//master主机的binlog日志名称
MASTER_LOG_POS=862,//binlog日志偏移量
master_port=3307;//端口

 

设置完之后需要启动:

# 启动slave服务
start slave;

然后继续执行:

show slave status\G;

成功效果:

mysql> show slave status \G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.22.15
                  Master_User: root
                  Master_Port: 3307
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 154
               Relay_Log_File: mysqld-relay-bin.000003
                Relay_Log_Pos: 333
        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: 154
              Relay_Log_Space: 507
              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: 232
                  Master_UUID: cba2fada-f0e7-11ea-9fae-00cfe04ae3a4
             Master_Info_File: /var/lib/mysql/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
           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
1 row in set (0.00 sec)

 

 

如果出现以下问题:

提示错误:Got fatal error 1236 from master when reading data from binary log: 'Could not find first log file name in binary log index file

解决方法:https://blog.csdn.net/mameng1988/article/details/84142859

 

测试主从复制

在master主机执行sql:

 

slave:

 

 

 

 

posted @ 2021-04-16 17:33  ToDarcy  阅读(147)  评论(0编辑  收藏  举报