MySQL建立主从服务器配置方法
A、环境描述
服务器A(主) 192.168.1.112
服务器B(从) 192.168.1.113
Mysql版本:mysql-5.1.26
System OS:CentOS 5.9 X64
主从需同步的数据库内容保持一致。
B、配置步骤
a)主机配置
1、主机配置:(192.168.1.112),主机添加备机连接权限,并且拥有replication slave的权限
grant replication slave on *.* to 'rep'@'192.168.1.113' identified by '123456'; flush privileges;
修改/etc/my.cnf 文件:开启binlog功能
[mysqld] server-id = 1 log-bin=/usr/local/mysql/biglog //开启binlog功能 binlog-do-db = mo //要同步的数据库 binlog-ignore-db=mysql //忽略的数据库
service mysqld restart
[root@localhost var]# mysql -u root -p123456 Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 3 Server version: 5.1.26-rc-log Source distribution Type 'help;' or '\h' for help. Type '\c' to clear the buffer. mysql>
mysql>flush tables with read lock;
mysql>show master status\G
*************************** 1. row ***************************
File: mysql-bin.000003
Position: 106
Binlog_Do_DB: mo
Binlog_Ignore_DB: mysql
1 row in set (0.00 sec)
mysql>unlock tables;
b)备机配置
1、修改/etc/my.cnf 的配置文件 这个要注意点,吃一个亏,定位了1一个小时,之前的源码安装中,cp的my.cnf 文件,已经包含了server-id 配置,一定要注释掉
[mysqld] server-id = 2//注意一定为2,或者和主机的不一样的值 log-bin=/usr/local/mysql/biglog //开启binlog功能 binlog-do-db = mo //要同步的数据库 binlog-ignore-db=mysql //忽略的数据库2、重启服务
service mysqld restart
mysql>change master to master_host='192.168.1.112', master_user='rep', master_password='123456', master_log_file='mysql-bin.000003', master_log_pos=106;
master_host :主机ip
master_user:就是在主机配置的 步骤1中用户
master_password:密码
master_log_file :binlog二进制文件,可以使用show master stauts \G 命令查询到的File,示例如下
mysql> show master status\G *************************** 1. row *************************** File: mysql-bin.000003 Position: 106 Binlog_Do_DB: mo Binlog_Ignore_DB: mysql 1 row in set (0.00 sec)master_log_pos:就是 show master stauts \G命令查询到的Position
4、启动从机的服务线程
mysql>start slave;
5、查看,可以看到Slave_IO_Running:YES 和Slave_SQL_Running:Yes,就表示OK了
mysql>show slave status\G;
mysql> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.1.112 Master_User: replication Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000003 Read_Master_Log_Pos: 106 Relay_Log_File: localhost-relay-bin.000010 Relay_Log_Pos: 251 Relay_Master_Log_File: mysql-bin.000003 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: mo Replicate_Ignore_DB: mysql,information_schema 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: 106 Relay_Log_Space: 555 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 Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 0 Last_SQL_Error: 1 row in set (0.00 sec)
在测试的主机数据库中找一张表,对其增删改,看下从机是否同步了
注意:
这里注意点主从机器之间的防火墙要关掉,不关闭可能会出现以下错误
error connecting to master retry-time 60 retries 86400
关闭方式:
service iptables status 查看iptables状态
service iptables restart iptables服务重启
service iptables stop iptables服务禁用
后续在写一个主主同步。