ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

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

2021-12-25 12:32:03  阅读:164  来源: 互联网

标签:主从复制 从库 log mysql5.7 Master mysql master mysqld 搭建


  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)

标签:主从复制,从库,log,mysql5.7,Master,mysql,master,mysqld,搭建
来源: https://blog.csdn.net/qq_42303467/article/details/122138416

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有