场景描述:
主数据库服务器:172.16.19.187MySQL已经安装,并且无应用数据。
从数据库服务器:172.16.19.188MySQL已经安装,并且无应用数据。

 

主服务器上进行的操作

启动mysql服务
/opt/mysql/init.d/mysql start

通过命令行登录管理MySQL服务器
/opt/mysql/bin/mysql -uroot -p'yhl'

授权给从数据库服务器172.16.19.188
mysql> GRANT REPLICATION SLAVE ON *.* to 'yhl'@'172.16.19.188' identified by 'yhl';

 
查询主数据库状态
mysql> show master status;

+------------------+----------+--------------+------------------+

| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |

+------------------+----------+--------------+------------------+

| mysql-bin.000003 |     1012 |              |                  |

+------------------+----------+--------------+------------------+

1 row in set (0.00 sec)

记录下 FILE Position 的值,在后面进行从服务器操作的时候需要用到。

配置从服务器
修改从服务器的配置文件/opt/mysql/etc/my.cnf
server-id = 1修改为 server-id = 10,并确保这个ID没有被别的MySQL服务所使用。

启动mysql服务
/opt/mysql/init.d/mysql start

通过命令行登录管理MySQL服务器
/opt/mysql/bin/mysql -uroot -p'yhl

 
执行同步SQL语句

mysql> change master to master_host='172.16.19.187',master_user='yhl',master_password='yhl',master_log_file='mysql-bin.000003',master_log_pos=1012;'

 
正确执行后启动Slave同步进程
mysql> start slave;

主从同步检查

mysql> show slave status\G

*************************** 1. row ***************************

Slave_IO_State: Connecting to master

Master_Host: 172.16.19.187

Master_User: yhl

Master_Port: 3306

Connect_Retry: 60

Master_Log_File: mysql-bin.000003

Read_Master_Log_Pos: 1012

Relay_Log_File: bogon-relay-bin.000001

Relay_Log_Pos: 4

Relay_Master_Log_File: mysql-bin.000003

Slave_IO_Running: yes

Slave_SQL_Running: Yes

……………省略若干……………

其中Slave_IO_Running Slave_SQL_Running 的值都必须为YES,才表明状态正常。

如果为 Slave_IO_Running: Connecting可能有以下几个原因

1、  网络不通