4.1、 mysql复制的优点:
1、如果主服务器出现问题,可以快速的切换到从服务器进而提供服务
2、在从服务器上可以执行查询操作,降低主服务器的访问压力
3、可以在从服务器上执行备份操作,避免备份期间对主服务器的影响
4.2、 主服务器配置(192.168.202.139)
4.2.1 登陆mysql数据库
mysql -u root -p 123456 shanTest
4.2.2 给从服务器授权用户
grant all on *.* to 'user1'@192.168.202.135 identified by '123456';
4.2.3 修改主数据库服务 器的配置文件my.conf
log-bin=mysql-bin
server-id=1
4.2.4 查看主服务器对从服务器的授权
mysql> show grants for user1@192.168.202.135;
4.2.5 将主服务器上的数据备份文件复制到从服务器上
scp shanTest.sql 192.168.135/
4.3 从服务器的配置(192.168.202.135)
4.3.1 修改从数据库服务器的配置文件my.conf
server-id=2
log-bin=mysql-bin
master-host=192.168.202.139
master-user=user1
master-password=123456
master-port=3306
4.3.2 在从服务器上创建数据库
create database shanTest;
4.3.3 使用复制的shanTest.sql加载数据
mysql -uroot -p123456 shanTest
4.3.4 查看从服务的状态
show slave statusG;
结果:
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.202.139
Master_User: user1
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000008
Read_Master_Log_Pos: 247
Relay_Log_File: mysqld-relay-bin.000014
Relay_Log_Pos: 392
Relay_Master_Log_File: mysql-bin.000008
Slave_IO_Running: Yes #标识可以同步bin-log日志
Slave_SQL_Running: Yes # 标识可以执行sql
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: 247
Relay_Log_Space: 693
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:
No query specified
主从配置完成。
4.4 对从服务器的操作
4.4.1 停止从服务器
stop slave
4.4.2 开启从服务器
start slave
4.4.3 重置从服务器日志
stop slave
reset slave
4.4.4 Slave_IO_Running 为 NO
标识从服务器读取主服务器的bin-log日志失败,需要查看主服务器与从服务器的bin-log日志的版本及位置是否一致。
解决方案:
先stop slave,然后执行了一下提示的语句,再SET GLOBAL SQL_SLAVE_SKIP_COUNTER=1; START SLAVE;