MySQL 主从复制—— 一主两从 配置

三台机器的mysql版本都是5.7.36

master:192.168.23.110

slave1:192.168.23.111

slave2: 192.168.23.112

master配置

1.在/etc/my.cnf中添加:
[mysqld]
# 开启binlog
log-bin=mysql-bin
server-id=1
#binlog-do-db=test_db #需要同步的数据库,如果不配置则同步全部数据库
expire-logs-days=10   #binlog #日志保留的天数,清除超过10天的日志,防止日志文件过大,导致磁盘空间不足

2.配置完成后,重启mysql:
systemctl mysql restart

3.查看当前binlog日志的信息(后面有用):
mysql> show master status;
或
mysql> show master status\G; 
#去掉分号";" 就不会报错:
ERROR: 
No query specified

注意:Position和File下面要用到
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |      154 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)


#4.然后创建用户,创建的这两个用户在配置slave从机时要用到
#GRANT REPLICATION SLAVE ON *.* to 'root'@'192.168.23.111' identified by '123456';
#GRANT REPLICATION SLAVE ON *.* to 'root'@'192.168.23.112' identified by '123456';
#192.168.23.111和192.168.23.112是从机器的ip

slave配置

1.在/etc/my.cnf中添加
[mysqld]
server-id=2

#另外一台192.168.23.112设置为server-id=3

2.设置启动参数
mysql> CHANGE MASTER TO 
    -> MASTER_HOST='192.168.23.110',
    -> MASTER_USER='root',
    -> MASTER_PASSWORD='123456',
    -> MASTER_LOG_FILE='mysql-bin.000001',
    -> MASTER_LOG_POS=154, 
    -> master_port=3306;

3.刷新一下配置
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

开启同步
mysql> START SLAVE;

# 查看启动状态
mysql> show slave status\G;  #或者show slave status;


注意这俩值都为 Yes时 则为成功
Slave_IO_Running: Yes # io线程 此线程为NO 则可能是 数据过大等原因
Slave_SQL_Running: Yes # sql 线程 此线程为NO 就是SQL 同步异常 根据Position 参数解决


然后我一台从机器:192.168.23.112一只报错,按照网上的重新加载mysql库中的那几张表也不行
如果报错:
mysql> CHANGE MASTER TO
    -> MASTER_HOST='192.168.23.110',
    -> MASTER_USER='root',
    -> MASTER_PASSWORD='123456',
    -> MASTER_LOG_FILE='mysql-bin.000001',
    -> MASTER_LOG_POS=154,
    -> master_port=3306;
ERROR 1794 (HY000): Slave is not configured or failed to initialize properly. You must at least set --server-id to enable either a master or a slave. Additional error messages can be found in the MySQL error log.


可以在/etc/my.cnf中添加

[root@localhost support-files]# cat /etc/my.cnf

[mysqld]
bind-address=0.0.0.0
port=3306
user=mysql
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
socket=/tmp/mysql.sock
log-error=/var/log/mysql/mysql.log
pid-file=/usr/local/mysql/mysql.pid
symbolic-links=0
#skip-grant-tables

log-bin=mysql-bin  #加上这个!!!
server-id=3



然后重启数据库:systemctl restart mysql
重新登录数据库:mysql -u root -p 

mysql> CHANGE MASTER TO
    -> MASTER_HOST='192.168.23.110',
    -> MASTER_LOG_FILE='mysql-bin.000001',
    -> MASTER_USER='root',
    -> MASTER_PASSWORD='123456',
    -> MASTER_LOG_POS=154,
    -> master_port=3306;
Query OK, 0 rows affected, 2 warnings (0.00 sec)
成功!


然后查看一下:
mysql> show slave status\G;
*************************** 1. row ***************************
  Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.23.110
                  Master_User: root
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 154
               Relay_Log_File: localhost-relay-bin.000003
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql-bin.000003
             Slave_IO_Running: Yes   #这两个为Yes就行了
            Slave_SQL_Running: Yes   #这两个为Yes就行了

                   ......


1 row in set (0.00 sec)

ERROR: 
No query specified


搞定!

你可能感兴趣的:(mysql,数据库,database)