centos7环境mysql5.7主备
一、mysql安装
1、下载mysql二进制包
wget http://mirrors.163.com/mysql/Downloads/MySQL-5.7/mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz
2、解压缩
tar -zxvf mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz
mv mysql-5.7.23-linux-glibc2.12-x86_64 /usr/local/mysql
3、修改配置文件
vi /etc/my.cnf
[mysqld]
port=3306
character-set-server=utf8
basedir=/usr/local/mysql
datadir=/data
#innodb_buffer_pool_size=8M
[mysqld_safe]
log-error=/data/error.log
pid-file=/data/mysql.pid
tmpdir = /tmp
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
4、创建用户及用户组
添加mysql用户组
#groupadd mysql
给mysql组下添加mysql用户
#useradd -r -g mysql -s /sbin/nologin mysql
改变文件所有者和所属组
#chown -R mysql /usr/local/mysql/
#chgrp -R mysql /usr/local/mysql/
5、卸载mariadb
#列出所有被安装的rpm package
# rpm -qa | grep maria*
mariadb-libs-5.5.56-2.el7.x86_64
# yum -y remove mari*
已加载插件:fastestmirror
正在解决依赖关系
--> 正在检查事务
---> 软件包 mariadb-libs.x86_64.1.5.5.56-2.el7 将被 删除
......
...
删除:
mariadb-libs.x86_64 1:5.5.56-2.el7
作为依赖被删除:
postfix.x86_64 2:2.10.1-6.el7
完毕!
# rm -rf /var/lib/mysql/*
6、将mysql放入系统服务,并修改对应文件
[[email protected] support-files]# cp mysql.server /etc/init.d/mysqld
7、初始化安装
yum install libaio -y
[[email protected] /]# mkdir /data
[[email protected] /]# chown -R mysql:mysql /data
[[email protected] bin]# ./mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data
出现上图,说明已经安装成功!
8、启动mysql
[[email protected] bin]# service mysqld start
9、登陆mysql并输入5.7初始化时打印的密码:
10、修改root密码
mysql>SET PASSWORD FOR 'root'@localhost=PASSWORD('123456');
11、设置root允许远程连接
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> update user set host='%' where user='root' limit 1;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
12、mysql加入环境变量并设置mysql为自启服务
[[email protected] bin]# vi /etc/profile
添加到最后
export MYSQL_HOME=/usr/local/mysql
export PATH=$MYSQL_HOME/bin:$PATH
[[email protected] bin]# source /etc/profile (立即生效)
[[email protected] bin]# chmod +x /etc/rc.d/init.d/mysqld (添加可执行权限)
[[email protected] bin]# chkconfig --add mysqld (添加到开机自启)
---------------------
作者:JetBrains_孙健
来源:****
原文:https://blog.****.net/sj349781478/article/details/84875273
版权声明:本文为博主原创文章,转载请附上博文链接!
二、配置主备模式
MySQL主从原理:
一,master记录二进制日志,在每个事务更新数据完成之前,master在二进制日志中记录这些改变、mysql将事务写入二进制日志,即使事务中的语句都是交叉执行的。在事件写入二进制日志完成后,master通知存储引擎提交事务。
二,slave将master的binary log拷贝到它自己的中继日志。首先,slave开始一个工作线程——I/O线程。I/O线程在master上打开一个普通的连接,然后开始binlog dump process。Binlog dump process从master的二进制日志中读取事件,如果已经执行完master产生的所有文件,它会睡眠并等待master产生新的事件。I/O线程将这些事件写入中继日志。
三,SQL slave thread(SQL从线程)处理该过程的最后一步。SQL线程从中继日志读取事件,并重新执行其中的事件而更新slave的数据,使其与master中的数据一致。
主从配置:
主机名 IP 系统版本 mysql版本 角色
mysqlmaster.cn 10.10.10.69 CentOS Linux release 7.4.1708 (Core) 5.7.20 master
mysqlslave.cn 10.10.10.72 CentOS Linux release 7.4.1708 (Core) 5.7.20 slave
步骤一、主从服务环境初始化
[[email protected] ~]# iptables -F
[[email protected] ~]# systemctl stop firewalld
[[email protected] ~]# systemctl disable firewalld
[[email protected] ~]# systemctl stop NetworkManager
[[email protected] ~]# systemctl disable NetworkManager
步骤二,安装mysql
根据第一部分内容在两个节点安装好mysql
步骤三,启动MySQL服务,并进行MySQL服务初始化(主从服务器均进行同样操作)
根据第一部分内容在两个节点安装好mysql
步骤四、登录MySQL主服务器,创建一个测试数据库及表,并创建一个授权账号进行主从数据同步
[[email protected] ~]# mysql -uroot -p'[email protected]' 使用新密码登录mysql
mysql: [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 6
Server version: 5.7.20 MySQL Community Server (GPL)
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
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 statement.
mysql>
mysql> show databases; 查看当前数据库
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
mysql> create database test; 创建一个名为test的数据库
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| test | 新建数据库
+--------------------+
5 rows in set (0.00 sec)
mysql> use test; 切换到test数据库
Database changed
mysql> show tables; 查询当前数据库表
Empty set (0.00 sec)
mysql> create table test1(id int,name varchar(20)); 创建一个测试表
Query OK, 0 rows affected (0.03 sec)
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| test1 |
+----------------+
1 row in set (0.00 sec)
创建授权账号:slave 指定从服务器IP:10.10.10.72 密码:[email protected]*qw92!derS
mysql> grant replication slave on *.* to [email protected] identified by "[email protected]*qw92!derS";
Query OK, 0 rows affected, 1 warning (0.00 sec)
步骤五、主从服务器配置:
修改master系统配置文件 /etc/my.cnf 在mysqld下面加入 以下内容
log-bin=mysql-bin-master 启用二进制日志
server-id=1 本机数据库ID 标示
binlog-do-db=test 可以被从服务器复制的库, 二进制需要同步的数据库名(创建的测试数据库)
binlog-ignore-db=mysql 不可以被从服务器复制的库
[[email protected] ~]# systemctl restart mysqld 重启数据库服务器
[[email protected] ~]# mysql -uroot -p'[email protected]' 登录数据库
mysql: [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 3
Server version: 5.7.20-log MySQL Community Server (GPL)
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
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 statement.
mysql>
mysql> show master status; 查看数据库状态信息
+-------------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------------+----------+--------------+------------------+-------------------+
| mysql-bin-master.000001 | 154 | test | mysql | |
+-------------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
mysql> show binlog events\G
*************************** 1. row ***************************
Log_name: mysql-bin-master.000001
Pos: 4
Event_type: Format_desc
Server_id: 1
End_log_pos: 123
Info: Server ver: 5.7.20-log, Binlog ver: 4
*************************** 2. row ***************************
Log_name: mysql-bin-master.000001
Pos: 123
Event_type: Previous_gtids
Server_id: 1
End_log_pos: 154
Info:
2 rows in set (0.00 sec)
如上所示,master服务器已配置成功
mysqldump -uroot -p'[email protected]' test >test.sql 导出master测试数据库test
[[email protected] ~]# mysqldump -uroot -p'[email protected]' test >test.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[[email protected] ~]# ll test.sql
-rw-r--r-- 1 root root 1790 5月 13 00:01 test.sql
将导出的数据库传到slave服务器上
[[email protected] ~]# scp test.sql 10.10.10.72:/root
The authenticity of host '10.10.10.72 (10.10.10.72)' can't be established.
ECDSA key fingerprint is SHA256:cHQticA8/IMXFPFXspEnN0h4FLG7LaXnT8Zpr7ricrA.
ECDSA key fingerprint is MD5:1a:70:c5:60:05:47:6a:75:8a:47:db:85:51:1c:32:2c.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '10.10.10.72' (ECDSA) to the list of known hosts.
[email protected]'s password:
test.sql 100% 1790 1.9MB/s 00:00
配置从服务器
[[email protected] ~]# mysql -uslave -p'[email protected]*qw92!derS' -h 10.10.10.69 在slave服务器上使用授权账号slave登录master mysql测试授权账号能否登录
mysql: [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 8
Server version: 5.7.20-log MySQL Community Server (GPL)
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
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 statement.
mysql>
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
+--------------------+
1 row in set (0.00 sec)
如上所示查询不到test测试数据库
mysql> exit; 退出master数据库
Bye
[[email protected] ~]# mysql -uroot -p'[email protected]' 登录slave服务器,密码初始化与master相同
mysql: [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 12
Server version: 5.7.20 MySQL Community Server (GPL)
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
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 statement.
mysql>
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.01 sec)
mysql> create database test; 创建测试数据库
Query OK, 1 row affected (0.01 sec)
mysql> exit; 退出数据库
Bye
[[email protected] ~]# mysql -uroot -p'[email protected]' test<test.sql 将master导出的数据库内容导入slave创建的test数据库内
mysql: [Warning] Using a password on the command line interface can be insecure.
[[email protected] ~]# mysql -uroot -p'[email protected]' 登录slave服务器
mysql: [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 15
Server version: 5.7.20 MySQL Community Server (GPL)
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
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 statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| test |
+--------------------+
5 rows in set (0.00 sec)
mysql> use test; 切换到test数据库
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables; 查看数据库内容
+----------------+
| Tables_in_test |
+----------------+
| test1 | 已存在master服务器创建的表test1,导入成功
+----------------+
1 row in set (0.00 sec)
mysql> exit;
Bye
[[email protected] ~]# systemctl stop mysqld 停掉slave数据库服务
[[email protected] ~]# vim /etc/my.cnf 编辑slave服务器数据库配置文件mysqld下面添加一行 server-id=2 (从服务器ID号,不要和主ID相同 ,如果设置多个从服务器,每个从服务器必须有一个唯一的server-id值,必须与主服务器的以及其它从服务器的不相同。可以认为server-id值类似于IP地址:这些ID值能唯一识别复制服务器群集中的每个服务器实例。)
[[email protected] ~]# systemctl start mysqld 启动mysql服务
[[email protected] ~]# mysql -uroot -p'[email protected]' 登录mysql服务器
mysql: [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 3
Server version: 5.7.20 MySQL Community Server (GPL)
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
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 statement.
mysql> stop slave; 停止slave
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> change master to master_host='10.10.10.69',master_user='slave',master_password='[email protected]*qw92!derS'; (授权slave服务器同步master,指定账号和密码)
Query OK, 0 rows affected, 2 warnings (0.05 sec)
mysql> start slave; 启动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: 10.10.10.69
Master_User: slave
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin-master.000001
Read_Master_Log_Pos: 448
Relay_Log_File: xuegodslave-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进程
如上图所示,主从服务器均运行并连接成功
再到主服务器上查看状态:
最后在master服务器中的test数据库中插入数据进行同步测试:
测试成功,主从服务器已成功同步数据!
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
如果遇到主从不同步,看一下主从bin-log的位置,然后再同步。
在主服务器上看二进制日志事件列表
mysql> show binlog events \G
从服务器执行MySQL命令下:
mysql> stop slave; #先停止slave服务
mysql> change master to master_log_file='mysql-bin-master.000001',master_log_pos=1164;
#根据上面主服务器的show master status的结果,进行从服务器的二进制数据库记录回归,达到同步的效果
mysql>slave start; #启动从服务器同步服务
mysql> show slave status\G; #用show slave status\G;看一下从服务器的同步情况
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
如果都是yes,那代表已经在同步
重启从服务器,再查看状态:
停止从服务器slave stop;
开启从服务器slave start;
排错思路:
1、二进制日志没有开启
2、IPTABLES 没有放开端口
3、对应的主机 IP地址写错了
SQL线程出错
1、主从服务器数据库结构不统一
出错后,数据少,可以手动解决创建插入,再更新slave状态。
注:如果主上误删除了。那么从上也就误删除了。 #因此主上要定期做mysqldump备份。