当业务流量过大时,我们的一台服务器可能难以负载,我们需要用到主从服务器的配置。
具体配置如下:
172.17.10.57位主服务器
172.17.55.206 从服务器
正确的安装数据库后,确保两台的能互通。
mysql>GRANT REPLICATION SLAVE ON *.* to ‘mysync‘@‘%‘ identified by
‘q123456‘;
//一般不用root帐号,“%”表示所有客户端都可能连,只要帐号,密码正确,此处可用具体客户端IP代替,如192.168.145.226,加强安全
先进行测试,看是否能正确的连接。对数据的权限管理也要清楚的了解。
主服务器配置如下:
在/etc/my.cn
#vi /etc/my.cnf
[mysqld]
log-bin=mysql-bin //[必须]启用二进制日志
server-id=222 //[必须]服务器唯一ID,默认是1,一般取IP最后一段
3、修改从服务器slave:
#vi /etc/my.cnf
[mysqld]
log-bin=mysql-bin //[必须]启用二进制日志
server-id=226 //[必须]服务器唯一ID,默认是1,一般取IP最后一段
4、重启两台服务器的mysql
/etc/init.d/mysql restart
5 查看状态
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000006 | 331 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
6 从服务器连接主服务器
mysql> change master to master_host=‘172.17.10.57‘, -> master_user=‘test‘, -> master_password=‘passwd‘, -> master_log_file=‘mysql-bin.000006‘; mysql> start slave mysql> show slave status\G;
mysql> change master to master_host = ‘172.17.10.57‘,master_user = ‘mysync‘,master_password=‘q123456‘,master_log_file=‘mysql-bin.000019‘,master_log_pos=120;
8、检查从服务器复制功能状态:
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.2.222 //主服务器地址
Master_User: myrync //授权帐户名,尽量避免使用root
Master_Port: 3306 //数据库端口,部分版本没有此行
Connect_Retry: 60
Master_Log_File: mysql-bin.000004
Read_Master_Log_Pos: 600 //#同步读取二进制日志的位置,大于等于>=Exec_Master_Log_Pos
Relay_Log_File: ddte-relay-bin.000003
Relay_Log_Pos: 251
Relay_Master_Log_File: mysql-bin.000004
Slave_IO_Running: Yes //此状态必须YES
Slave_SQL_Running: Yes //此状态必须YES
......
注:Slave_IO及Slave_SQL进程必须正常运行,即YES状态,否则都是错误的状态(如:其中一个NO均属错误)。
以上操作过程,主从服务器配置完成。
如果出现错误记得来这里找
mysql 互为主从复制常见问题
来源: 作者: 发表于:2012-08-19 17:15 点击:
报错: 1) change
master导致的: Last_IO_Error: error connecting to master - retry-time: 60
retries 2) 在没有解锁的情况下停止slave进程: stop slave; ERROR 1192 (HY000): Cant
execute the given command because you have active locked tables
报错:
1)
change master导致的:
Last_IO_Error: error connecting to master - retry-time: 60 retries
2)
在没有解锁的情况下停止slave进程:
> stop slave;
ERROR 1192 (HY000): Can‘t execute the given command because you have active locked tables or an active transaction
3)
change master语法错误,落下逗号
mysql> change master to
-> master_host=‘IP‘
-> master_user=‘USER‘,
-> master_password=‘PASSWD‘,
-> master_log_file=‘mysql-bin.000002‘,
-> master_log_pos=106;
ERROR 1064 (42000): You have an error in your SQL syntax; check the
manual that corresponds to your MySQL server version for the right
syntax to use near ‘master_user=‘USER‘,
master_password=‘PASSWD‘,
master_log_file=‘mysql-bin.000002‘ at line 3
4)
在没有停止slave进程的情况下change master
mysql> change master to master_host=‘IP‘, master_user=‘USER‘,
master_password=‘PASSWD‘,
master_log_file=‘mysql-bin.000001‘,master_log_pos=106;
ERROR 1198 (HY000): This operation cannot be performed with a running slave; run STOP SLAVE first
5)
A B的server-id相同:
Last_IO_Error: Fatal error: The slave I/O thread stops because master and slave have equal MySQL server ids;
these ids must be different for replication to work (or the --replicate-same-server-id option must be used on
slave but this does not always make sense; please check the manual before using it).
查看server-id
mysql> show variables like ‘server_id‘;
手动修改server-id
mysql> set global server_id=2; #此处的数值和my.cnf里设置的一样就行
mysql> slave start;
6)change master之后,查看slave的状态,发现slave_IO_running 为NO
需要注意的是,做完上述操作之后最后重启mysql进程。