环境
注意虚拟机环境下的NAT模式,务必把IP设置成固定的
Master操作系统:Centos 5.6
Ip:192.168.214.138
Slave操作系统:Centos 5.6
Ip:192.168.214.137
根据安装方法的不同,数据库相关路径也不太一致,我用YUM安装,因些,数据库存放路径/var/lib/mysql,数据库配置文件路径/usr/share/mysql,数据库相关命令/usr/bin.
查看主库服务器/usr/share/mysql目录下的cnf文件
/usr/share/mysql/*.cnf
其中.cnf文档如下:
my-small.cnf 内存少于或等于64M,只提供很少数据库服务
my-medium.cnf 内存在32M-64M之间而且和其他服务一起使用,如web
my-large.cnf 内存在512M主要提供数据库服务
my-huge.cnf 内存在1G-2G,主要提供数据库服务
my-innodb-heavy-4G.cnf 内存有4G,主要提供较大负载数据库服务(一般服务器使用这个)
复制文件到/etc下并更名为my.cnf
cp /usr/share/mysql/my-large.cnf /etc/my.cnf
***Master端***
进入mysql,创建一个数据库wing
Mysql>create database wing;
创建一个用来同步的用户,指定只能在192.168.214.137登录
Mysql>grant replication slave on *.* to 'wing'@'192.168.214.137' identified by '123456';
打开my.cnf,并添加如下字段
vi /etc/my.cnf
server-id = 1
以下内容直接加在server-id = 1下面即可
log-bin=mysql-bin
binlog-do-db = wing //需要同步的数据库,如果没有本行,即表示同步所有数据库
master-host = 192.168.214.137
master-user = wing
master-password = 123456
master-port = 3306
master-connect-retry = 10
replicate-do-db = wing //需要接收的数据库,如有多个数据库,每个数据库一行
binlog-ignore-db = mysql
重启master机的mysql服务
Service mysqld restart
进入mysql,执行
Mysql>slave start;
***Slave端***
进入mysql,创建一个数据库wing
Mysql>create database wing;
创建一个用来同步的用户,指定只能在192.168.214.138登录
Mysql>grant replication slave on *.* to 'wing'@'192.168.214.138' identified by '123456';
打开my.cnf,并添加如下字段
Vi /etc/my.cnf
其中把Server-id = 1注释掉
Server-id = 2
以下内容直接加在server-id = 2下面即可
log-bin=mysql-bin *****注意此处**** 它在后面会经常使用的
Binlog-do-db = wing
Master-host = 192.168.214.138
Master-user = wing
Master-password = 123456
Master-port = 3306
Master-connect-retry = 10
Replcate-do-db = wing //需要接收的数据库,如有多个数据库,每个数据库一行
binlog-ignore-db = mysql
然后重启slave机的mysql
Service mysqld restart
在slaves机中进入mysql
Mysql>start slave;
****关键步骤****
把两台服务器上需要同步的数据库进行拷贝,保证这两台数据库初始状态一致
进行双向同步
双向同步就是把单向同步反过来在做一遍,但一定要注意操作顺序,这是成功的关键。
在master服务器上进入mysql
mysql> show master status;
+------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------+----------+--------------+------------------+
| mysql-bin.000010 | 98 | wing | mysql |
+------------+----------+--------------+------------------+
1 row in set (0.00 sec)
记录下log.000010 和98
在slave服务器上执行
mysql> stop slave;
Query OK, 0 rows affected (0.00 sec)
输入如下命令:
mysql> change master to
-> master_host = '192.168.214.138',
-> master_user = 'wing',
-> master_password = '123456',
-> master_log_file = 'mysql-bin.000010',
-> master_log_pos = 98;
Query OK, 0 rows affected (0.00 sec)
mysql> start slave; //开始同步
Query OK, 0 rows affected (0.00 sec)
在slave服务器上进入mysql
mysql> show master status;
+------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------+----------+--------------+------------------+
| mysql-bin.000009 | 98 | wing | mysql |
+------------+----------+--------------+------------------+
1 row in set (0.00 sec)
记录下mysql-bin.000009 和98
在master服务器上执行
mysql> stop slave;
Query OK, 0 rows affected (0.00 sec)
输入如下命令:
mysql> change master to
-> master_host = '192.168.214.137',
-> master_user = 'wing',
-> master_password = '123456',
-> master_log_file = 'mysql-bin.000009',
-> master_log_pos = 98;
Query OK, 0 rows affected (0.00 sec)
mysql> start slave; //开始同步
Query OK, 0 rows affected (0.00 sec)
下面进行测试。
在slave机器数据库上查看:
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.214.138
Master_User: wing
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000010
Read_Master_Log_Pos: 98
Relay_Log_File: mysqld-relay-bin.000015
Relay_Log_Pos: 235
Relay_Master_Log_File: mysql-bin.000010
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: wing
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 98
Relay_Log_Space: 235
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
1 row in set (0.01 sec)
ERROR:
No query specified
在master机器数据库上查看:
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.214.137
Master_User: wing
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000009
Read_Master_Log_Pos: 98
Relay_Log_File: mysqld-relay-bin.000015
Relay_Log_Pos: 235
Relay_Master_Log_File: mysql-bin.000009
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: wing
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 98
Relay_Log_Space: 235
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
1 row in set (0.00 sec)
ERROR:
No query specified
在salve和master数据库中进入wing 分别创建enkjsalve ,enkj两个表 ,两表会实时更新数据库信息。
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| test |
| wing |
+--------------------+
4 rows in set (0.06 sec)
mysql> use wing;
Database changed
mysql> show tables;
+----------------+
| Tables_in_wing |
+----------------+
| enkj |
| enkjsalve |
+----------------+
2 rows in set (0.00 sec)