[root@server ~]# ls
anaconda-ks.cfg
mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz
2. 创建系统用户和组
[root@localhost ~]# useradd -r -M -s /sbin/nologin mysql
[root@localhost ~]# id mysql
uid=994(mysql) gid=991(mysql) groups=991(mysql)
3. 解压到/usr/local/
[root@server ~]# tar xf mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@server ~]# cd /usr/local
[root@server local]# ls
bin lib sbin
etc lib64 share
games libexec src
include mysql-5.7.31-linux-glibc2.12-x86_64
做一个软连接
[root@localhost local]# ln -s mysql-5.7.31-linux-glibc2.12-x86_64 mysql
[root@localhost local]# ll
total 0
drwxr-xr-x. 2 root root 6 Aug 12 2018 bin
drwxr-xr-x. 2 root root 6 Aug 12 2018 etc
drwxr-xr-x. 2 root root 6 Aug 12 2018 games
drwxr-xr-x. 2 root root 6 Aug 12 2018 include
drwxr-xr-x. 2 root root 6 Aug 12 2018 lib
drwxr-xr-x. 2 root root 6 Aug 12 2018 lib64
drwxr-xr-x. 2 root root 6 Aug 12 2018 libexec
lrwxrwxrwx. 1 root root 35 Oct 26 16:29 mysql -> mysql-5.7.31-linux-glibc2.12-x86_64
drwxr-xr-x. 9 7161 31415 129 Jun 2 21:11 mysql-5.7.31-linux-glibc2.12-x86_64
drwxr-xr-x. 2 root root 6 Aug 12 2018 sbin
drwxr-xr-x. 5 root root 49 Aug 30 14:12 share
drwxr-xr-x. 2 root root 6 Aug 12 2018 src
修改属主,属组
[root@localhost local]# chown -R mysql.mysql mysql*
[root@localhost local]# ll
total 0
drwxr-xr-x. 2 root root 6 Aug 12 2018 bin
drwxr-xr-x. 2 root root 6 Aug 12 2018 etc
drwxr-xr-x. 2 root root 6 Aug 12 2018 games
drwxr-xr-x. 2 root root 6 Aug 12 2018 include
drwxr-xr-x. 2 root root 6 Aug 12 2018 lib
drwxr-xr-x. 2 root root 6 Aug 12 2018 lib64
drwxr-xr-x. 2 root root 6 Aug 12 2018 libexec
lrwxrwxrwx. 1 mysql mysql 35 Oct 26 16:29 mysql -> mysql-5.7.31-linux-glibc2.12-x86_64
drwxr-xr-x. 9 mysql mysql 129 Jun 2 21:11 mysql-5.7.31-linux-glibc2.12-x86_64
drwxr-xr-x. 2 root root 6 Aug 12 2018 sbin
drwxr-xr-x. 5 root root 49 Aug 30 14:12 share
drwxr-xr-x. 2 root root 6 Aug 12 2018 src
4. 添加一个环境变量
[root@localhost ~]# ls /usr/local/mysql
bin include LICENSE README support-files
docs lib man share
[root@localhost ~]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@localhost ~]# source /etc/profile.d/mysql.sh
[root@localhost ~]# echo $PATH
/usr/local/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
5. 创建各实例数据存放目录
[root@localhost ~]# mkdir -p /opt/data/{3306,3307,3308}
[root@localhost ~]# ls /opt/data/
3306 3307 3308
[root@localhost ~]# chown -R mysql.mysql /opt/data
[root@localhost ~]# ll /opt
total 0
drwxr-xr-x. 5 mysql mysql 42 Oct 26 16:32 data
drwxr-xr-x. 5 root root 92 Sep 6 16:00 qq
[root@localhost ~]# ll /opt/data
total 0
drwxr-xr-x. 2 mysql mysql 6 Oct 26 16:32 3306
drwxr-xr-x. 2 mysql mysql 6 Oct 26 16:32 3307
drwxr-xr-x. 2 mysql mysql 6 Oct 26 16:32 3308
6. 初始化各实例
#初始化3306
[root@localhost ~]# mysqld --initialize --user=mysql --datadir=/opt/data/3306
2020-10-26T08:37:56.884114Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-10-26T08:37:57.079187Z 0 [Warning] InnoDB: New log files created, LSN=45790
2020-10-26T08:37:57.107635Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2020-10-26T08:37:57.162968Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 8c39bdff-1766-11eb-a9a2-000c2959b333.
2020-10-26T08:37:57.163604Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2020-10-26T08:37:58.123561Z 0 [Warning] CA certificate ca.pem is self signed.
2020-10-26T08:37:58.292679Z 1 [Note] A temporary password is generated for root@localhost: sno_WlOiu5S6
[root@localhost ~]# echo 'sno_WlOiu5S6' > 3306_pass
[root@localhost ~]# cat 3306_pass
sno_WlOiu5S6
#初始化3307
[root@localhost ~]# mysqld --initialize --user=mysql --datadir=/opt/data/3307
2020-10-26T08:40:43.119490Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-10-26T08:40:43.308575Z 0 [Warning] InnoDB: New log files created, LSN=45790
2020-10-26T08:40:43.342122Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2020-10-26T08:40:43.397690Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: ef4f25f6-1766-11eb-afe4-000c2959b333.
2020-10-26T08:40:43.398491Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2020-10-26T08:40:44.012440Z 0 [Warning] CA certificate ca.pem is self signed.
2020-10-26T08:40:44.353702Z 1 [Note] A temporary password is generated for root@localhost: kOwHCYf,?5q>
[root@localhost ~]# echo 'kOwHCYf,?5q>' > 3307_pass
[root@localhost ~]# cat 3307_pass
kOwHCYf,?5q>
#初始化3308
[root@localhost ~]# mysqld --initialize --user=mysql --datadir=/opt/data/3308
2020-10-26T08:42:04.728785Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-10-26T08:42:04.911757Z 0 [Warning] InnoDB: New log files created, LSN=45790
2020-10-26T08:42:04.945662Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2020-10-26T08:42:05.008066Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 1ff3e74e-1767-11eb-b2a1-000c2959b333.
2020-10-26T08:42:05.008738Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2020-10-26T08:42:05.985707Z 0 [Warning] CA certificate ca.pem is self signed.
2020-10-26T08:42:06.135160Z 1 [Note] A temporary password is generated for root@localhost: Hq;yDXhhq6a3
[root@localhost ~]# echo ' Hq;yDXhhq6a3' > 3308_pass
[root@localhost ~]# cat 3308_pass
Hq;yDXhhq6a3
[root@localhost ~]# ls
3306_pass anaconda-ks.cfg
3307_pass mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz
3308_pass
[root@localhost ~]# cat 3306_pass
sno_WlOiu5S6
[root@localhost ~]# mysql -uroot -p'sno_WlOiu5S6' -S /tmp/mysql3306.sock
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 2
Server version: 5.7.31
Copyright (c) 2000, 2020, 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('maqiang123');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> quit
Bye
[root@localhost ~]# cat 3307_pass
kOwHCYf,?5q>
[root@localhost ~]# mysql -uroot -p'kOwHCYf,?5q>' -S /
tmp/mysql3307.sock
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 2
Server version: 5.7.31
Copyright (c) 2000, 2020, 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('maqiang123');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> quit
Bye
[root@localhost ~]# cat 3308_pass
Hq;yDXhhq6a3
[root@localhost ~]# mysql -uroot -p'Hq;yDXhhq6a3' -S /tmp/mysql3308.sock
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.31
Copyright (c) 2000, 2020, 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('maqiang123');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> quit
Bye
11. 写脚本控制启动,停止
[root@localhost ~]# vim /etc/init.d/my3306
# chkconfig: 2345 35 65
# Description: akldjaldallk
export PATH=/usr/local/mysql/bin:$PATH
mysqld_multi=/usr/local/mysql/bin/mysqld_multi
case $1 in
'start')
$mysqld_multi start 3306
;;
'stop')
ps -ef|grep 3306|grep -v grep|awk '{print $2}'|xargs kill -9
;;
'restart')
$mysqld_multi reload 3306
;;
'status')
status=$($mysqld_multi report 3306|grep -c 'not')
if [ $status -eq 0 ];then
echo "mysqld3306 is running."
else
echo "mysqld3306 is not running."
fi
;;
*)
echo "Usage: service $0 start|stop|restart|status"
;;
esac
停止mysql所有进程
[root@localhost ~]# pkill mysql
[root@localhost ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
手动启动3306,3307,3308服务
[root@localhost ~]# mysqld_multi start 3306
[root@localhost ~]# mysqld_multi start 3307
[root@localhost ~]# mysqld_multi start 3308
[root@localhost ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 80 *:3306 *:*
LISTEN 0 80 *:3307 *:*
LISTEN 0 80 *:3308 *:*
停止3306服务
[root@localhost ~]# service my3306 stop
Killed
[root@localhost ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 80 *:3307 *:*
LISTEN 0 80 *:3308 *:*
查看状态
[root@localhost ~]# service my3306 status
mysqld3306 is not running.
[root@localhost ~]# service my3306 start
[root@localhost ~]# service my3306 status
mysqld3306 is running.
12. 脚本控制单或多实例部署
[root@localhost ~]# vim xx.sh
#!/bin/bash
#安装源
repo =$(ls /etc/yum.repos.d|wc -l)
if [ $repo -eq 0 ];then
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-8.repo
sed -i 's/$releasever/8/g' /etc/yum.repos.d/CentOS-Base.repo
sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
fi
#安装
yum -y install perl
#变量
install_dir=/usr/local
soft=mysql-5.7.30-linux-glibc2.12-x86_64
datadir=/opt/data
read -p "请输入创建的实例数:" count
port=3306
#创建用户
id mysql
if [ $? -ne 0 ];then
useradd -r -M -s /sbin/nologin -u 306 mysql
fi
#解压
if [ ! -d $install_dir/$soft ];then
tar xf ${soft}.tar.gz -C /usr/local/
fi
#建立软连接
ln -s $install_dir/$soft/ $install_dir/mysql
#修改属组
chown -R mysql.mysql $install_dir/mysql*
#环境变量
echo "export PATH=$install_dir/mysql/bin:\$PATH" > /etc/profile.d/mysql.sh
. /etc/profile.d/mysql.sh
cat > /etc/my.cnf <<EOF
[mysqld_multi]
mysqld = $install_dir/mysql/bin/mysqld_safe
mysqladmin = $install_dir/mysql/bin/mysqladmin
EOF
#创建数据库日志目录
mkdir -p /var/log/mysql
chown -R mysql.mysql /var/log/mysql
#初始化各实例、创建数据库目录
for i in $(seq $count);do
mkdir -p $datadir/$port
chown -R mysql.mysql $datadir
/usr/local/mysql/bin/mysqld --initialize --datadir=$datadir/$port --user=mysql &>/tmp/${port}_tpf
tenpw=$(grep 'password' /tmp/${port}_tpf|awk '{print $NF}')
#配置配置文件/etc/my.cnf
cat >> /etc/my.cnf <<EOF
[mysqld$port]
datadir = $datadir/$port
port = $port
socket = /tmp/mysql${port}.sock
pid-file = $datadir/$port/mysql_${port}.pid
log-error=/var/log/mysql/${port}.log
EOF
##启动各实例、#初始化密码
$install_dir/mysql/bin/mysqld_multi start $port
read -p "请输入新的数据库密码:" password
$install_dir/mysql/bin/mysql -uroot -p$tenpw -S /tmp/mysql${port}.sock -e "set password=password('$password');" --connect-expired-password
$install_dir/mysql/bin/mysql -uroot -p$password -S /tmp/mysql${port}.sock -e 'show databases;' &>/dev/null
if [ $? -eq 0 ];then
echo "您的数据库密码修改成功"
else
echo "Error your operation fault"
fi
let port++
done
[root@localhost ~]# pkill mysql
[root@localhost ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
[root@localhost ~]# bash -x xx.sh
++ ls /etc/yum.repos.d
++ wc -l
+ repo =8
xx.sh: line 3: repo: command not found
+ '[' -eq 0 ']'
xx.sh: line 4: [: -eq: unary operator expected
+ yum -y install perl
+ install_dir=/usr/local
+ soft=mysql-5.7.30-linux-glibc2.12-x86_64
+ datadir=/opt/data
+ read -p 请输入创建的实例数: count
请输入创建的实例数:2
+ port=3306
+ id mysql
uid=994(mysql) gid=991(mysql) groups=991(mysql)
+ '[' 0 -ne 0 ']'
+ '[' '!' -d /usr/local/mysql-5.7.30-linux-glibc2.12-x86_64 ']'
+ tar xf mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
tar: mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz: Cannot open: No such file or directory
tar: Error is not recoverable: exiting now
+ ln -s /usr/local/mysql-5.7.30-linux-glibc2.12-x86_64/ /usr/local/mysql
ln: failed to create symbolic link '/usr/local/mysql/mysql-5.7.30-linux-glibc2.12-x86_64': File exists
+ chown -R mysql.mysql /usr/local/mysql /usr/local/mysql-5.7.31-linux-glibc2.12-x86_64
+ echo 'export PATH=/usr/local/mysql/bin:$PATH'
+ . /etc/profile.d/mysql.sh
++ export PATH=/usr/local/mysql/bin:/usr/local/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
++ PATH=/usr/local/mysql/bin:/usr/local/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
+ cat
+ mkdir -p /var/log/mysql
+ chown -R mysql.mysql /var/log/mysql
++ seq 2
+ for i in $(seq $count)
+ mkdir -p /opt/data/3306
+ chown -R mysql.mysql /opt/data
+ /usr/local/mysql/bin/mysqld --initialize --datadir=/opt/data/3306 --user=mysql
++ grep password /tmp/3306_tpf
++ awk '{print $NF}'
+ tenpw=
+ cat
+ /usr/local/mysql/bin/mysqld_multi start 3306
+ read -p 请输入新的数据库密码: password
请输入新的数据库密码:maqiang123
+ /usr/local/mysql/bin/mysql -uroot -p -S /tmp/mysql3306.sock -e 'set password=password('\''maqiang123'\'');' --connect-expired-password
Enter password:
+ /usr/local/mysql/bin/mysql -uroot -pmaqiang123 -S /tmp/mysql3306.sock -e 'show databases;'
+ '[' 0 -eq 0 ']'
+ echo 您的数据库密码修改成功
您的数据库密码修改成功
+ let port++
+ for i in $(seq $count)
+ mkdir -p /opt/data/3307
+ chown -R mysql.mysql /opt/data
+ /usr/local/mysql/bin/mysqld --initialize --datadir=/opt/data/3307 --user=mysql
++ awk '{print $NF}'
++ grep password /tmp/3307_tpf
+ tenpw=
+ cat
+ /usr/local/mysql/bin/mysqld_multi start 3307
+ read -p 请输入新的数据库密码: password
请输入新的数据库密码:maqiang123
+ /usr/local/mysql/bin/mysql -uroot -p -S /tmp/mysql3307.sock -e 'set password=password('\''maqiang123'\'');' --connect-expired-password
Enter password:
+ /usr/local/mysql/bin/mysql -uroot -pmaqiang123 -S /tmp/mysql3307.sock -e 'show databases;'
+ '[' 0 -eq 0 ']'
+ echo 您的数据库密码修改成功
您的数据库密码修改成功
+ let port++
[root@localhost ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 80 *:3306 *:*
LISTEN 0 80 *:3307 *:*
[root@localhost ~]# service my3306 start
[root@localhost ~]# service my3306 status
mysqld3306 is running.
13. 单实例和多实例脚本部署
写配置文件
[root@localhost ~]# ls
anaconda-ks.cfg mysql
[root@localhost ~]# cd mysql/
[root@localhost mysql]# ls
install.sh soft
[root@localhost mysql]# vim install.sh
#!/bin/bash
installdir=/usr/local
packname=mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz
unpck=$installdir/$(echo $packname| awk -F'.tar' '{print $1}')
port=3306
datadir=/data
PATH=$installdir/mysql/bin:$PATH
yum -y -q install perl ncurses-compat-libs &>/dev/null
read -p "请输入,你要创建几个实例就输入数字几:" num
read -p "请输入,你要为数据库设置什么密码:" num1
id mysql &>/dev/null || /usr/sbin/useradd -r -M -s /sbin/nologin mysql
[ ! -d $installdir ] && mkdir -p $installdir
if [ ! -d $unpck ];then
echo "正在解压$packname至$installdir下"
tar xf soft/$packname -C $installdir
fi
ln -s $unpck $installdir/mysql &>/dev/null
chown -R mysql.mysql $installdir/mysql*
echo "export PATH=$installdir/mysql/bin:\$PATH" > /etc/profile.d/mysql.sh
if [ $num -gt 1 ];then
cat > /etc/my.cnf <<EOF
[mysqld_multi]
mysqld = $installdir/mysql/bin/mysqld_safe
mysqladmin = $installdir/mysql/bin/mysqladmin
EOF
fi
for i in $(seq $num);do
if [ $i -ne 1 ]; then
let port++
fi
mkdir -p $datadir/$port &>/dev/null
chown -R mysql.mysql $datadir
wcinit=$(ls $datadir/$port|wc -l)
if [ $wcinit -eq 0 ];then
echo "正在初始化$port实例"
$installdir/mysql/bin/mysqld --initialize-insecure --datadir=$datadir/$port --user=mysql --explicit_defaults_for_timestamp &> /dev/null
fi
if [ $num -eq 1 ];then
cat > /etc/my.cnf <<EOF
[mysqld]
basedir = $installdir/mysql
datadir = $datadir/$port
socket = /tmp/mysql.sock
port = 3306
pid-file = $datadir/$port/mysql.pid
user = mysql
skip-name-resolve
EOF
\cp -a $installdir/mysql/support-files/mysql.server /etc/init.d/mysqld
sed -ri "s#^(basedir=).*#\1$installdir/mysql#g" /etc/init.d/mysqld
sed -ri "s#^(datadir=).*#\1$datadir#g" /etc/init.d/mysqld
service mysqld start
$installdir/mysql/bin/mysql -uroot -e "set password = password('$num1');"
else
cat >> /etc/my.cnf <<EOF
[mysqld$port]
datadir = $datadir/$port
port = $port
socket = /tmp/mysql$port.sock
pid-file = $datadir/$port/mysql_$port.pid
log-error=/var/log/$port.log
EOF
$installdir/mysql/bin/mysqld_multi start $port
sleep 3
$installdir/mysql/bin/mysql -uroot -S /tmp/mysql$port.sock -e "set password = password('$num1');"
fi
done
[root@localhost ~]# mysql -uroot -p123456
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.31 MySQL Community Server (GPL)
Copyright (c) 2000, 2020, 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> Terminated