mysql 主从复制

2018-10-15  本文已影响0人  iwhao

基础信息

主: 192.168.0.1

从: 192.168.0.2

备份

复制 两台服务器的/etc/mysql/mysql.conf.d/mysqld.cnf 到 /etc/mysql/mysql.conf.d/mysqld.cnf.bak 便于数据恢复

主服务器配置

mysqld.cnf

# bind-address           = 127.0.0.1
server-id               = 1
log_bin                 = /var/log/mysql/mysql-bin.log
注释掉bind-address到127.0.0.1,允许远程连接,打开server-id和log二进制文件的注释

设置好 记得重启 mysql服务
centos服务器

service mysqld restart

ubuntu服务器

service mysql restart

创建从库的登录用户,并且授予slave权限

mysql> grant replication slave on *.* to 'iwhao'@'192.168.0.2' identified by '123456';

刷新权限

mysql> FLUSH PRIVILEGES;

mysql> show master status;

获取mysql-bin的file信息和position信息 例如: mysql-bin.000030 154

从服务器配置

server-id               = 2
// 默认不配置的话 是 复制全部 以下配置为 只复制 buildings单库
replicate-do-db=buildings

设置好 记得重启 mysql服务
centos

service mysqld restart

ubuntu

service mysql restart

mysql-bin的值和master_log_pos值 在主服务器中查看 myqsl 中执行 show master status;

change master to master_host='192.168.0.1', master_user='iwhao', master_password='123456', master_log_file='mysql-bin.000030', master_log_pos=154;

mysql> show slave status \G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.0.1
                  Master_User: iwhao
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000030
          Read_Master_Log_Pos: 154
               Relay_Log_File: relay-bin.000002
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql-bin.000030
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: buildings
          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: 154
              Relay_Log_Space: 521
              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: 5f51ec81-7c33-11e8-80f0-00163e06250b
             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)

Slave_IO_Running 和 Slave_SQL_Running 值 都为 Yes 说明就成功了

我的博客

我的掘金

我的简书

Laravel China

我的微信公众号
上一篇 下一篇

猜你喜欢

热点阅读