mysql双机热备
?
mysql>grant replication slave on *.* to '用户名'@'从服务器ip' identified by '密码';
?
mysql>grant replication slave on *.* to '用户名'@'主服务器ip' identified by '密码';?
测试是否能够连接:
主服务器:mysql -h从服务器ip -u用户名(从服务器上创建) -p密码
如果能进到mysql下说明连接成功,如果报错,cann't connect to 从服务器ip ,大致是这样,是因为,从服务器上的
防火墙限制了连接,要在防火墙里设置例外,添加主服务器ip以及所访问从服务器mysql的端口号。
从服务器同样。
?
server-id=1(主服务器ID,不能重复)binlog-do-db=labostest(要复制的库)log-bin="C:/mysql/log"(log文件存放处,最后的参数表明日志文件名,replication是基于log来实现复制的)#replication slavemaster-host=192.168.1.108(设置从服务器ip)master-port=3306master-user=backup(在从服务器上创建的用户名)master-password=123456(对应密码)replicate-do-db=labostest(要复制的库)replicate-do-db=labostest1(要复制的另一个数据库)master-connect-retry=60(连接时间)?
?
server-id=2(从服务器ID,不能为1)binlog-do-db=labostest(要复制的库)log-bin="C:/mysql/log"(log文件存放处,最后的参数表明日志文件名,replication是基于log来实现复制的)#replication slavemaster-host=192.168.1.103(设置从服务器ip)master-port=3306master-user=backup(在主服务器上创建的用户名)master-password=123456(对应密码)replicate-do-db=labostest(要复制的库)replicate-do-db=labostest1(要复制的另一个数据库)master-connect-retry=60(连接时间)
?
重新启动两台机器的mysql服务
三、查看服务器状态
?
重启服务之后,查看日志文件夹下是否生成了文件,如果是则已经加载成功,否则,并未加载以上的配置信息,
可能是修改的配置文件并不是启动mysql时要加载的配置文件,很可能在C:/windows下
修改之后,重启服务,直到日志文件夹下生成文件,否则show master status时,会一直是空集。
*************************主服务器****************************
?
show master status\G
? mysql> show master status\G;
+---------------+----------+--------------+------------------+| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |+---------------+----------+--------------+------------------+| mysql-bin.003 | 73 | test | manual,mysql |+---------------+----------+--------------+------------------+
?切换到从服务器下,,通过下面命令来设置从服务器的参数
?? 如果在配置文件里设置可以不用执行以下命令,直接 slave start;
?? 首先停止slave服务
?? ?mysql> slave stop; //停止slave的服务
???
mysql> CHANGE MASTER TO-> MASTER_HOST='master_host_name', //主服务器的IP地址-> MASTER_USER='replication_user_name', //同步数据库的用户-> MASTER_PASSWORD='replication_password', //同步数据库的密码-> MASTER_LOG_FILE='recorded_log_file_name', //主服务器二进制日志的文件名(前面要求记住的参数)-> MASTER_LOG_POS=recorded_log_position; //日志文件的开始位置(前面要求记住的参数)
??启动同步数据库的线程
?
?
mysql> slave start;
?
*************************从服务器******************************
?
?
?
show master status\G
? mysql> show master status\G;
+---------------+----------+--------------+------------------+| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |+---------------+----------+--------------+------------------+| mysql-bin.003 | 73 | test | manual,mysql |+---------------+----------+--------------+------------------+
?切换到主服务器下,通过下面命令来设置主服务器的参数
? 如果在配置文件里设置可以不用执行以下命令,直接 slave start;
?? 首先停止slave服务
?? ?mysql> slave stop; //停止slave的服务
???
mysql> CHANGE MASTER TO-> MASTER_HOST='master_host_name', //从服务器的IP地址-> MASTER_USER='replication_user_name', //同步数据库的用户-> MASTER_PASSWORD='replication_password', //同步数据库的密码-> MASTER_LOG_FILE='recorded_log_file_name', //从服务器二进制日志的文件名(上面的参数)-> MASTER_LOG_POS=recorded_log_position; //从服务器日志文件的开始位置(上面的参数)
?
***********************分别在从服务器和主服务器上查看************************
?启动同步数据库的线程
?
?
mysql> slave start;
?
?
查看数据库的同步情况,直到成功。
?
mysql> show slave status\G*************************** 1. row ************************** Slave_IO_State: Waiting for master to send event Master_Host: 202.xx.xx.xx Master_User: backup Master_Port: 3306 Connect_Retry: 60 Master_Log_File: logbin.000002 Read_Master_Log_Pos: 930 Relay_Log_File: idc-ojf9p55rkb5-relay-bin.000009 Relay_Log_Pos: 1064 Relay_Master_Log_File: logbin.000002 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: backuptest 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: 930 Relay_Log_Space: 1064 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: 01 row in set (0.00 sec)?
?
注意红色的,如果是这样,代表成功了.
主要是
?
- ??Slave_IO_Running:?Yes??
- ??Slave_SQL_Running:?Yes ?
?
show slave status\G
show master status\G
