主库 master:A
备库 slave:B
修改A的my.cnf
server-id = 1
修改B的my.cnf
server-id = 2
要求是slave的server-id要大于master的server-id,整数。
在A
1:创建用户slave_user
mysql> create user 'slave_user'@'xxx.xxx.xxx.xxx' identified by 'password';
给用户授权
mysql> grant REPLICATION SLAVE on *.* to 'slave_user'@'xxx.xxx.xxx.xxx' identified by 'passwordr';
mysql>flush privileges;
在B
2 进行连接测试:
mysql -uslave_user -ppassword -h AIP
登录成功!
在A
3:纪录主库binlog位置
执行如下语句:
show master status;
记录目前的主库binlog所在的位置。
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show master status;
+------------------+-----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+-----------+--------------+------------------+
| mysql-bin.001301 | 951491236 | | mysql,test |
+------------------+-----------+--------------+------------------+
在A
4:备份master现有的数据:
增加锁:
use dbname;
flush table with read lock;
导出主库数据;
mysqldump --opt dbname>dbname.sql;
解锁:
unlock tables;
5:在slave进行数据初始化
tar 之后scp到备库,
在备库(B)执行 mysql dbname source <dbname.sql;
配置同步:
在B:
mysql> change master to
-> master_host=’192.168.1.22′
-> master_user=’slave_user’
-> master_password=’password’
-> master_log_file=’mysql-bin.001301′
-> master_log_pos=951491236;
6:启动B的同步进程:
start slave;
show slave status\G; 查看同步状态。
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
则表示同步正常。
done!