CentOS 数据库主从(读写分离)

2016年5月13日

两台主机 Centos
master slave

199.180.135.109 master
212.124.107.15 slave

首先安装两台主机的环境 详见Apache + mysql + php 的安装 也可以安装 Mysql

1、mastre机上:

mysql
mysql>create database www;
mysql>use www;
mysql>create table www(id int);
mysql>insert into www values(1);
mysql> select * from www;
+------+
| id |
+------+
| 1 |
+------+
1 row in set (0.01 sec)mysql>create database blog;
mysql>use blog;
mysql>create table blog(id int);
mysql>insert into blog values(1);
mysql> select * from blog;
+------+
| id |
+------+
| 1 |
+------+
1 row in set (0.00 sec)

2、编辑 master 机上的my.cnf

vim /etc/my.cnf

注意一下2个参数,这一段在[mysqld]节点中添加

log-bin=mysql-bin //日志为2进制,不需要更改
server-id=1 //为1就是Master,不需要更改
binlog-do-db=blog //要同步的库
binlog-do-db=www //要同步的库
binlog-ignore-db=mysql,test,information_schema
//是不要记录日志的数据库名,多个数据库中间用逗号(,)隔开log-bin=mysql-bin
server-id=1
binlog-do-db=blog
binlog-do-db=www
binlog-ignore-db=mysql,test,information_schema

3、从master服务器添加要从slave服务器访问master服务器的有权限的帐号 (可以有多个 slave)

格式:GRANT REPLICATION SLAVE ON *.* TO '帐号'@'从服务器IP或主机名' IDENTIFIED BY '密码';
例如:
grant replication slave on *.* to repluser@'212.124.107.15' identified by '123456';

更新下权限 flush privileges;

重启 mysql : service mysqld restart

flush tables with read lock; //不要退出这个终端,否则这个锁就不生效了。从服务器的数据库建好后。在主服务器执行解锁

mysql> show master status;
+------------------+----------+--------------+-------------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+-------------------------------+
| mysql-bin.000001 | 106 | blog,www | mysql,test,information_schema |
+------------------+----------+--------------+-------------------------------+
1 row in set (0.01 sec)

取得快照并记录日志名和偏移量
开启另一个终端对主服务器数据目录做快照。
#cd /var/lib/mysql
#tar -zcvf backup.tar.gz www blog
此时在主库解开table的锁定
mysql> unlock tables;

4 、 进入 slave的主机编辑 vim /etc/my.cnf 即从服务器

vim /etc/my.cnf 添加如下内容在[mysqld]中

server-id = 2
#master-host The master server IP
master-host=199.180.135.109
master-user=repluser
master-password=123456
master-port=3306
master-connect-retry=60
#replicate-do-db=www //告诉slave只做www数据库的更新
#replicate-do-db=blog //告诉slave只做blog数据库的更新
#log-slave-updates

5、把从主数据库服务器备份出来的数据库导入到从服务器中

先用scp或ftp等方式把主服务器上的backup.tar.gz拷贝过来,解压到从服务器的musql目录下 /var/lib/mysql

SCP:
scp [email protected]:/var/lib/mysql/backup.tar.gz /var/lib/mysql/
scp [email protected]:/var/lib/mysql/backup.tar.gz /var/lib/mysql/
tar zxvf backup.tar.gz

6、重新启动从库服务器 以及 置主服务器的各种参数

#service mysqld restart

停止slave服务,设置主服务器的各种参数

#mysql
mysql>slave stop;
change master to
MASTER_HOST='199.180.131.122',
MASTER_USER='repluser',
MASTER_PASSWORD='123456',
MASTER_LOG_FILE='mysql-bin.000001',
MASTER_LOG_POS=98;MASTER_LOG_FILE 和 MASTER_LOG_POS是 主服务器 show master status; 中得到的File | Position 的值。

mysql> slave start;

7、查看当前 slave 当前状态

show slave status/G;

*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 199.180.135.109
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 106
Relay_Log_File: mysqld-relay-bin.000002
Relay_Log_Pos: 251
Relay_Master_Log_File: mysql-bin.000001
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: 106
Relay_Log_Space: 407
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:
1 row in set (0.00 sec)

Slave_IO_Running: Yes
Slave_SQL_Running: Yes
这两个为 Yes 才表示配置成功

从节点2的配置跟从节点1的配置一样,只是server-id 改成3 (多从)

最重要是以下三点:

Slave_IO_Running:是否要从 Master Server 复製 Binary Log 资料,必须为 Yes。

Slave_SQL_Running:是否要执行从 Master Server 复製过来的 Binary Log 资料,必须为 Yes。

Seconds_Behind_Master:Slave 的资料落后了 Master 多少秒,执行一段时间后应该会是零。

然后在master上更新数据,会发现在2台slave上都能查询到数据,本实验测试成功。

http://my.oschina.net/duxuefeng/blog/73707

master blog 权限 所有
grant all privileges on blog.* to "blog"@"%" identified by "123456";

savle 权限 仅仅可查看即只读
grant select on blog.* to "blog"@"%" identified by "123456";

没有评论

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注