1、主从安装mysql,版本一致
我们装的是 mysql-5.5.30.tar.gz 这里省略…
2、修改master,slave服务器
Shell
master服务器配置:
vi /usr/local/mysql/etc/my.cnf
[mysqld]
server-id=202 #设置服务器唯一的id,默认是1,我们设置ip最后一段,slave设置203
log-bin=mysql-bin # 启用二进制日志
binlog-ignore-db = mysql,information_schema #忽略写入binlog的库
slave服务器配置:
vi /usr/local/mysql/etc/my.cnf
[mysqld]
server-id=203
replicate-do-db = abc #只同步abc库
slave-skip-errors = all #忽略因复制出现的所有错误
1
2
3
4
5
6
7
8
9
10
11
12
13
master服务器配置:
vi /usr/local/mysql/etc/my.cnf
[mysqld]
server-id=202 #设置服务器唯一的id,默认是1,我们设置ip最后一段,slave设置203
log-bin=mysql-bin # 启用二进制日志
binlog-ignore-db = mysql,information_schema #忽略写入binlog的库
slave服务器配置:
vi /usr/local/mysql/etc/my.cnf
[mysqld]
server-id=203
replicate-do-db = abc #只同步abc库
slave-skip-errors = all #忽略因复制出现的所有错误
3、重启主从服务器mysql
Shell
/etc/init.d/mysqld restart
1
/etc/init.d/mysqld restart
4、在主服务器上建立帐户并授权slave
Shell
mysql> mysql -u root -p123.com
mysql> GRANT REPLICATION SLAVE ON . to ‘sync’@‘192.168.1.2’ identified by ‘1234.com’; #replication:复制
1
2
mysql> mysql -u root -p123.com
mysql> GRANT REPLICATION SLAVE ON . to ‘sync’@‘192.168.1.2’ identified by ‘1234.com’; #replication:复制
5、查看主数据库状态
Shell
mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000002 | 263 | | |
+------------------+----------+--------------+------------------+
1
2
3
4
5
6
mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000002 | 263 | | |
+------------------+----------+--------------+------------------+
6、配置从数据库
Shell
mysql> change master to
-> master_host='192.168.0.202',
-> master_user='sync',
-> master_password='1234.com',
-> master_log_file='mysql-bin.000002',
-> master_log_pos=263;
#Log和pos是master上随机获取的。这段也可以写到my.cnf里面。
1
2
3
4
5
6
7
mysql> change master to
-> master_host='192.168.0.202',
-> master_user='sync',
-> master_password='1234.com',
-> master_log_file='mysql-bin.000002',
-> master_log_pos=263;
#Log和pos是master上随机获取的。这段也可以写到my.cnf里面。
7、启动slave同步进程并查看状态
Shell
mysql> start slave;
1
mysql> start slave;
103403849.jpg
其中Slave_IO_Running 与 Slave_SQL_Running 的值都必须为YES,才表明状态正常。
8、验证主从同步
在主mysql创建数据库abc,再从mysql查看已经同步成功!
Shell
mysql> create database abc;
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| abc |
| mysql |
| performance_schema |
| test |
+--------------------+
1
2
3
4
5
6
7
8
9
10
11
mysql> create database abc;
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| abc |
| mysql |
| performance_schema |
| test |
+--------------------+
在slave启动报错:
“Last_IO_Error: Got fatal error 1236 from master when reading data from binary log: ‘Could not find first log file name in binary log index file’”
解决:报错的原因主要是slave设置master的二进制文件名或pos值不对应!
先flush logs;清空日志,在查看下主数据库的状态 show master status;看下日志文件名字和position值;
再在slave中,执行:CHANGE MASTER TO MASTER_LOG_FILE=‘二进制日志名’,MASTER_LOG_POS=值;
最后启动同步进程:start slave;
本文转自 Lee_吉 博客,原文链接: http://blog.51cto.com/12173069/2047750 如需转载请自行联系原作者