mysql 主从配置

Mysql 5.6.41安装 

服务器节点两台:  192.168.1.129(主),192.168.1.131(从)

一,192.168.1.129(主)/etc/my.cnf配置文件加入

# # server-id 唯一的服务辨识号,数值位于 1 到 2^32-1之间.
# # 此值在master和slave上都需要设置.
# # 如果 “master-host” 没有被设置,则默认为1, 但是如果忽略此选项,MySQL不会作为master生效.
server-id = 1

#[必须]启用二进制日志
log-bin = mysql-bin

# # 需要开启生成二进制日志记录相关配置,配置在需要被复制的服务器上,即:master
#指定对名称为test_db的数据库记录二进制日志
binlog-do-db = test

#指定不对名称为mysql的数据库记录二进制日志
binlog-ignore-db = mysql

#指定不对名称为information_schema的数据库记录二进制日志                  #
binlog-ignore-db = information_schema

#binlog日志格式,mysql默认采用,如果从服务器slave有别的slave要复制那么该slave也需要这一项#
binlog_format = mixed

#超过7天的binlog删除###
expire_logs_days = 7

重启mysql后,连接进mysql 查看状态

[root@localhost ~]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 17
Server version: 5.6.41-log MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show master status;
+------------------+----------+--------------+--------------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB         | Executed_Gtid_Set |
+------------------+----------+--------------+--------------------------+-------------------+
| mysql-bin.000002 |     1388 | test         | mysql,information_schema |                   |
+------------------+----------+--------------+--------------------------+-------------------+
1 row in set (0.00 sec)

mysql> 

#给ip地址为所有(%表示任何ip)MySQL服务器上的 盈月 授权对该master的复制权限
#mysql> grant replication slave on *.* to 'yingyue'@'%';
#给ip地址为所有(%表示任何ip)MySQL服务器上的 盈月 授权对该master的复制权限
mysql> grant replication slave on *.* to 'hxs'@'192.168.1.%' identified by '123456';

#刷新权限
mysql> flush privileges; 

主数据库配置完成

二, 192.168.1.131(从)/etc/my.cnf配置文件加入

server-id = 2 
#需要做复制的数据库,如果复制多个数据库,重复设置这选项即可master上不需要此项,slave上需要
replicate-do-db = test

#不需要复制的数据库,如果要忽略复制多个数据库,重复设置这个选项即可 
replicate-ignore-db = mysql
replicate-ignore-db = information_schema

#MySQL选项以避免外部锁定。该选项默认开启
skip-external-locking 
    
#默认存储引擎
default-storage-engine = InnoDB
#停止从服务器
mysql> stop slave;

#重置从服务器
mysql> reset slave;

#从服务器关联到主服务器
mysql> change master to master_user='hxs', 
master_password='123456', 
master_host='192.168.1.129',
master_port=3306, 
#可以在主数据库129上通过 show master status 查看
master_log_file='mysql-bin.000002',
master_log_pos=1388;

#开启从服务器
mysql> start slave

#查看从服务器连接状态
 

mysql> show slave status\G ;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.1.129
                  Master_User: hxs
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000002
          Read_Master_Log_Pos: 601
               Relay_Log_File: localhost-relay-bin.000002
                Relay_Log_Pos: 283
        Relay_Master_Log_File: mysql-bin.000002
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: test
          Replicate_Ignore_DB: mysql,information_schema
           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: 601
              Relay_Log_Space: 460
              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: 209b7324-cd01-11e8-9652-000c298cf5f5
             Master_Info_File: /usr/local/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)


是否成功就看
Slave_IO_Running: Yes 
Slave_SQL_Running: Yes
是否都为Yes
如果有出现以上结果,则配置成功!

三,验证主从是否可以成功同步,在主库中进行增删改查,看从表能否正确同步

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章