MySQL8.0.20单机多实例部署步骤
作者:宇枫16 时间:2024-01-13 13:46:37
0.环境需要
1.准备Linux环境(系统:CentOS7)
2.准备MySQL安装包(版本:8.0.20)
3.安装方式为:msyql解压安装
1.安装步骤
1.下载解压安装的mysql安装包文件
下载地址:https://cdn.mysql.com/archives/mysql-8.0/mysql-8.0.20-el7-x86_64.tar.gz
2.上传解压缩(我这里的上传为:xhell,当然也可使用其他方式)
## 创建mysql目录
mkdir -p /usr/app/mysql
## 移动到目录下
cd /usr/app/mysql
## 使用xhell上传文件到服务器
## 解压后重命名
tar -zxvf mysql-8.0.20-el7-x86_64.tar.gz
mv mysql-8.0.20-el7-x86_64 mysql-8.0
3.在mysql目录创建创建数据文件存放路径并赋权
## 创建各实例数据存放目录
mkdir -p /usr/app/mysql/mysql-8.0/{3306,3307,3308}/data
## 创建各个mysql对应error日志
mkdir -p /usr/app/mysql/mysql-8.0/{3306,3307,3308}/log
touch /usr/app/mysql/mysql-8.0/3306/log/error.log
touch /usr/app/mysql/mysql-8.0/3307/log/error.log
touch /usr/app/mysql/mysql-8.0/3308/log/error.log
## 创建mysql用户组及用户
groupadd mysql
useradd -g mysql mysql
## 目录归属赋权
chown -R mysql:mysql /usr/app/mysql
4.配置my.cnf文件
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/8.0/en/server-configuration-defaults.html
[client]
default-character-set=utf8mb4
[mysqld]
user=mysql
basedir=/usr/app/mysql/mysql-8.0
lower_case_table_names=1
[mysqld_multi]
mysqld=/usr/app/mysql/mysql-8.0/bin/mysqld_safe
mysqladmin=/usr/app/mysql/mysql-8.0/bin/mysqladmin
log=/usr/app/mysql/mysql-8.0/mysqld_multi.log
# 3306 数据库实例
[mysqld3306]
port=3306
server_id=1
mysqld=mysqld
mysqladmin=mysqladmin
datadir=/usr/app/mysql/mysql-8.0/3306/data
socket=/tmp/mysql_3306.sock
log-error=/usr/app/mysql/mysql-8.0/3306/log/error.log
pid-file=/usr/app/mysql/mysql-8.0/3306/mysql3306.pid
## skip-grant-tables #用于跳过密码登录
character_set_server=utf8mb4
init_connect='SET NAMES utf8mb4'
lower_case_table_names=1
explicit_defaults_for_timestamp=true
# 3307 数据库实例
[mysqld3307]
port=3307
server_id=2
mysqld=mysqld
mysqladmin=mysqladmin
datadir=/usr/app/mysql/mysql-8.0/3307/data
socket=/tmp/mysql_3307.sock
log-error=/usr/app/mysql/mysql-8.0/3307/log/error.log
pid-file=/usr/app/mysql/mysql-8.0/3307/mysql3307.pid
# lc_messages_dir=/usr/local/mysql/share/english
## 默认最大连接数设置
# max_connections=300
character_set_server=utf8mb4
init_connect='SET NAMES utf8mb4'
lower_case_table_names=1
explicit_defaults_for_timestamp=true
# 3308 数据库实例
[mysqld3308]
port=3308
server_id=3
mysqld=mysqld
mysqladmin=mysqladmin
datadir=/usr/app/mysql/mysql-8.0/3308/data
socket=/tmp/mysql_3308.sock
log-error=/usr/app/mysql/mysql-8.0/3308/log/error.log
pid-file=/usr/app/mysql/mysql-8.0/3308/mysql3308.pid
# lc_messages_dir=/usr/local/mysql/share/english
character_set_server=utf8mb4
init_connect='SET NAMES utf8mb4'
lower_case_table_names=1
explicit_defaults_for_timestamp=true
5.初始化各实例数据库
/usr/app/mysql/mysql-8.0/bin/mysqld --defaults-file=/etc/my.cnf --datadir=/usr/app/mysql/mysql-8.0/3306/data/ --initialize
/usr/app/mysql/mysql-8.0/bin/mysqld --defaults-file=/etc/my.cnf --datadir=/usr/app/mysql/mysql-8.0/3307/data/ --initialize
/usr/app/mysql/mysql-8.0/bin/mysqld --defaults-file=/etc/my.cnf --datadir=/usr/app/mysql/mysql-8.0/3308/data/ --initialize
注意记住临时密码,后面登录时使用,例如:
2022-04-04T15:12:39.011998Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: sJH):ayhH5cW
2022-04-04T15:14:24.214337Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: <>y)qhyh/4E7
2022-04-04T15:15:32.283026Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: s7qrjzwqI<wd
6.设置msyql环境变量
添加了环境变量,操作系统才能够自己找到mysql、mysqld_multi等命令的位置
## 在/etc/profile 文件末尾添加
export PATH=/usr/app/mysql/mysql-8.0/bin:$PATH
## 使环境变量生效
source /etc/profile
7.启动与查看 mysql 服务(需指定配置文件)
## 启动mysql
mysqld_multi --defaults-file=/etc/my.cnf start 3306
mysqld_multi --defaults-file=/etc/my.cnf start 3307
mysqld_multi --defaults-file=/etc/my.cnf start 3308
## 查看mysql服务
mysqld_multi --defaults-file=/etc/my.cnf report
启动不成功,查看 mysqld_multi.log 日志或各实例目录下的错误日志
cat /usr/app/mysql/mysql-8.0/mysqld_multi.log
cat /usr/app/mysql/mysql-8.0/3306/log/error.log
8.设置远程访问密码
## 服务器登录 (注意:另外两个实例同样如此。)
mysql -uroot -p -S /tmp/mysql_3306.sock
## 输入上面初始化数据库时的临时密码
## 设置本地访问密码,例如:Mysql@123,注意密码为高安保等级(例如大小写和特殊字符的组合),不然无法使用其他操作
ALTER USER 'root'@'localhost' IDENTIFIED BY 'Mysql@123';
## 进入mysql数据
use mysql;
## 更新root信息
update user set host='%' where user='root';
## 刷新
flush privileges;
# 授权root用户可以远程登陆
GRANT ALL ON *.* TO 'root'@'%';
## 远程连接设置
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'Mysql@123';
## 刷新
flush privileges;
9.防火墙开启访问端口(获取关闭防火墙)
## 开放防火墙端口
firewall-cmd --zone=public --add-port=3306/tcp --permanent
firewall-cmd --zone=public --add-port=3307/tcp --permanent
firewall-cmd --zone=public --add-port=3308/tcp --permanent
## 查看开放端口
firewall-cmd --list-port
## 配置生效
firewall-cmd --reload
然后就可以外部访问了,以上就是我的MySQL8.0单机多实例安装步骤采坑记录。
来源:https://blog.csdn.net/rao991207823/article/details/123963586