1、主库需改配置文件,在/etc/my.cnf中添加:
port = 3306
log_bin = /var/lib/mysql/mysql-binlog
server-id = 1 //只要主从不一样就行
binlog_do_db = test//要做同步的数据库名字,可以是多个数据库,之间用分号分割。
2、从库的配置文件中添加
server-id = 2
master-host = 10.4.14.168 //主库的ip地址
master-user = gechong //同步的mysql账号
master-password = gechong //同步的mysql密码
master-port = 3306
master-connect-retry = 5
replicate-do-db = test //数据库名
3、分别重启服务,登陆数据库。
4、主库上创建复制用户
mysql> GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO gechong@'%' IDENTIFIED BY 'gechong';
5、在主库上执行
mysql> show master status;
+---------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+---------------+----------+--------------+------------------+
| binlog.000003 | 412 | | |
+---------------+----------+--------------+------------------+
1 row in set (0.00 sec)
6、从库上执行
mysql> show slave status\G