1,关闭防火墙

对主服务器master关闭 132 slave从服务器也关闭133


2,配置主服务器132

在mysql配置文件my.cnf中的[mysqld]节点下配置 (要先关闭mysql服务器)

  1. log_bin=mysql-bin
  2. server-id=132 (为ip地址的最后一段)


3,启动mysql服务,并连接进入mysql

service mysql start
[root@localhost ~]# mysql -u root -p

4,授权从服务器中的slave 此处的IP为从服务器服务器

mysql>GRANT REPLICATION SLAVE ON *.* to 'root'@'192.168.245.133' identified by 'root'; ####Mysql8下执行: mysql>GRANT REPLICATION SLAVE ON  *.*  to 'root'@'%';(需要先修该user表中host为%)

5, 查看master(主)服务器的状态


mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000002 |      156 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.01 sec)

6,配置从服务器

配置从服务器中mysql的my.cnf配置文件(同第一步一样,但要注意bin_log和service_id的值不能相同)


7,启动mysql服务并连接

8,在从服务器中执行同步语句


change master to master_host='主服务器的ip', 
master_user='root', master_password='root',(为主服务的密码和用户) 
master_log_file='第五步结果中的file字段值',
master_log_pos='第五步结果中的Position'字段值;

mysql> change master to
    -> master_host='192.168.223.131',
    -> master_user='root',
    -> master_password='root',
    -> master_log_file='mysql-bin.000002',
    -> master_log_pos=156;

9,启动从服务器的slave


mysql> start slave;/*stop slave关闭slave*/
Query OK, 0 rows affected, 1 warning (0.00 sec)

10 查看slave状态

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event/*代表等待主服务操作*/
                  Master_Host: 192.168.223.131
                  Master_User: root
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000002
          Read_Master_Log_Pos: 156
               Relay_Log_File: localhost-relay-bin.000002
                Relay_Log_Pos: 324
        Relay_Master_Log_File: mysql-bin.000002
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes

如果SlaveIO_State: Waiting for master即为成功
[📎mysql
主从备份.rar](https://www.yuque.com/attachments/yuque/0/2018/rar/96947/1535014578113-3ea2d767-c369-4434-b46c-aab5eaf2ac9a.rar)