MySQL实现主从复制
1.主服务器配置
设置复制主配置
vi /etc/my.cnf
[mysqld]
log-bin=mysql-bin
server-id=1
创建用于复制的用户
mysql> CREATE USER 'repl'@'%.example.com' IDENTIFIED BY 'password';
mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%.example.com';
获取复制主二进制日志坐标
mysql > SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000003 | 73 | test | manual,mysql |
+------------------+----------+--------------+------------------+
选择数据快照的方法
使用mysqldump创建数据快照
mysqldump --all-databases --master-data > dbdump.db
重启mysql
service mysqld restart
2.设置复制从站
设置复制从站配置
[mysqld]
server-id=2
在从设备上设置主配置
mysql> CHANGE MASTER TO
-> MASTER_HOST='master_host_name',
-> MASTER_USER='replication_user_name',
-> MASTER_PASSWORD='replication_password',
-> MASTER_LOG_FILE='recorded_log_file_name',
-> MASTER_LOG_POS=recorded_log_position;
设置新主服务器和从服务器之间的复制
mysql -h master < fulldb.dump
将从站添加到复制环境
mysql> STOP SLAVE;
mysql> SHOW SLAVE STATUS\G
START SLAVE;
mysql> SHOW SLAVE STATUS\G
参考连接:link