码迷,mamicode.com
首页 > 数据库 > 详细

mysql主从复制

时间:2016-11-01 14:57:50      阅读:290      评论:0      收藏:0      [点我收藏+]

标签:mysql复制

Master主服务器IP:192.168.1.202 

Slave从服务器IP:192.168.1.203

Master配置

设置server-id、启用bin-log二进制日志

vim /etc/my.cnf

[mysqld]

log_bin = master_bin

server-id = 1

innodb_file_per_table = ON

skip_name_resolve = ON

添加slave账户授权访问

mysql> grant replication slave,replication client on *.* to ‘repluser‘@‘192.168.1.%‘ identified by ‘123123‘;

Query OK, 0 rows affected (0.00 sec)


mysql> flush privileges;

Query OK, 0 rows affected (0.00 sec)


Slave配置

设置server-id、启用relay-log中继日志

vim /etc/my.cnf

[mysqld]

relay-log = relay_log  #中继日志

server-id = 3

innodb_file_per_table = ON

skip_name_resolve = ON


重启master、slave服务

service mysqld restart


复制操作

在Master查看bin-log日志文件及位置

mysql> show master logs;

+-------------------+-----------+

| Log_name          | File_size |

+-------------------+-----------+

| master_bin.000001 |       120 |

+-------------------+-----------+

1 row in set (0.00 sec)


mysql> show master status;

+-------------------+----------+--------------+------------------+-------------------+

| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |

+-------------------+----------+--------------+------------------+-------------------+

| master_bin.000001 |      525 |              |                  |                   |

+-------------------+----------+--------------+------------------+-------------------+

1 row in set (0.00 sec)


在slave中向master服务器建立复制链接,MASTER_LOG_POS=0;表示从第一行位置开始复制。

在slave中建立授权访问

mysql> CHANGE MASTER TO MASTER_HOST=‘192.168.1.202‘,

    -> MASTER_USER=‘repluser‘,

    -> MASTER_PASSWORD=‘123123‘,

    -> MASTER_LOG_FILE=‘master_bin.000001‘,

    -> MASTER_LOG_POS=0;

Query OK, 0 rows affected, 2 warnings (0.03 sec)


Mysql>start slave;    //启动从服务器复制


mysql> show slave status\G;  //从服务器查看复制状态

*************************** 1. row ***************************

               Slave_IO_State: Waiting for master to send event

                  Master_Host: 192.168.1.202

                  Master_User: repluser

                  Master_Port: 3306

                Connect_Retry: 60

              Master_Log_File: master_bin.000001

          Read_Master_Log_Pos: 525

               Relay_Log_File: relay_log.000004

                Relay_Log_Pos: 689

        Relay_Master_Log_File: master_bin.000001

             Slave_IO_Running: Yes

            Slave_SQL_Running: Yes

              Replicate_Do_DB: 

          Replicate_Ignore_DB: 

Slave_IO_Running 、Slave_SQL_Running为Yes,表明slave复制状态正常。


Master,slave同步测试

在master中创建sync_db库及test1表,并添加一条记录

mysql> create database sync_db;

Query OK, 1 row affected (0.00 sec)


mysql> use sync_db;

Database changed


mysql> create table test1( id int(10));

Query OK, 0 rows affected (0.01 sec)


mysql> insert into test1 values(10);

Query OK, 1 row affected (0.00 sec)


mysql> select * from test1;

+------+

| id   |

+------+

|   10 |

+------+

1 row in set (0.00 sec)


mysql> show master status;

+-------------------+----------+--------------+------------------+-------------------+

| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |

+-------------------+----------+--------------+------------------+-------------------+

| master_bin.000001 |     1048 |              |                  |                   |

+-------------------+----------+--------------+------------------+-------------------+

1 row in set (0.00 sec)


Slave中查看数据同步是否一致。

mysql> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| mysql              |

| performance_schema |

| sync_db            |

+--------------------+

4 rows in set (0.00 sec)


mysql> select * from test1;

+------+

| id   |

+------+

|   10 |

+------+

1 row in set (0.00 sec)


mysql> show slave status\G;

*************************** 1. row ***************************

               Slave_IO_State: Waiting for master to send event

                  Master_Host: 192.168.1.202

                  Master_User: repluser

                  Master_Port: 3306

                Connect_Retry: 60

              Master_Log_File: master_bin.000001

          Read_Master_Log_Pos: 1048

               Relay_Log_File: relay_log.000004

                Relay_Log_Pos: 1212

        Relay_Master_Log_File: master_bin.000001

             Slave_IO_Running: Yes

            Slave_SQL_Running: Yes

              Replicate_Do_DB: 

          Replicate_Ignore_DB: 

mysql主从复制

标签:mysql复制

原文地址:http://linuxinfo.blog.51cto.com/412748/1868013

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!