Mysql主从配置

准备工作

环境

  1. 两台虚拟机,地址:master-192.168.52.130/slave-192.168.52.131;
  2. 系统版本:Centos 7;
  3. 软件版本:mysql 5.6;

数据库

  1. 同样的配置:端口-3307等;
  2. 同样的数据:database-TestDB等;

主从配置

主库配置

  1. 编辑 /etc/my.cnf:
[mysqld]
basedir=/var/lib/mysql
datadir=/var/lib/mysql/data
socket=/var/lib/mysql/mysql.sock
port=3307

log-bin=/var/lib/mysql/mysql-bin.log
explicit_defaults_for_timestamp=true

innodb_flush_log_at_trx_commit=1
sync_binlog=1
server-id=1

binlog-do-db=TestDB
binlog-ignore-db=performance_schema
binlog-ignore-db=mysql
binlog-ignore-db=test

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

# Recommended in standard MySQL setup
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

配置项解释:

server-id=1 标识
binlog-do-db=TestDB 需要同步的数据库
binlog-ignore-db=performance_schema 不需要同步的数据库

  1. 创建同步用户,在主服务器上为从服务器建立一个连接帐户,该帐户必须授予REPLICAITON SLAVE权限。在主服务器登陆mysql上执行:
grant replication slave on *.* to 'master'@'%' identified by '123456';
  1. 重启之后,查看状态:
mysql> show master status\G;
*************************** 1. row ***************************
             File: mysql-bin.000009
         Position: 120
     Binlog_Do_DB: TestDB
 Binlog_Ignore_DB: performance_schema,mysql,test
Executed_Gtid_Set: 
1 row in set (0.01 sec)

注意上面的 File 和 Position ,从库slave 配置的时候会使用。

从库配置

  1. 编辑 /etc/my.cnf
[mysqld]

...

server_id=2
log-bin=mysql-bin.log
replicate-do-db=TestDB
  1. change master语句指定同步位置:
mysql>change master to master_host='192.168.52.130', master_user='master', master_password='123456', master_log_file='mysql-bin.000007', master_log_pos=120,master_port=3307;

  1. 启用/关闭 slave;
start slave;

stop slave;
  1. 重启,查看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.52.130
                  Master_User: master
                  Master_Port: 3307
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000007
          Read_Master_Log_Pos: 120
               Relay_Log_File: mysqld-relay-bin.000010
                Relay_Log_Pos: 283
        Relay_Master_Log_File: mysql-bin.000007
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: TestDB
          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: 120
              Relay_Log_Space: 620
              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: 1
                  Master_UUID: 1cacfa49-04c4-11e7-b744-000c29d5b78c
             Master_Info_File: /var/lib/mysql/data/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)

测试

在主库进行数据的操作,查看从库的数据。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 这篇博客会详细介绍如何配置主从配置,但重点是想分享如何当配置未成功时,如何调试。 原文连接(推荐这个看 比较清晰)...
    海里的沙丁鱼阅读 802评论 0 2
  • 简介 MySQL 的主从复制可以实现mysql的多种高可用性,如数据库的读写分离 ,在线热备,负载均衡,数据分布 ...
    超凡陆战队阅读 1,139评论 0 3
  • 环境准备: 两个相同版本的mysql实例10.230.20.101/10.230.20.102(mysql 5.7...
    jsdjl阅读 470评论 0 0
  • mysqly主从 1、授权一个用户,在从服务器上可以通过这个用户来连接主服务器: grant all on *.*...
    一直以来都很好阅读 212评论 0 0
  • 大多数人找我聊天并不是真的希望从我这得到什么指引,倾诉者只是单纯的想找个人说说话而已。 如果有人跟你抱怨生活种种的...
    泉布阅读 403评论 0 3