您的位置:首页 > 数据库 > MySQL

mysql 主从同步的工作原理和配置

2015-08-03 00:22 441 查看
MySQL复制(Replication)工作原理:Slave端通过将Master端的binlog利用I/O线程通过网络复制到Slave端,然后再通过SQL线程解析Binlog中的日志并应用到数据库中来实现。
公共配置:
安装完软件包后启动服务(2台都是一下操作)
[root@localhost mysql]# cat /root/.mysql_secret 查看默认的MYSQL密码
# The random password set for the root user at Sun Aug 2 18:40:27 20 15 (local time): huGTgnSX

[root@localhost mysql]# cd
[root@localhost ~]# mysql -hlocalhost -uroot -phuGTgnSX 登陆服务
Warning: Using a password on the command line interface can be insecu re.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.15

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights re served.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input s tatement.

mysql> set password for root@localhost=password("123"); 修改服务的密码
Query OK, 0 rows affected (0.03 sec)

mysql> quit
Bye

物理环境

master 192.168.1.60
slave 192.168.1.70

master端的配置
mysql> grant replication slave on *.* to slave70@"192.168.1.70" identified by "123";
Query OK, 0 rows affected (0.02 sec)

编辑master的主配置文件
[root@fanxh ~]# vim /etc/my.cnf

[mysqld]
log_bin=master 启动binlog日志
server_id = 60 给master添加一个 编号
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql

[root@localhost ~]# service mysql restart 主配置文件配置完成后从新启动服务

Shutting down MySQL.. [确定]
Starting MySQL.
[root@localhost ~]# mysql -hlocalhost -uroot -p123 从新登陆系统
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.15-log MySQL Community Server (GPL)

mysql> show master status; 查看master的状态
+---------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------+----------+--------------+------------------+-------------------+
| master.000001 | 120 | | | |
+---------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

3.slave配置
[root@localhost ~]# mysql -h192.168.1.60 -uslave70 -p123 首先测试授权的用户能不能登陆
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.15-log MySQL Community Server (GPL)

修改slave主配置 文件
[root@fanxh ~]# vim /etc/my.cnf

[mysqld]
log_bin=slave
srever_id = 70
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
[root@localhost ~]# service mysql restart 更改配置文件后要从新启动服务

[root@localhost ~]# mysql -hlocalhost -uroot -p123 登陆mysql设置自己是那个服务器的slave
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.6.15 MySQL Community Server (GPL)

mysql> show slave status; 查看slave的状态
Empty set (0.02 sec)
mysql> change master to
-> master_host="192.168.1.60",
-> master_user="slave70",
-> master_password="123",
-> master_log_file="master.000001", master_log_file的值在master用show master status 看到的值必须一致
-> master_log_pos=120; / master_log_pos的值在master用show master status 看到的值必须一致
Query OK, 0 rows affected, 2 warnings (0.12 sec)
4) 验证从服务器设置是否成功,登录从服务器
mysql〉show slave status\G;
能看到 Slave_IO_Running: Yes
Slave_SQL_Running: Yes 即可
mysql> mysql> show slave status\G 查看状态
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: 192.168.1.60
Master_User: slave70
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master.000003
Read_Master_Log_Pos: 120
Relay_Log_File: localhost-relay-bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: master.000003
Slave_IO_Running: No 这个值和下面的值同时为YES才能同步数据
Slave_SQL_Running: No 这个值为和上面的值同时为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: 120
Relay_Log_Space: 120
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: NULL
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: 0
Master_UUID:
Master_Info_File: /var/lib/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State:
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
1 row in set (0.00 sec)

mysql> start slave; 启动执行命令
Query OK, 0 rows affected (0.01 sec)

mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.60
Master_User: slave70
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master.000003
Read_Master_Log_Pos: 120
Relay_Log_File: localhost-relay-bin.000002
Relay_Log_Pos: 280
Relay_Master_Log_File: master.000003
Slave_IO_Running: Yes
Slave_SQL_Running: Yes

mysql主从同步常见故障问题:
1)给master授权slave主机登陆权限,在登陆的时候出错 编辑master的配置文件在[mysqld]下面添加
skip-name-resolve
2)在从服务器上面show slave status\G;出现1236错误排错办法:
重启主库
给从库重新授权
执行以下命令
slave stop;
reset slave;
slave start;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息