mysql-主从搭建
Posted 砖头哥-
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql-主从搭建相关的知识,希望对你有一定的参考价值。
环境准备
主机名 |
IP |
系统/MySQL版本 |
角色 |
node3 |
192.168.210.132 |
CentOS7.3/5.5.60 |
Master |
node2 |
192.168.210.130 |
CentOS7.3/5.5.60 |
slave |
一、master节点
1、创建需要同步的数据库
mysql> create database zn; mysql> use zn; mysql> create table T1(id int,name varchar(20));
2、停止数据库服务
systemctl stop mariadb.service
3、编辑my.cnf
[mysqld]
log-bin=mysql-bin-master #启用二进制日志 server-id=1 #本机数据库ID 标示 binlog-do-db=zn #可以被从服务器复制的库, 二进制需要同步的数据库名 binlog-ignore-db=mysql #不可以被从服务器复制的库
4、重启数据库服务
systemctl restart mariadb.service
5、创建同步用户并授权
MariaDB [(none)]> grant replication slave on *.* to slave@‘%‘ identified by "123"; MariaDB [(none)]> show master status;
+-------------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------------+----------+--------------+------------------+
| mysql-bin-master.000001 | 384 | zn | mysql |
+-------------------------+----------+--------------+------------------+
MariaDB [(none)]>
MariaDB [(none)]>
MariaDB [(none)]> show binlog eventsG
*************************** 1. row ***************************
Log_name: mysql-bin-master.000001
Pos: 4
Event_type: Format_desc
Server_id: 1
End_log_pos: 245
Info: Server ver: 5.5.60-MariaDB, Binlog ver: 4
*************************** 2. row ***************************
Log_name: mysql-bin-master.000001
Pos: 245
Event_type: Query
Server_id: 1
End_log_pos: 384
Info: grant replication slave on *.* to slave@‘%‘ identified by "123"
2 rows in set (0.00 sec)
6、查看二进制文件
[root@node3 my.cnf.d]# cd /var/lib/mysql/ [root@node3 mysql]# ls[root@node3 mysql]# ls
aria_log.00000001 ibdata1 ib_logfile1 mysql-bin-master.000001 mysql.sock test
aria_log_control ib_logfile0 mysql mysql-bin-master.index performance_schema zn
7、将要同步的数据库导出,并发送给slave节点,复制前要保证同步的数据库一致
mysqldump -uroot -p123 zn >zn.sql #可以导出数据库
scp zn.sql 192.168.210.130:/root
二、slave节点
1、两台数据库服务器mysql版本要一致
MariaDB [(none)]> show variables like ‘%version%‘; +-------------------------+----------------------+ | Variable_name | Value | +-------------------------+----------------------+ | innodb_version | 5.5.59-MariaDB-38.11 | | protocol_version | 10 | | slave_type_conversions | | | version | 5.5.60-MariaDB | | version_comment | MariaDB Server | | version_compile_machine | x86_64 | | version_compile_os | Linux | +-------------------------+----------------------+ 7 rows in set (0.00 sec) MariaDB [(none)]>
2、测试连接到主服务器是否成功,只有复制的权限, 是看不到其他库的
[root@node2 ~]# mysql -uslave -p123 -h 192.168.210.132 MariaDB [(none)]> MariaDB [(none)]> MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | test | +--------------------+ 2 rows in set (0.00 sec) MariaDB [(none)]>
3、正常导入数据库,和主数据库服务器保持一致
[root@node2 ~]# mysql -u root MariaDB [(none)]> create database zn; Query OK, 1 row affected (0.00 sec) MariaDB [(none)]> exit [root@node2 ~]# mysql -uroot zn<zn.sql
4、从服务器没必要开启bin-log日志,修改从服务器配置文件:
[root@node2 ~]# systemctl stop mariadb.service [root@node2 ~]# vim /etc/my.cnf [mysqld] server-id=2 #从服务器ID号,不要和主ID相同 ,如果设置多个从服务器,每个从服务器必须有一个唯一的server-id值,必须与主服务器的以及其它从服务器的不相同。可以认为server-id值类似于IP地址:这些ID值能唯一识别复制服务器群集中的每个服务器实例。
5、停止slave服务,设置master节点ip,master_user同步用户和密码
[root@node2 ~]# mysql -u root MariaDB [(none)]> stop slave; Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> MariaDB [(none)]> change master to master_host=‘192.168.210.132‘,master_user=‘slave‘,master_password=‘123‘; Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> MariaDB [(none)]> start slave; Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]>MariaDB [(none)]> show slave statusG
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.210.132
Master_User: slave
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin-master.000001
Read_Master_Log_Pos: 384
Relay_Log_File: mariadb-relay-bin.000002
Relay_Log_Pos: 675
Relay_Master_Log_File: mysql-bin-master.000001
Slave_IO_Running: Yes #负责与主机的io通信
Slave_SQL_Running: Yes #负责自己的slave mysql进程
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: 384
Relay_Log_Space: 971
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)]>
三、maste节点
(1)再到主服务器上查看状态:
[root@node3 mysql]# mysql -u root
MariaDB [(none)]> show processlistG *************************** 1. row *************************** Id: 11 User: slave Host: node2:37878 db: NULL Command: Binlog Dump Time: 343 State: Master has sent all binlog to slave; waiting for binlog to be updated Info: NULL Progress: 0.000
(2)同步测试插入数据测试同步:
MariaDB [zn]> select * from T1; Empty set (0.00 sec)
MariaDB [zn]>
MariaDB [zn]> insert into T1 values (1,‘man‘);
Query OK, 1 row affected (0.00 sec)
MariaDB [zn]>
MariaDB [zn]> select * from T1;
+------+------+
| id | name |
+------+------+
| 1 | man |
+------+------+
1 row in set (0.00 sec)
MariaDB [zn]>
四、slave节点验证数据是否同步
MariaDB [zn]> show tables; +-----------------------+ | Tables_in_zn | +-----------------------+ | T1 | | wp_commentmeta | | wp_comments | | wp_links | | wp_options | | wp_postmeta | | wp_posts | | wp_term_relationships | | wp_term_taxonomy | | wp_terms | | wp_usermeta | | wp_users | +-----------------------+ 12 rows in set (0.00 sec) MariaDB [zn]> MariaDB [zn]> select * from T1; +------+------+ | id | name | +------+------+ | 1 | man | +------+------+ 1 row in set (0.01 sec) MariaDB [zn]>
五、如果遇到主从不同步修复
(1)在master节点,看一下主从bin-log的位置,然后再同步
MariaDB [(none)]> show master status;
+-------------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------------+----------+--------------+------------------+
| mysql-bin-master.000001 | 384 | zn | mysql |
+-------------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
MariaDB [(none)]> show binlog eventsG
*************************** 1. row ***************************
Log_name: mysql-bin-master.000001
Pos: 4
Event_type: Format_desc
Server_id: 1
End_log_pos: 245
Info: Server ver: 5.5.60-MariaDB, Binlog ver: 4
*************************** 2. row ***************************
Log_name: mysql-bin-master.000001
Pos: 245
Event_type: Query
Server_id: 1
End_log_pos: 384
Info: grant replication slave on *.* to slave@‘%‘ identified by "123"
2 rows in set (0.00 sec)
MariaDB [(none)]>
(2)slave节点执行MySQL命令下:
MariaDB [(none)]> stop slave; Query OK, 0 rows affected (0.01 sec) MariaDB [(none)]> MariaDB [(none)]> change master to master_log_file=‘mysql-bin-master.000002‘,master_log_pos=245; Query OK, 0 rows affected (0.01 sec) #根据上面主服务器的show master status的结果,进行从服务器的二进制数据库记录回归,达到同步的效果 MariaDB [(none)]> MariaDB [(none)]> slave start ; Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> show slave statusG Slave_IO_Running: Yes #如果都是yes,那代表已经在同步 Slave_SQL_Running: Yes
以上是关于mysql-主从搭建的主要内容,如果未能解决你的问题,请参考以下文章