mysql5.7 主从复制简单环境搭建

  1. 准备两台mysql服务器环境

    参考 https://blog.csdn.net/qq_42303467/article/details/122137696

  2. 配置主库 ip 192.168.95.130

    修改/etc/下的my.cnf文件

    vim /etc/my.cnf

    my.cnf 配置如下

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html

[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.

log_bin=mysql-bin #开启binlog功能

server-id=1 #与从机的id不可相同

sync-binlog=1 #每次执行写入操作与磁盘同步 

binlog-ignore-db=performance_schema  #指定不同步的从库的系统数据库
binlog-ignore-db=information_schema
binlog-ignore-db=sys

#binlog-do-db=mysql  #指定只同步的从库的数据库(可以是任意的) 不指定默认全部

#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
  1. 重启主库mysql

    systemctl restart mysqld

  2. 登录主库

    mysql -uroot -p

  3. 配置主库给从库增加同步授权

    mysql> grant replication slave on *.* to 'root'@'%' identified by 'root';

    mysql> grant all privileges on *.* to 'root'@'%' identified by 'root';

    mysql>flush privileges;

  4. 查看 主库状态

    mysql>show master status;

+------------------+----------+--------------+-------------------------------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB                          | Executed_Gtid_Set |
+------------------+----------+--------------+-------------------------------------------+-------------------+
| mysql-bin.000001 |      869 |              | performance_schema,information_schema,sys |                   |
+------------------+----------+--------------+-------------------------------------------+-------------------+
1 row in set (0.00 sec)

  1. 配置从库 ip192.168.95.132

    vim /etc/my.cnf

    从库的binlog可以不需要开启

    配置如下

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html

[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
server-id=2  #设置serverid

relay_log=mysql-relay-bin  #指定中继日志

read_only=1 #设置只读

#
#
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

  1. 重启从库

    systemctl restart mysqld

  2. 登录从库

    mysql -uroot -p

  3. 指定主库ip 端口 账号 密码 等信息

    mysql>change master to master_host='192.168.95.130',master_port=3306,master_user='root',
    master_password='root',master_log_file='mysql-bin.000001',master_log_pos=869;

  4. 开启从库

    mysql>start slave;

  5. 查看从机状态

    mysql>show slave status \G;

*************************** 1. row ***************************
               Slave_IO_State: Connecting to master
                  Master_Host: 192.168.95.130
                  Master_User: root
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 869
               Relay_Log_File: mysql-relay-bin.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Connecting
            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: 869
              Relay_Log_Space: 154
              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: NULL
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: 0
                  Master_UUID: 
             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 more updates
           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
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)

如果出现 error connecting to master 等问题 记得检查 两台机器的防火墙是否都关闭

  1. 使用主库 验证是否成功

    mysql>create database example;
    mysql>use example;
    mysql>create table user( id int primary key, uername varchar(20))engine=innodb charset=utf8;
    mysql>insert into user values(1,'zhangsan');

  2. 使用从库查看对应的数据库 表

mysql>create database example;

+--------------------+
| Database           |
+--------------------+
| information_schema |
| example            |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql>use example;
mysql>show tables;

+-------------------+
| Tables_in_example |
+-------------------+
| user              |
+-------------------+
1 row in set (0.00 sec)

mysql>select * from user;

+----+----------+
| id | uername  |
+----+----------+
|  1 | zhangsan |
+----+----------+
1 row in set (0.00 sec)
上一篇:MYSQL免安装


下一篇:安装 MySQL 5.7