Mysql配置主从复制

来源:互联网 发布:java随机读取数组的值 编辑:程序博客网 时间:2024/05/29 06:48
主服务器:centos6.5 192.168.32.33 mysql5.7
从服务器:centos6.5 192.168.32.34 mysql5.7
主服务器配置:
1、创建从服务器链接账号
mysql>grant replocation slave on *.* to 'rep34'@'192.168.32.34' identified by 'rep34'
mysql>flush privileges
2.1、编辑my.inf文件(位置一般在/etc或者/etc/mysql下)
[mysqld]
#设置服务器ID
server-id=33
#设置binlog文件位置
log-bin=/var/lib/mysql/mysql-bin
#设置更新记录的操作会记录到binlog中
log-slave-updates=1
#以上三个配置为必填
#设置需要备份的数据库,如果有多个数据库重复设置
binlog-do-db=test
#设置不需要备份的数据库,如果有多个数据库重复设置
binlog-ignore-db=test2
#跳过错误
slave-skip-errors=1

2.2
重启数据库
service mysqld restart
2.3查询binlog日志文件(File)的文件名与偏移量(Position),这两个变量在配置从数据库时需要用到
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000002 | 3034 | test | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
2.4备份需要用到的数据库
[root@localhost /]#mysqldump -u root -p test > /data/test.sql
小技巧:备份数据库时,最好将数据库设置读锁,保证数据库在备份的过程中状态不会进行改变
#加锁
mysql>flush tables with read lock;、
#解锁
mysql>unlock tables;

从数据库配置:
1 将主数据库备份的数据恢复到从数据库中,可通过source xx.sql命令进行数据库脚本的执行
2.1 编辑my.cnf文件,添加数据库id并重启数据库
[mysqld]
server-id=34
2.2 对从数据库进行设置
mysq>stop slave;
mysq>change master to master_host='192.168.32.33',master_user='rep34',master_password='rep34',master_log_file='mysql-bin.000002',master_log_pos=154;
mysq>start slave;
参数解析:
master_host:主数据库的IP
master_user:主数据库配置给从数据库链接用的用户名
master_password:用户密码
master_log_file:binlog文件名,通过在主数据库中使用show matser status;命令查看
master_log_pos:binlog偏移量,查询方法同master_log_file

2.3 查看从slave运行情况
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.32.33
Master_User: rep34
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 3034
Relay_Log_File: localhost-relay-bin.000003
Relay_Log_Pos: 594
Relay_Master_Log_File: mysql-bin.000002
Slave_IO_Running: Yes
Slave_SQL_Running: 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: 3034
Relay_Log_Space: 3577
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:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 33
Master_UUID: 12d7e610-8497-11e7-ab9e-0050563aff6b
Master_Info_File: /var/lib/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
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
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
 
ERROR:
No query specified
主要关注参数:
Slave_IO_State:slave的运行状态,当为“Waitingfor master to send event”时表示正常
Slave_IO_Running:当为YES时表示正常,当为NO时可通过Last_IO_Error参数查看出错原因,并针对性的解决
Slave_SQL_Running:当为YES时表示正常,当为NO时可通过Last_SQL_Error参数查看出错原因,并针对性的解决
Last_IO_Error:IO出现问题时的原因
Last_SQL_Error:SQL出现问题时的原因

本文章仅用于个人学历记录用,有哪里出错了请各位大神指出