当业务流量过大时,咱们的一台服务器可能难以负载,咱们须要用到主从服务器的配置。html
具体配置以下:mysql
172.17.10.57位主服务器linux
172.17.55.206 从服务器sql
正确的安装数据库后,确保两台的能互通。数据库
1 主服务器创建账号
安全
mysql>GRANT REPLICATION SLAVE ON *.* to 'mysync'@'%' identified by 'q123456';服务器
//通常不用root账号,“%”表示全部客户端均可能连,只要账号,密码正确,此处可用具体客户端IP代替,如192.168.145.226,增强安全ide
先进行测试,看是否能正确的链接。对数据的权限管理也要清楚的了解。
测试
2主服务器配置以下:this
在/etc/my.cn
#vi /etc/my.cnf
[mysqld]
log-bin=mysql-bin //[必须]启用二进制日志
server-id=222 //[必须]服务器惟一ID,默认是1,通常取IP最后一段
三、修改从服务器slave:
#vi /etc/my.cnf
[mysqld]
log-bin=mysql-bin //[必须]启用二进制日志
server-id=226 //[必须]服务器惟一ID,默认是1,通常取IP最后一段
四、重启两台服务器的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;
七、检查从服务器复制功能状态:
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均属错误)。
以上操做过程,主从服务器配置完成。
总结:
1 首先须要检查主从关系的连通性
2 创建一个合理权限的帐户,只要给能复制的那个就行。
3 配置能复制过去的库或者表格,既能够在主服务器上配置,也能够在从服务器上配置。
制定了一个能够过去的,其他的就是不能够过去的。这个就像linux的黑白名单那同样。
建议在从服务器上进行配置
报错: 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进程。
参考博客
http://www.cnblogs.com/hustcat/archive/2009/12/19/1627525.html
三、双主从服务器复制 (1)第一台服务器设置auto_increment_increment = 2 \\数据表记录的标识增加数,通常等于服务器的数量auto_increment_offset = 1 \\数据表记录标识每次增长的数,通常第一台为1,第二台为2,以此类推sync_binlog = 0 \\二进制写入磁盘的同步方式 (2)第二台服务器配置auto_increment_increment = 2auto_increment_offset = 2sync_binlog = 0