在主服务器新建一个用户,给从服务器同步使用。
GRANT ALL PRIVILEGES ON *.* TO 'syncAccount'@'172.16.254.170' IDENTIFIED BY 'syncPassword';
修改my.ini
server-id=1
log-bin=mysql-bin
binlog-do-db=lbqdb
binlog-ignore-db=mysql
重启主服务器 mysql服务
mysql>show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 | 446 | lbqdb | mysql |
+------------------+----------+--------------+------------------+
1 row in set
修改slave机中mysql配置文件my.cnf
同样在[mysqld]字段下添加如下内容
server-id=2
master-host=xxx.xxx.xxx.xxx
master-user=syncAccount
master-password=syncPassword
master-port=3306
master-connect-retry=60
replicate-do-db=lbqdb
重启mysql报错:
service mysql start
Starting MySQL.. ERROR! The server quit without updating PID file (/var/lib/mysql/localhost.localdomain.pid).
经查 因为这些参数5.1.7以后就不支持了
master-host=
master-user=
master-password=
master-port=
master-connect-retry=
这些参数 mysql5.5已经废弃了
必须在slave上用change master to 来设置slave
重新编辑my.cnf
server_id = 2
log_bin = mysql_bin
binlog_do_db = lbqdb
binlog_ignore_db = mysql
重启成功。
mysql –u root –p
设置连接master:
mysql->change master to master_host='xxx.xxx.xxx.xxx',master_user='syncAccount',master_password='syncPassword', master_log_file='mysql-bin.000001',master_log_pos=446;
连接的账户 IP 密码为mastermysql的。master_log_file 和log_pos 为前期windows mysql的show master status 中看到。
出现OK之后
Start slave;启动slave
查看状态
Show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.3.4
Master_User: syncAccount
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 1682
Relay_Log_File: localhost-relay-bin.000002
Relay_Log_Pos: 1489
Relay_Master_Log_File: mysql-bin.000001
**Slave_IO_Running: Yes
Slave_SQL_Running: Yes**
Replicate_Do_DB:
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: 1682
Relay_Log_Space: 1649
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:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
1 row in set (0.00 sec)
重点关注这两个:
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
当slave_IO_Running 和Slave_SQL_Running 都为yes 说明配置成功。
测试了一下,在主数据库新建表并插入数据,从数据库也新建了表,里面也有对应数据。证明数据库已经同步了。
MySQL主从不同步问题解决
问题 描述:主从复制在某个时刻,从服务器同步失败,并给出报错消息。
消息内容:基本意思是缺少某个库的某个表
原因:主从服务长时间不同步,主库已经生成很多新的表或者库,而从库却没有。
操作过程:
(1)登陆主服务器,查看主服务器的状态
代码如下 复制代码
mysql>show master status;
(2)登陆从服务器,执行同步操作。
代码如下 复制代码
mysql>stop slave;mysql > change master to ...(此处省略);mysql > start slave;
(3)从服务器上查看状态
代码如下 复制代码
mysql > show slave status\G
看报错信息少什么表或库,少什么就直接从主服务器通过scp复制,然后重复过程(1)~(3)直到不报错为止