一、配置MySQL主服务器(192.168.21.128)
mysql -u root -p #进入MySQL控制台
create database study; #建立数据库study
grant replication slave on *.* to 'root'@'192.168.21.129' identified by 'root' with grant option; #授权用户root只能从192.168.21.129这个IP访问主服务器192.168.21.128上面的数据库,并且只具有数据库备份的权限(注意这里的黄色部分root, root,分别为数据库的帐号,密码,前面为帐号
后面是密码,因为这里的权限是replication, 所以on 后只能是 *.* 如果想指定单一同步的库,可以在my.cnf中设置,后面会提到)
grant all on *.* to 'root'@'192.168.21.130' identified by 'root' with grant option; #授权用户root从192.168.21.130完全访问数据库 (注意这里的黄色部分root, root,分别为数据库的帐号,密码,前面为帐号,后面是密码,注意如果代码是多个服务器上的集群,一定要给all的权限,不然,若代码在非主库的服务器,将无法写入数据进主库)
flush privileges; #刷新系统授权表
二、配置MySQL主服务器(192.168.21.128)的my.cnf文件
vi /etc/my.cnf #编辑配置文件,在[mysqld]部分添加下面内容
server-id=1 #设置服务器id,为1表示主服务器,注意:如果原来的配置文件中已经有这一行,就不用再添加了。
log-bin=mysql-bin #启动MySQ二进制日志系统,注意:如果原来的配置文件中已经有这一行,就不用再添加了。
binlog-do-db= study #需要同步的数据库名,如果有多个数据库,可重复此参数,每个数据库一行
binlog-ignore-db=mysql #不同步mysql系统数据库
:wq! #保存退出
service mysql restart #重启MySQL
mysql -u root -p #进入mysql控制台
show variables like 'server_id'; #查看server-id的值是否为1
mysql> show variables like 'server_id';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id |1 |
+---------------+-------+
1 row in set (0.00 sec)
show master status; #查看主服务器,出现以下类似信息
mysql> show master status;
+------------------+----------+--------------+------------------+
|File |Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000011 | 107 | osyunweidb |mysql |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
注意:这里记住File的值:mysql-bin.000011和Position的值:107,后面会用到。
三、配置MySQL从服务器(192.168.21.129)的my.cnf文件
vi /etc/my.cnf #编辑配置文件,在[mysqld]部分添加下面内容
server-id=2 #设置服务器id,修改其值为2,表示为从数据库
log-bin=mysql-bin #启动MySQ二进制日志系统,注意:如果原来的配置文件中已经有这一行,就不用再添加了。
replicate-do-db= study #需要同步的数据库名,如果有多个数据库,可重复此参数,每个数据库一行
replicate-ignore-db=mysql #不同步mysql系统数据库
read_only #设置数据库只读
:wq! #保存退出
service mysql restart #重启MySQL
mysql -u root -p #进入MySQL控制台
show variables like'server_id'; #查看server-id的值,必须为上面设置的2,否则请返回修改配置文件
mysql> show variables like 'server_id';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id |2 |
+---------------+-------+
1 row in set (0.01 sec)
stop slave; #停止slave同步进程
change master tomaster_host='192.168.21.128',master_user='root',master_password='root',master_log_file='mysql-bin.000011' ,master_log_pos=107; #执行同步语句
start slave; #开启slave同步进程
SHOW SLAVE STATUS\G #查看slave同步信息,出现以下内容
mysql> SHOW SLAVE STATUS\G
*************************** 1. row***************************
Slave_IO_State: Waiting for master to sendevent
Master_Host: 192.168.21.128
Master_User: osyunweidbbak
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000011
Read_Master_Log_Pos: 107
Relay_Log_File:mysqlslave-relay-bin.000004
Relay_Log_Pos: 253
Relay_Master_Log_File: mysql-bin.000011
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: osyunweidb
Replicate_Ignore_DB: mysql
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: 107
Relay_Log_Space: 560
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>
注意查看:
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
以上这两个参数的值为Yes,即说明配置成功!
如果出现Slave_IO_Running:No; 看是不是有错误日志 Last_IO_Error:Fatal error: The slave I/O thread stops because master and slave have equalMySQL server UUIDs; these UUIDs must be different for replication to work.
如果是有,环境是Lnmp, 编辑Mysql uuid为不同的即可, 位置 /usr/local/mysql/var/auto.cnf, 其它错误请自行查看日志解决。
另一台从库的配置和第一个从库类似,不再重复。