Mysql主从配置

环境:
RHEL6.5
MySQL5.5.54

Master IP :192.168.3.7
Slave IP :192.168.3.2

第一步:编辑配置文件,开启bin-log日志

1 vim /etc/my.cnf
2 mysql-bin=log-bin
3 server-id=7    //这里的id值必须唯一,这台服务器的ip是192.168.3.7,所以我将id改为7

如下图所示

同理将从服务器上的bin-log日志也打开,将192.168.3.2的server-id改为2,

说明这里server-id可以随便给,只要所有mysql的server-id不重复就可以,这里
我是为了方便记,改为和ip一样。

分别重启两台mysql服务器。

第二步:在主服务器上给从服务器授权

 1 mysql> grant replication slave on *.* to 'repuser'@'%' identified by '123456';
 2 Query OK, 0 rows affected (0.00 sec)
 3 
 4 mysql> show master status;
 5 +------------------+----------+--------------+------------------+
 6 | File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
 7 +------------------+----------+--------------+------------------+
 8 | mysql-bin.000006 |      252 |              |                  |
 9 +------------------+----------+--------------+------------------+
10 1 row in set (0.00 sec)
11 
12 mysql>

第三步:在mysql从服务器上指定master服务器的信息
192.168.3.2上的操作

 1 mysql> change master to
 2     -> master_host='192.168.3.7',
 3     -> master_user='repuser',
 4     -> master_password='123456',
 5     -> master_log_file='mysql-bin.000006',
 6     -> master_log_pos=252;
 7 Query OK, 0 rows affected (0.11 sec)
 8 mysql> start slave;
 9 Query OK, 0 rows affected (0.01 sec)
10 mysql> show slave status \G
11 *************************** 1. row ***************************
12                Slave_IO_State: Waiting for master to send event
13                   Master_Host: 192.168.3.7
14                   Master_User: repuser
15                   Master_Port: 3306
16                 Connect_Retry: 60
17               Master_Log_File: mysql-bin.000006
18           Read_Master_Log_Pos: 252
19                Relay_Log_File: localhost-relay-bin.000003
20                 Relay_Log_Pos: 252
21         Relay_Master_Log_File: mysql-bin.000006
22              Slave_IO_Running: Yes
23             Slave_SQL_Running: Yes
24 
25 ......此处省略部分代码

如下图所示:

第四步:在mysql主服务器上建库建表并插入数据,测试主从配置是否成功并生效
服务器192.168.3.7上

 1 mysql> create database masterdb;
 2 Query OK, 1 row affected (0.04 sec)
 3 
 4 mysql> use masterdb;
 5 Database changed
 6 mysql> create table user(
 7     -> id int auto_increment primary key,
 8     -> name varchar(32) not null default '',
 9     -> age tinyint not null default 0
10     -> )engine=myisam default charset=utf8;
11 Query OK, 0 rows affected (0.10 sec)
12 
13 mysql> insert into user(name,age) values('zhangsan',20);
14 Query OK, 1 row affected (0.05 sec)
15 1 row in set (0.00 sec)
16 
17 mysql> \! ifconfig
18 eth0      Link encap:Ethernet  HWaddr 00:0C:29:79:52:AE  
19           inet addr:192.168.3.7  Bcast:192.168.3.255  Mask:255.255.255.0

在从服务器192.168.3.2上查看数据是否已经同步过来

 1 mysql> select * from masterdb.user;
 2 +----+----------+-----+
 3 | id | name     | age |
 4 +----+----------+-----+
 5 |  1 | zhangsan |  20 |
 6 +----+----------+-----+
 7 1 row in set (0.01 sec)
 8 
 9 mysql> \! ifconfig
10 eth0      Link encap:Ethernet  HWaddr 00:0C:29:CA:6F:9E  
11           inet addr:192.168.3.2  Bcast:192.168.3.255  Mask:255.255.255.0

可以看到主从同步已配置成功。

测试中开始没有关闭两边服务器的防火墙,导致slave_io_running一直处于connection状态,
关闭防火墙iptables -F 再重启从服务器后才成功。

posted on 2017-11-16 22:09  iaknehc  阅读(183)  评论(0编辑  收藏  举报

导航