1.主DB server和从DB server数据库的版本一致
2.修改/etc/my.cnf文件:
#指定binlog文件在哪个数据库上,这里指定在db1上,这里特别需要注意的是,这里只指定了对数据库db1进行主从复制,也就意味着其它数据库是不能主从复制的
binlog-do-db=db1
#binlog忽略的数据库,这里忽略mysql数据库本身带的mysql库
binlog-ignore-db=mysql
#启用二进制日志
log-bin=mysql-bin
#服务器唯一ID,一般取IP最后一段
server-id=120
3.重启mysql
service mysqld restart
4.建立账户并授权slave
#创建一个用户"backup",并且设置密码
GRANT FILE ON *.* TO 'yourusername'@'%' IDENTIFIED BY 'yourpassword';
#为新建的用户授权,允许主从复制
GRANT REPLICATION SLAVE ON *.*TO 'yourusername'@'%' identified by 'yourpassword';
刷新权限
FLUSH PRIVILEGES;
5.查询master的状态
show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000002 | 1016 | xxx | mysql | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
6.修改从服务器/etc/my.cnf文件,在[mysqld]下面添加
server-id=121
并重启数据库
7.配置从服务器
change master to master_host='ipaddress',master_port=3306,master_user='yourusername',master_password='yourpassword',master_log_file='mysql-bin.000002',master_log_pos=1016
8.启动从服务器复制功能
start slave;
9.检查从服务器复制功能状态
Slave_IO_Running: Yes //此状态必须YES
Slave_SQL_Running: Yes //此状态必须YES
在主服务器的/etc/my.cnf文件当中,在[mysqld]下面我们配置了这么一个配置
binlog-do-db=db1
主从服务器只针对名称为"db1"的数据库进行主从复制,其它数据库不能进行主从复制。