MySQL 5.7--------多实例部署最佳实战
安装之前关闭linux防火墙:centos6和7是不一样的!!!!!!
CentOS 6.5关闭防火墙
|
1
2
|
CentOS 7.2关闭防火墙
CentOS 7.0默认使用的是firewall作为防火墙,这里改为iptables防火墙步骤。
firewall-cmd --state #查看默认防火墙状态(关闭后显示notrunning,开启后显示running)
|
1
2
|
[[email protected] ~]#firewall-cmd --state
not running
|
检查防火墙的状态:
从centos7开始使用systemctl来管理服务和程序,包括了service和chkconfig。
|
1
2
|
firewalld.service disabled
|
或者
|
1
2
3
4
|
[[email protected] ~]#systemctl status firewalld.service
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
Active: inactive (dead)
|
关闭防火墙:
systemctl stop firewalld.service #停止firewall
systemctl disable firewalld.service #禁止firewall开机启动
|
1
2
|
[[email protected] ~]#systemctl stop firewalld.service
[[email protected] ~]#systemctl disable firewalld.service
|
|
1
2
3
4
5
6
7
8
|
启动一个服务:systemctl start firewalld.service
关闭一个服务:systemctl stop firewalld.service
重启一个服务:systemctl restart firewalld.service
显示一个服务的状态:systemctl status firewalld.service
在开机时启用一个服务:systemctl enable firewalld.service
在开机时禁用一个服务:systemctl disable firewalld.service
查看服务是否开机启动:systemctl is-enabled firewalld.service;echo $?
查看已启动的服务列表:systemctl list-unit-files|grep enabled
|
1. 背景
MySQL数据库的集中化运维,可以通过在一台服务器上,部署运行多个MySQL服务进程,通过不同的socket监听不同的服务端口来提供各自的服务。各个实例之间是相互独立的,每个实例的datadir, port, socket, pid都是不同的。
2. 多实例特点
* 有效利用服务器资源,当单个服务器资源有剩余时,可以充分利用剩余的资源提供更多的服务。
* 资源互相抢占问题,当某个服务实例服务并发很高时或者开启慢查询时,会消耗更多的内存、CPU、磁盘IO资源,导致服务器上的其他实例提供服务的质量下降。
3. 环境 [ 关闭SeLinux ]
|
1
2
3
4
5
6
7
8
|
[[email protected]~]# cat /etc/redhat-release
CentOS Linux release 7.4.1708 (Core)
[[email protected]~]# uname -r
3.10.0-693.2.2.el7.x86_64
[[email protected]~]# getenforce
Disabled
|
4. 部署 [ 2个实例 ]
* 下载 MySQL 5.7 二制包 [ 推荐官方下载 ]
1 |
[[email protected]~]#wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz
|
* 解压 MySQL 5.7 二进制包到指定目录
1 |
[[email protected]~]# tar zxvf mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz -C /usr/local/
|
* 创建 MySQL 软链接
1 |
[[email protected]~]# ln -s /usr/local/mysql-5.7.18-linux-glibc2.5-x86_64 /usr/local/mysql
|
* 创建 MySQL 用户
1 |
[[email protected]~]# useradd -r -s /sbin/nologin mysql
|
* 在 MySQL 二进制包目录中创建 mysql-files 目录 [MySQL 数据导入/导出数据专放目录]
|
1
2
|
[[email protected]~]# mkdir -v /usr/local/mysql/mysql-files
mkdir: created directory `/usr/local/mysql/mysql-files'
|
* 创建多实例数据目录
|
1
2
3
4
|
[[email protected]~]# mkdir -vp /data/mysql_data{3306,3307}
mkdir: created directory `/data'
mkdir: created directory `/data/mysql_data3306'
mkdir: created directory `/data/mysql_data3307'
|
* 修改 MySQL 二进制包目录的所属用户与所属组
1 |
[[email protected]~]# chown root.mysql -R /usr/local/mysql-5.7.18-linux-glibc2.5-x86_64
|
* 修改 MySQL 多实例数据目录与 数据导入/导出专放目录的所属用户与所属组
1 |
[[email protected]~]# chown mysql.mysql -R /usr/local/mysql/mysql-files /data/mysql_data{3306,3307}
|
* 配置 MySQL 配置文件 /etc/my.cnf
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
[mysqld_multi]
mysqld = /usr/local/mysql/bin/mysqld
mysqladmin = /usr/local/mysql/bin/mysqladmin
log = /tmp/mysql_multi.log
[mysqld1]
# 设置数据目录 [多实例中一定要不同]
datadir = /data/mysql_data3306
# 设置sock存放文件名 [多实例中一定要不同]
socket = /tmp/mysql.sock3306
# 设置监听开放端口 [多实例中一定要不同]
port = 3306
# 设置运行用户
user = mysql
# 关闭监控
performance_schema = off
# 设置innodb 缓存大小
innodb_buffer_pool_size = 32M
# 设置监听IP地址
bind_address = 0.0.0.0
# 关闭DNS 反向解析
skip-name-resolve = 0
[mysqld2]
datadir = /data/mysql_data3307
socket = /tmp/mysql.sock3307
port = 3307
user = mysql
performance_schema = off
innodb_buffer_pool_size = 32M
bind_address = 0.0.0.0
skip-name-resolve = 0
|
* 初始化各个实例 [ 初始化完成后会自带随机密码在输出日志中 ]
|
1
2
3
4
|
[[email protected] ~]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql_data3306
error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory
[[email protected] ~]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql_data3307
|
千万要记住保存截图中生成的密码,这是初始化实例时生成的随机密码!!!!!!!!
上面错误这么解决:[[email protected] mysql]# yum install libaio*
* 各实例开启 SSL 连接
|
1
2
3
4
|
[[email protected] ~]# /usr/local/mysql/bin/mysql_ssl_rsa_setup --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql_data3306
[[email protected] ~]# /usr/local/mysql/bin/mysql_ssl_rsa_setup --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql_data3307
|
* 复制多实例脚本到服务管理目录下 [ /etc/init.d/ ]
1 |
[[email protected] ~]# cp /usr/local/mysql/support-files/mysqld_multi.server /etc/init.d/mysqld_multi
|
* 添加脚本执行权限
1 |
[[email protected] ~]# chmod +x /etc/init.d/mysqld_multi
|
* 添加进service服务管理
1 |
[[email protected] ~]# chkconfig --add mysqld_multi
|
5. 启动测试
* 查个多实例状态
[[email protected] mysql]# export PATH=/usr/local/mysql/bin:$PATH
|
1
2
3
4
|
[[email protected] ~]# /etc/init.d/mysqld_multi report
Reporting MySQL servers
MySQL server from group: mysqld1 is not running
MySQL server from group: mysqld2 is not running
|
* 启动多实例
1 |
[[email protected] ~]# /etc/init.d/mysqld_multi start
|
* 查看多实例状态
|
1
2
3
4
|
[[email protected] ~]# /etc/init.d/mysqld_multi report
Reporting MySQL servers
MySQL server from group: mysqld1 is running
MySQL server from group: mysqld2 is running
|
* 查看实例监听端口
|
1
2
3
|
[[email protected] ~]# netstat -lntp | grep mysqld
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 1651/mysqld
tcp 0 0 0.0.0.0:3307 0.0.0.0:* LISTEN 1648/mysqld
|
6. 连接测试
* 实例1
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
[[email protected] ~]# /usr/local/mysql/bin/mysql -S /tmp/mysql.sock3306 -p'/Tf_k(Rat3+Q'
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 4
Server version: 5.7.18
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> SET PASSWORD = PASSWORD('root');
Query OK, 0 rows affected (0.00 sec)
|
重置密码
解决远程连接问题:
默认情况下,mysql只允许本地登录,如果要开启远程连接,则需要修改/etc/mysql/my.conf文件。
一、修改/etc/mysql/my.conf
找到bind-address = 127.0.0.1这一行
改为bind-address = 0.0.0.0即可
二、为需要远程登录的用户赋予权限
1、新建用户远程连接mysql数据库
grant all on *.* to [email protected]'%' identified by 'root' with grant option;
flush privileges;
允许任何ip地址(%表示允许任何ip地址)的电脑用admin帐户和密码(root)来访问这个mysql server。
注意admin账户不一定要存在。
2、支持root用户允许远程连接mysql数据库
grant all privileges on *.* to 'root'@'%' identified by 'root' with grant option;
flush privileges;
到这一步我们用Navicart Perminum 连接时出错
原因是:没有配置3306/3307的入口
配置安全组规则:
结果如下:登录成功
参考博客:http://blog.51cto.com/lisea/1941788