MySQL 主从搭建

环境

两台 Ubuntu 机器

master

Ubuntu 16.04.2 LTS 192.168.189.137

slave

Ubuntu 16.04.2 LTS 192.168.189.133

MySQL 版本均为 5.5.62-0ubuntu0.14.04.1

修改配置

master 机器中 /etc/mysql/my.conf (其他版本可能在 /etc/mysql/mysql.conf.d/mysqld.cnf )配置添加以下:


# server id 默认 1
server-id               = 1
#  开启binlog(二进制日志)         
log_bin                 = /var/log/mysql/mysql-bin.log
# 对那些库进行复制
binlog_do_db            = permission
# 对那些库进行忽略(多个使用逗号隔开)
binlog_ignore_db        = mysql

slave 机器同理,不过需要设置不同的 server-id

启动

  1. 启动master 机器的 mysql,并连接
  2. 创建一个特权账号给 slave 用来进行复制,因为是在本地使用,因此使用 授权访问IP为 192.168开头即可
mysql> GRANT REPLICATION SLAVE,FILE ON *.* TO 'repl'@'192.168.%' IDENTIFIED BY 'repl';
  1. 查看 master ,执行 show master status命令
mysql> show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000003 |     2457 | permission   |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.01 sec)

  1. 启动 slave 机器的 mysql 并连接
  1. 向 master 建立复制连接

master_host master 机器 ip
master_port 端口号
master_user 用户名
master_password 密码
master_log_file binlog 文件名字(show master status 中 File 列 )
master_log_pos 偏移量(show master status 中 Position 列 )

mysql > CHANGE MASTER TO master_host = '192.168.189.137',master_port = 3306,master_user='repl',
master_password='repl',master_log_file='mysql-bin.000003',master_log_pos = 2457; 
  1. 查看 slave 状态 show slave status\G;
mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: 
                  Master_Host: 192.168.189.137
                  Master_User: echo_slave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 2457
               Relay_Log_File: ubuntu-relay-bin.000009
                Relay_Log_Pos: 304
        Relay_Master_Log_File: mysql-bin.000003
             Slave_IO_Running: No
            Slave_SQL_Running: No
              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: 2457
              Relay_Log_Space: 512
              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: 1
                  Master_UUID: 
             Master_Info_File: /var/lib/mysql/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: 
           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)

可以看到 IO 线程、SQL 线程都没有运行,

Slave_IO_Running: No
Slave_SQL_Running: No

  1. 启动复制线程(关闭为:STOP SLAVE)
START SLAVE:

此时
Slave_IO_Running: Yes
Slave_SQL_Running: Yes

便完全开启复制了

  1. 其他

默认错误日志在 /var/log/mysql

你可能感兴趣的:(MySQL 主从搭建)