主库 ip:192.168.1.66
从库 ip:192.168.1.67
主机:
1)创建一个slave用户,并且赋予权限
grant replication slave,reload,super on *.* to ‘slave‘@‘%‘ identified by ‘123456‘;
flush privileges;
2)更改配置文件(/etc/my.cnf 放在[mysqld]下面)
server-id = 1 #主机标示,整数
log-bin = mysql-binlog #确保此文件可写
read-only =0 #主机,读写都可以
binlog-do-db =1309phpb #需要备份数据,多个写多行
binlog-ignore-db =mysql #不需要备份的数据库,多个写多行
3)重启mysql
service mysqld restart
4)查看主库的状态
在mysql中:show master status\G
mysql> show master status\G;
*************************** 1. row ***************************
File: mysql-binlog.000003
Position: 271
Binlog_Do_DB: 1309phpb
Binlog_Ignore_DB: mysql
1 row in set (0.00 sec)
ERROR:
No query specified
出现这样的提示,说明主库配置没有什么问题了
5)查看主库日志开始
mysql> show master status;
+---------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+---------------------+----------+--------------+------------------+
| mysql-binlog.000003 | 271 | 1309phpb | mysql |
+---------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
二)从库的配置
1)修改从库配置文件(vi /etc/my.cnf)
server-id = 2
log-bin = mysql-binlog
master-host =192.168.1.66
master-user =slave
master-pass =123456
master-port =3306
master-connect-retry=60 #如果从服务器发现主服务器断掉,重新连接的时间差(秒)
replicate-do-db =lvs #只复制某个库
replicate-ignore-db=mysql #不复制某个库
2)重启mysql服务
3)进入mysql 更改主库参数
change master to master_host=‘192.168.1.66‘,master_user=‘slave‘,master_password=‘123456‘,master_log_file=‘File字段的值‘,master_log_pos=Position 字段值;
查看主库日志开始
mysql> show master status;
+---------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+---------------------+----------+--------------+------------------+
| mysql-binlog.000003 | 271 | 1309phpb | mysql |
+---------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
4)重启进程
start slave
5)查看从库的的状态
show slave status\G
show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.51
Master_User: slave
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-binlog.000003
Read_Master_Log_Pos: 271
Relay_Log_File: localhost-relay-bin.000004
Relay_Log_Pos: 419
Relay_Master_Log_File: mysql-binlog.000003
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: 1309phpb
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: 271
Relay_Log_Space: 578
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)
6)此时可以从主库添加数据,从库查询数据了