返回顶部

lnmp-mysql主从复制

要实现MySQL的主从复制,首先必须打开master端的binlog记录功能,可通过在MySQL的配置文件my.cnf的master模块增加"log-bin" 参数选项实现
[mysqld]
log-bin = /data/3306/mysql-bin #必须放在[mysqld]后


MySQL主从复制原理过程:
1.从服务器做一个start slave命令开启主从复制开关,开始进行主从复制
2.从服务器的I/O线程通过已经授权复制的用户的权限请求连接主服务器,并从指定的binlog日志文件,发送binlog日志内容
3.主服务器收到来自从服务器的I/O线程的请求后,根据从服务器的I/O线程请求的信息分批读取binlog日志内容,然后返回给从服务器I/O线程,返回信息除了binlog日志内容,还有主服务器的记录的新的binlog文件名称,以及binlog中跟新的位置
4.当从服务器I/O线程获取主服务器上I/O线程发来的日志内容,日志文件,及位置后,会将binlog日志依次写到从服务器的Relay log(中继日志),将新的的binlog文件名及位置记录到master-info文件中,以便下次读取主服务器内容
5.从服务器点的sql线程会检查本地Relay log中I/O线程新增加的日志内容,然后把Relay log内容,解析成SQL语句。

 

binlog只会记录对数据库更改的SQL语句,不会记录查询

 

主从复制需要的服务器角色

[root@mysql ~]# netstat -tulnp | grep mysqld
tcp 0 0 0.0.0.0:3307 0.0.0.0:* LISTEN 40427/mysqld
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 40097/mysqld

[root@mysql ~]# ifconfig eth0 | awk -F'[ :]+' 'NR==2{print $4}'
192.168.1.14

master服务器为: 192.168.1.14 port为 3306
slave服务器为: 192.168.1.14 port为 3307

 

在master上的操作
1)修改主库的配置文件(/data/3306/my.cnf)
[mysqld]
server-id = 1 #用于同步的id不能相同
log-bin=/data/3306/mysql-bin

[root@mysql ~]# bash /data/3306/mysql restart

[root@mysql ~]# mysql -uroot -S /data/3306/mysql.sock
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.5.49-log Source distribution
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show variables like 'server_id';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id     | 1       |      #server_id =1
+---------------+-------+
1 row in set (0.13 sec)

mysql> show variables like 'log_bin';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_bin     | ON    | #binlog日志开启
+---------------+-------+
1 row in set (0.00 sec)

mysql> grant replication slave on *.* to'yang'@'192.168.1.%' identified by 'redhat';
Query OK, 0 rows affected (0.15 sec)

mysql> flush privileges;

mysql> select user,host from mysql.user;
+------+-------------+
| user | host |
+------+-------------+
| root | 127.0.0.1 |
| yang | 192.168.1.% |
| root | localhost |
+------+-------------+
3 rows in set (0.00 sec)

 

posted on 2017-03-07 01:32  augustyang  阅读(364)  评论(0编辑  收藏  举报

导航