mysql主从复制

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 [email protected]


二、修改配置文件,在两个节点上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)

发现已经同步过来了。


4、让从服务器同步主服务器当前时间之前所有数据。


4.1首先完全备份主服务器,复制到从服务器上。


# mysqldump --master-data=2 --all-databases --lock-all-tables --events > /root/all.sql
# scp all.sql b:/root/

4.2修改配置文件/etc/my.cnf将之前的配置先注释掉,初始化完了再启用。


4.3让从服务器重新初始化,在/usr/local/mysql目录下执行命令


# scripts/mysql_install_db --user=mysql --datadir=/mydata/data

4.4启用配置文件中的配置


Server -id  = 20
Read-only=1
Relay-log = /mydata/data/relay-bin
skip-slave-start=1
# log-bin = /mysql-bin 关闭二进制日志

4.5 启动服务,导入


# service mysqld start
# mysql < all.sql

4.6 查看是否导入成功


# mysql
Mysql> show databases;

4.6 启动从服务器,指定复制的起始位置


主服务器中


mysql> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000004 |      437 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

从服务器中


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

启动从服务器并查看从服务器状态


Mysql> start slave;
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: 437
               Relay_Log_File: relay-bin.000002
                Relay_Log_Pos: 421
        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: 437
              Relay_Log_Space: 571
              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)

下面可以进行测试,是否可以同步主服务器的数据,应该没什么问题,不再演示。




你可能感兴趣的:(Mysql主从,主从复制)