Master主服务器的ip:192.168.1.113
Slave 从服务器的ip:192.168.1.99
一、master主服务器上设置:
1.权限设置
允许用户user从ip为192.168.1.99的主机连接到mysql服务器(master),并使用password作为密码
下面涉及到,从服务器的ip、登陆用户名、登陆密码。
下面用户名是repl,密码是repl
mysql>GRANT ALL PRIVILEGES ON *.* TO 'repl'@'192.168.1.99' IDENTIFIED BY 'repl' ;
2.文件配置
修改my.ini配置文件
[mysqld]
# The TCP/IP Port the MySQL Server will listen on
port=3306
加入下面两行,设置log文件、服务id
log-bin = mysql-bin.log
server-id = 1
重启mysql服务。
/*下面几个根据需要设定*/
/*
#设定不同步的数据库,这些库的修改不会记录到日志,可以添加多行
binlog-ignore-db = test
#设定记录的库,可以添加多行
binlog-do-db = vnet
*/
3.得到主服务器上当前的二进制日志名和偏移量
mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000005 | 106 | | |
+------------------+----------+--------------+------------------+
1 row in set
二、slave从服务器上设置:
1.文件配置
修改my.ini配置文件
[mysqld]
# The TCP/IP Port the MySQL Server will listen on
port=3306
加入下面两行,设置log文件、服务id
log-bin = mysql-bin.log
server-id = 2
重启mysql服务。
2.在从服务器上,关闭slave线程
Mysql>stop slave;
3.在从服务器做相应设置
指定复制使用的用户,主数据库服务器的ip、端口、以及开始执行复制的日志文件和位置等。
Mysql >Change master to
master_host='192.168.1.113',
master_port=3306,
master_user='repl',
master_password='repl',
master_log_file='mysql-bin.000005',
master_log_pos=106;
/*
#并且可以设置忽略的库,可以添加多行
replicate-ignore-db = testdb
#设置仅处理的库,可以添加多行
replicate-do-db = test
*/
3.在从服务器上,启动slave线程
Mysql >start slave;
4.显示slave的状态信息
Mysql >show slave status;
5.显示从服务器上的进程
Mysql >show processlist;
三、测试查看效果
1、保持master主服务器和slave开启;
在master服务器上操作,数据库和表,能看到,slave服务器上的数据库和表,跟随着,做相应变动。
备注:这里我们默认,要同步的数据库,他们已经具有相同的初始信息,包括schema和具体的表。如果,初始信息不相同,则需将master主服务器中的信息,先备份,然后再导入到从服务器中。下载本文