MySQL 主从复制

Master: 192.186.40.130
Slave :   192.186.40.131

以下是在Master库进行的操作

# 在Master上启用bin log,设置唯一Server ID
shell> vi /etc/my.cnf
[mysqld]
log-bin=mysql-bin
server-id       = 1

# 建立复制用的user
mysql> CREATE USER 'repl'@'%' IDENTIFIED BY 'repl';
mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%';

# 锁定Master,并记录日志的file和position信息

mysql> FLUSH TABLES WITH READ LOCK;
mysql> show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000020 |      696 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.03 sec)

 

# Master库的数据快照
shell> mysqldump --all-databases --lock-all-tables > dbdump.db

# 释放锁定mysql> UNLOCK TABLES;

以下是在Master库进行的操作

# 在Slave设置唯一Server ID
shell> vi /etc/my.cnf
[mysqld]
server-id       = 101

# 以--skip-slave-start 方式启动Slave
shell> mysqld_safe --skip-slave-start &

# 导入快照数据
shell> mysql < dbdump.dump

# 配置关于Master的信息
mysql> change master to
-> master_host='192.168.40.130',
-> master_user='repl',
-> master_password='repl',
-> master_log_file='mysql-bin.000020',
-> master_log_pos=696 ;


# 启动Slave进程
mysql> START SLAVE;
原文地址:https://www.cnblogs.com/wait4friend/p/2334562.html