mysql 主从复制配置详解
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql 主从复制配置详解相关的知识,希望对你有一定的参考价值。
主从复制模型配置过程;
备注: 主节点IP地址是192.168.1.106 从节点的ip地址是192.168.1.107;
主节点:
(1)启动二进制日志;
1-1 编辑配置文件 vim /etc/my.cnf配置如下所示;
[mysqld]
log-bin=master-bin
server-id=1
innodb-file-per-table=ON
skip_name_resolve=ON
systemctl start mariadb.service
1-2 进入mysql使用 mysql命令
1-3 show global variables like ‘%log%‘; 查看一下日志文件是否已经打开,如下状态;
| log_bin | ON
1-4 show master logs;查看一下主节点日志
MariaDB [(none)]> show master logs;
+------------------+-----------+
| Log_name | File_size |
+------------------+-----------+
| mysql-bin.000001 | 16739 |
| mysql-bin.000002 | 516 |
| mysql-bin.000003 | 245 |
+------------------+-----------+
1-5show global variables like ‘%server%‘;
MariaDB [(none)]> show global variables like ‘%server%‘;
+----------------------+-------------------+
| Variable_name | Value |
+----------------------+-------------------+
| character_set_server | latin1 |
| collation_server | latin1_swedish_ci |
| server_id | 1 |
+----------------------+-------------------+
(2)为当前节点设置一个全局唯一的ID号;
此配置已经在第一步的时候设置完成了,这里在注明一下
编辑配置文件vim /etc/my.cnf
[mysqd]
server-id=1
(3)创建有复制权限的用户账号;
grant replication slave,replication client on *.* to ‘repluser‘@‘192.168.%.%‘ identified by ‘replpass‘;
flush privileges;
MariaDB [(none)]> grant replication slave,replication client on *.* to ‘repluser‘@‘192.168.%.%‘ identified by ‘replpass‘;
Query OK, 0 rows affected (0.01 sec)
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
从节点;
(1)启动中继日志;
1-1编辑配置文件 vim /etc/my.cnf
[mysqld]
relay-log=relay-log
server-id=7
relay_log_index=relay-log.index
innodb_file_per_table=ON
skip_name_resolve=ON
systemctl start mariadb
mysql
1-2查看一下中继日志是否已经启动show global variables ‘%log%‘; 如下所示
| relay_log | relay-log
1-3查看一下server id 如下所示;
MariaDB [(none)]> show global variables like ‘%server%‘;
+----------------------+-------------------+
| Variable_name | Value |
+----------------------+-------------------+
| character_set_server | latin1 |
| collation_server | latin1_swedish_ci |
| server_id | 7 |
+----------------------+-------------------+
(2)为当前节点设置一个全局唯一的ID号;
编辑配置文件 vim /etc/my.cnf
[mysqld]
server-id=7
设置自己是从节点(备注: 从节点不能进行写操作)
(3)使用有复制权限的用户账号连接至主服务器,并启动复制线程;
3-1MariaDB [(none)]> change master to master_host=‘192.168.1.106‘, master_user=‘repluser‘, master_password=‘replpass‘, master_log_file=‘mysql-bin.000003‘, master_log_pos=245;
MariaDB [(none)]> change master to master_host=‘192.168.1.106‘, master_user=‘repluser‘, master_password=‘replpass‘, master_log_file=‘mysql-bin.000007‘, master_log_pos=245;
Query OK, 0 rows affected (0.03 sec)
然后将其从服务器的复制状态打开 slave start ;
MariaDB [(none)]> slave start;
Query OK, 0 rows affected (0.00 sec)
可以查看状态:show slave status; 状态如下所示;
MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: 192.168.1.106
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-bin.000003
Read_Master_Log_Pos: 245
Relay_Log_File: relay-log.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: master-bin.000003
Slave_IO_Running: No
Slave_SQL_Running: No
3-2启动从节点的复制状态;如下所示 ;
MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.02 sec)
MariaDB [(none)]> SHOW SLAVE STATUS\G 查看一下状态是否启动了、如下所示;
MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.106
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000007
Read_Master_Log_Pos: 328
Relay_Log_File: relay-log.000002
Relay_Log_Pos: 612
Relay_Master_Log_File: mysql-bin.000007
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: 328
Relay_Log_Space: 900
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: 1
1 row in set (0.00 sec)
3-3 进行最后的测试工作(备注:因为主服务器才有写权限,然后就在主服务器上面建一个数据库,看是否能够同步到从服务器上面来)如果能,我们的配置就成功了,如下所示;
先在主服务器上面操作如下;
MariaDB [(none)]> create database zhongguo;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000007 | 419 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| data |
| hellodb |
| li |
| mysql |
| performance_schema |
| test |
| zhongguo |
+--------------------+
8 rows in set (0.00 sec) ( 主服务器创建了一个zhongguo的数据库)
然后我们在从服务器上面进行查看一下;如下所示
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| data |
| hellodb |
| mysql |
| performance_schema |
| test |
| zhongguo |
+--------------------+
以上的配置没有问题,从服务器已经可以读取主服务器的数据了,OK,我们的配置成功了。
备注:出现的问题及解决办法
160605 9:19:59 [ERROR] Slave I/O: Got fatal error 1236 from master when reading data from binary log: ‘Could not find first log file name in binary log index file‘, Error_code: 1236160605 9:19:59 [Note] Slave I/O thread exiting, read up to log ‘master-bin.000009‘, position 245
Slave_IO_Running: No (这个问题按照如下的方法可以进行解决)
这个问题的解决办法是将从服务器的授权从新做一边
1.停止从服务器
stop slave;
reset slave ; 这一步时一点要执行的。
2.执行授权
MariaDB [(none)]> change master to master_host=‘192.168.1.106‘, master_user=‘repluser‘, master_password=‘replpass‘, master_log_file=‘mysql-bin.000007‘, master_log_pos=245;
3.然后进行启动从服务器;
MariaDB [(none)]> slave start;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.106
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000007
Read_Master_Log_Pos: 245
Relay_Log_File: relay-log.000002
Relay_Log_Pos: 529
Relay_Master_Log_File: mysql-bin.000007
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: 245
Relay_Log_Space: 817
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: 1
1 row in set (0.00 sec)
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| hellodb |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.00 sec)
MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.106
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000007
Read_Master_Log_Pos: 328
Relay_Log_File: relay-log.000002
Relay_Log_Pos: 612
Relay_Master_Log_File: mysql-bin.000007
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: 328
Relay_Log_Space: 900
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: 1
1 row in set (0.00 sec)
本文出自 “李文明的博客” 博客,请务必保留此出处http://liwenming18.blog.51cto.com/11068518/1786374
以上是关于mysql 主从复制配置详解的主要内容,如果未能解决你的问题,请参考以下文章