MySQL主从复制


一、准备

1、同步时间

Ntpdate 时间服务器ip

2、主从节点可以互相解析

# vim /etc/hosts

172.16.5.11 www.a.com a

172.16.5.12 www.b.com b

3、建立双机互信

# ssh-keygen -t rsa -P ‘’

# ssh-copy-id root@172.16.5.11

二、修改配置文件,在两个节点上mysql初始化完之后修改。

1、主服务器配置文件修改

Server-id = 10

Binlog_format=mixed

sync-binlog = ON 安装完了之后配置,不要再初始化数据文件的时候启用

2、从服务器配置文件修改

Server -id = 20

Read-only=1

Relay-log = /mydata/data/relay-bin

Relay-log-index = /mydata/data/relay-bin.index 中继日志索引,这个可以不启用,中继日志启动之后,自动开启,开启是为了自定义它的目录

skip-slave-start=1

# log-bin = /mysql-bin 关闭二进制日志

三、实现主从复制

1、在主服务器上创建一个只有复制权限的用户

mysql> grant replication slave,replication client on *.* to 'test'@'%' identified by 'pass';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> show master status;
+------------------+----------+--------------+------------------+
| File            | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000003 |      341 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.01 sec)

2、从服务器上启动主从复制功能

mysql> change master to master_host='172.16.5.11', master_user='user',
master_password='pass',master_log_file='mysql-bin.000003',master_log_pos=341;

这里最好指定位置,否则可能会报错,不如直接指定,来的省事。

mysql> start slave;
也可以单独启动线程,但start slave 默认会将连个线程同时启动
mysql> start io_thread;
mysql> start sql_thread;
mysql> show slave status\G
*************************** 1. row ***************************
              Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.16.5.11
                  Master_User: user
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000004
          Read_Master_Log_Pos: 341
              Relay_Log_File: relay-bin.000004
                Relay_Log_Pos: 253
        Relay_Master_Log_File: mysql-bin.000004
            Slave_IO_Running: Yes
            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: 188
              Relay_Log_Space: 403
              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
1 row in set (0.00 sec)

3、在中服务器上创建数据库,看从服务器能否同步。

主服务器

mysql> create database db0;
Query OK, 1 row affected (0.01 sec)
mysql> show databases;
+--------------------+
| Database          |
+--------------------+
| information_schema |
| DB1                |
| db0                |
| mysql              |
| performance_schema |
| test              |
+--------------------+
6 rows in set (0.00 sec)

从服务器

mysql> show databases;
+--------------------+
| Database          |
+--------------------+
| information_schema |
| DB1                |
| db0                |
| mysql              |
| performance_schema |
| test              |
+--------------------+
6 rows in set (0.01 sec)

发现已经同步过来了。

推荐阅读:

Ubuntu下Nginx做负载实现高性能WEB服务器5---MySQL主主同步

生产环境MySQL主主同步主键冲突处理

MySQL主从失败 错误Got fatal error 1236

MySQL主从复制,单台服务器上实施

 

继续阅读本文的精彩内容请看第2页

  • 1
  • 2
  • 下一页

相关内容