# centos7安装多版本多实例mysql
# 安装前的准备工作
mkdir -p /opt/software
rm -rf /opt/mysql
# MySQL安装目录
mkdir -p /opt/mysql/app
# MySQL数据目录
mkdir -p /opt/mysql/330{7,8,9}/data
mkdir -p /opt/mysql/33{17,18,19}/data
# Scoket目录
mkdir -p /opt/mysql/socket
# 上传mysql的解压包
# 安装mysql5.7
# 解压
# 解压mysql
cd /opt/software
tar xf mysql-5.7.39-el7-x86_64.tar.gz
mv /opt/software/mysql-5.7.39-el7-x86_64 /opt/mysql/app/mysql-5.7.39
ln -s /opt/mysql/app/mysql-5.7.39 /opt/mysql/app/mysql5739
# 实例配置文件
# 3307实例配置
cat > /opt/mysql/3307/my.cnf <<EOF
[mysqld]
user=mysql
lower_case_table_names=1
default-storage-engine=INNODB
character_set_server=utf8mb4
collation-server=utf8mb4_bin
max_allowed_packet=256M
innodb_log_file_size=256M
innodb_default_row_format=DYNAMIC
basedir=/opt/mysql/app/mysql5739
datadir=/opt/mysql/3307/data
socket=/opt/mysql/socket/mysql3307.sock
log-error=/opt/mysql/3307/mysql.log
log_bin=/opt/mysql/3307/mysql-bin
binlog_format=row
port=3307
server_id=7
max_connect_errors=500
max_connections=1000
EOF
# 3308实例配置
cat > /opt/mysql/3308/my.cnf <<EOF
[mysqld]
user=mysql
lower_case_table_names=1
default-storage-engine=INNODB
character_set_server=utf8mb4
collation-server=utf8mb4_bin
max_allowed_packet=256M
innodb_log_file_size=256M
innodb_default_row_format=DYNAMIC
basedir=/opt/mysql/app/mysql5739
datadir=/opt/mysql/3308/data
socket=/opt/mysql/socket/mysql3308.sock
log-error=/opt/mysql/3308/mysql.log
log_bin=/opt/mysql/3308/mysql-bin
binlog_format=row
port=3308
server_id=8
max_connect_errors=500
max_connections=1000
EOF
# 3309实例配置
cat > /opt/mysql/3309/my.cnf <<EOF
[mysqld]
user=mysql
lower_case_table_names=1
default-storage-engine=INNODB
character_set_server=utf8mb4
collation-server=utf8mb4_bin
max_allowed_packet=256M
innodb_log_file_size=256M
innodb_default_row_format=DYNAMIC
basedir=/opt/mysql/app/mysql5739
datadir=/opt/mysql/3309/data
socket=/opt/mysql/socket/mysql3309.sock
log-error=/opt/mysql/3309/mysql.log
log_bin=/opt/mysql/3309/mysql-bin
binlog_format=row
port=3309
server_id=9
max_connect_errors=500
max_connections=1000
EOF
# 创建用户并授权
useradd -s /sbin/nologin -M mysql
id mysql
chown -R mysql.mysql /opt/mysql
# 初始化数据库
# 3307初始化数据库
/opt/mysql/app/mysql5739/bin/mysqld --initialize-insecure --user=mysql --basedir=/opt/mysql/app/mysql5739 --datadir=/opt/mysql/3307/data
# 3308初始化数据库
/opt/mysql/app/mysql5739/bin/mysqld --initialize-insecure --user=mysql --basedir=/opt/mysql/app/mysql5739 --datadir=/opt/mysql/3308/data
# 3309初始化数据库
/opt/mysql/app/mysql5739/bin/mysqld --initialize-insecure --user=mysql --basedir=/opt/mysql/app/mysql5739 --datadir=/opt/mysql/3309/data
# 创建实例启动的服务脚本
# 3307实例启动的服务脚本
cat > /etc/systemd/system/mysqld3307.service <<EOF
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
ExecStart=/opt/mysql/app/mysql5739/bin/mysqld --defaults-file=/opt/mysql/3307/my.cnf
LimitNOFILE = 1000
EOF
# 3308实例启动的服务脚本
cat > /etc/systemd/system/mysqld3308.service <<EOF
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
ExecStart=/opt/mysql/app/mysql5739/bin/mysqld --defaults-file=/opt/mysql/3308/my.cnf
LimitNOFILE = 1000
EOF
# 3309实例启动的服务脚本
cat > /etc/systemd/system/mysqld3309.service <<EOF
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
ExecStart=/opt/mysql/app/mysql5739/bin/mysqld --defaults-file=/opt/mysql/3309/my.cnf
LimitNOFILE = 1000
EOF
# 服务启动
systemctl daemon-reload
# 服务启动
systemctl start mysqld3307
systemctl start mysqld3308
systemctl start mysqld3309
# 服务端口监控
netstat -lntup|grep mysqld
# 命令行登录
# 命令行登录
cd /opt/mysql/app/mysql5739/bin
./mysql -h 127.0.0.1 -u root -p -P 3307
./mysql -h 127.0.0.1 -u root -p -P 3308
./mysql -h 127.0.0.1 -u root -p -P 3309
# 更新密码
alter user user() identified by 'Root_12root';
ALTER USER 'root'@'localhost' IDENTIFIED BY 'tiankafei';
use mysql;
update user set host = '%' where user = 'root';
select host, user from user;
flush privileges;
# 设置开机启动
# 设置开机启动
systemctl enable mysqld3307
systemctl enable mysqld3308
systemctl enable mysqld3309
# 安装mysql8.0
# 解压
# 解压mysql
cd /opt/software
tar xf mysql-8.0.30-el7-x86_64.tar.gz
mv /opt/software/mysql-8.0.30-el7-x86_64 /opt/mysql/app/mysql-8.0.30
ln -s /opt/mysql/app/mysql-8.0.30 /opt/mysql/app/mysql8030
# 实例配置文件
# 3317实例配置
cat > /opt/mysql/3317/my.cnf <<EOF
[mysqld]
user=mysql
lower_case_table_names=1
default-storage-engine=INNODB
character_set_server=utf8mb4
collation-server=utf8mb4_bin
max_allowed_packet=256M
innodb_redo_log_capacity=256M
innodb_default_row_format=DYNAMIC
basedir=/opt/mysql/app/mysql8030
datadir=/opt/mysql/3317/data
socket=/opt/mysql/socket/mysql3317.sock
log-error=/opt/mysql/3317/mysql.log
log_bin=/opt/mysql/3317/mysql-bin
binlog_format=row
port=3317
mysqlx_port=33317
mysqlx_socket=/opt/mysql/socket/mysql33317.sock
max_connect_errors=500
max_connections=1000
server_id=17
EOF
# 3318实例配置
cat > /opt/mysql/3318/my.cnf <<EOF
[mysqld]
user=mysql
lower_case_table_names=1
default-storage-engine=INNODB
character_set_server=utf8mb4
collation-server=utf8mb4_bin
max_allowed_packet=256M
innodb_redo_log_capacity=256M
innodb_default_row_format=DYNAMIC
basedir=/opt/mysql/app/mysql8030
datadir=/opt/mysql/3318/data
socket=/opt/mysql/socket/mysql3318.sock
log-error=/opt/mysql/3318/mysql.log
log_bin=/opt/mysql/3318/mysql-bin
binlog_format=row
port=3318
mysqlx_port=33318
mysqlx_socket=/opt/mysql/socket/mysql33318.sock
max_connect_errors=500
max_connections=1000
server_id=18
EOF
# 3319实例配置
cat > /opt/mysql/3319/my.cnf <<EOF
[mysqld]
user=mysql
lower_case_table_names=1
default-storage-engine=INNODB
character_set_server=utf8mb4
collation-server=utf8mb4_bin
max_allowed_packet=256M
innodb_redo_log_capacity=256M
innodb_default_row_format=DYNAMIC
basedir=/opt/mysql/app/mysql8030
datadir=/opt/mysql/3319/data
socket=/opt/mysql/socket/mysql3319.sock
log-error=/opt/mysql/3319/mysql.log
log_bin=/opt/mysql/3319/mysql-bin
binlog_format=row
port=3319
mysqlx_port=33319
mysqlx_socket=/opt/mysql/socket/mysql33319.sock
max_connect_errors=500
max_connections=1000
server_id=19
EOF
# 初始化数据库
# 安装依赖
yum install -y perl-DBD-MySQL perl-Config-Tiny epel-release perl-Log-Dispatch perl-Parallel-ForkManager perl-Time-HiRes
# 初始化3317数据库
/opt/mysql/app/mysql8030/bin/mysqld --initialize-insecure --lower-case-table-names=1 --user=mysql --basedir=/opt/mysql/app/mysql8030 --datadir=/opt/mysql/3317/data
# 初始化3318数据库
/opt/mysql/app/mysql8030/bin/mysqld --initialize-insecure --lower-case-table-names=1 --user=mysql --basedir=/opt/mysql/app/mysql8030 --datadir=/opt/mysql/3318/data
# 初始化3319数据库
/opt/mysql/app/mysql8030/bin/mysqld --initialize-insecure --lower-case-table-names=1 --user=mysql --basedir=/opt/mysql/app/mysql8030 --datadir=/opt/mysql/3319/data
# 创建实例启动的服务脚本
# 3317实例启动的服务脚本
cat > /etc/systemd/system/mysqld3317.service <<EOF
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
ExecStart=/opt/mysql/app/mysql8030/bin/mysqld --defaults-file=/opt/mysql/3317/my.cnf
LimitNOFILE = 1000
EOF
# 3318实例启动的服务脚本
cat > /etc/systemd/system/mysqld3318.service <<EOF
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
ExecStart=/opt/mysql/app/mysql8030/bin/mysqld --defaults-file=/opt/mysql/3318/my.cnf
LimitNOFILE = 1000
EOF
# 3319实例启动的服务脚本
cat > /etc/systemd/system/mysqld3319.service <<EOF
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
ExecStart=/opt/mysql/app/mysql8030/bin/mysqld --defaults-file=/opt/mysql/3319/my.cnf
LimitNOFILE = 1000
EOF
# 服务启动
systemctl daemon-reload
# 服务启动
systemctl start mysqld3317
systemctl start mysqld3318
systemctl start mysqld3319
# 服务端口监控
netstat -lntup|grep mysqld
# 命令行登录
cd /opt/mysql/app/mysql8030/bin
./mysql -h 127.0.0.1 -u root -p -P 3317
./mysql -h 127.0.0.1 -u root -p -P 3318
./mysql -h 127.0.0.1 -u root -p -P 3319
# 更新密码
alter user user() identified by 'Root_12root';
SHOW VARIABLES LIKE 'validate_password%';
set global validate_password.policy=0;
set global validate_password.length=1;
ALTER USER 'root'@'localhost' IDENTIFIED BY 'tiankafei';
use mysql;
update user set host = '%' where user = 'root';
select host, user from user;
flush privileges;
# 设置开机启动
# 设置开机启动
systemctl enable mysqld3317
systemctl enable mysqld3318
systemctl enable mysqld3319