手动部署
1、官网下载压缩包
https://cdn.mysql.com/archives/mysql-5.7/mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz
2、解压
tar zxf mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz
2、移动
mv mysql-5.7.26-linux-glibc2.12-x86_64 /usr/local/mysql
3、创建数据目录
mkdir /usr/local/mysql/data
4、创建用户
useradd -r -M -s /sbin/nologin mysql
chown -R mysql.mysql /usr/local/mysql
5、添加配置
vim /etc/my.cnf
[mysqld]
bind-address=0.0.0.0
port=3306
user=mysql
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
socket=/tmp/mysql.sock
log-error=/usr/local/mysql/data/mysql.err
pid-file=/usr/local/mysql/data/mysql.pid
# character config
character_set_server=utf8mb4
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
6、配置环境变量
vim /etc/profile
export PATH=/usr/local/mysql/bin:$PATH
source /etc/profile
7、初始化数据库
/usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql/ --datadir=/data/mysql/ --user=mysql --initialize &> /usr/local/mysql/data/.password
# 不指定log-error=/usr/local/mysql/data/mysql.err的话将会直接输出,注意保存密码。
8、启动
/usr/local/mysql/support-files/mysql.server start
9、添加开机启动配置
6系统
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
chkconfig --add mysqld
7系统
vim /etc/systemd/system/mysqld.service
[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=/usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf
LimitNOFILE=5000
systemctl enable mysqld
10、密码存放位置
密码存放在 /usr/local/mysql/data/mysql.err
未指定 log-error 将存放在 /usr/local/mysql/data/.password
11、密码过期
mysql -uroot -p //登录
mysql> set password for root@localhost = password('输入新的密码'); //修改密码
Query OK, 0 rows affected, 1 warning (0.00 sec)
脚本部署
下载压缩包上传至 /root 目录
https://cdn.mysql.com/archives/mysql-5.7/mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz
#!/bin/bash
# install version mysql-5.7.26
SYSTEM_VERSION=`cat /etc/redhat-release|head -1|awk -F "." '{print $1}'`
err_dis() {
if [ $? != 0 ];then
echo $1 !
exit 1
fi
}
######## mysql-5.7.26 ########
cd /root
tar zxf mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz
if [ ! -d "/usr/local/mysql/" ]; then
mv mysql-5.7.26-linux-glibc2.12-x86_64 /usr/local/mysql
mkdir /usr/local/mysql/data
else
echo "###### The directory \"/usr/local/mysql/\" already exists , You have to do it manually ! ######"
exit 1
fi
######## create a user authorization directory ########
id mysql &>/dev/null
if [ $? -ne 0 ];then
useradd -r -M -s /sbin/nologin mysql
fi
chown -R mysql.mysql /usr/local/mysql
######## mysql configure ########
cat > /etc/my.cnf <<EOF
[mysqld]
bind-address=0.0.0.0
port=3306
user=mysql
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
socket=/tmp/mysql.sock
log-error=/usr/local/mysql/data/mysql.err
pid-file=/usr/local/mysql/data/mysql.pid
# character config
character_set_server=utf8mb4
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
EOF
######## setting environment variables ########
echo 'export PATH=/usr/local/mysql/bin:$PATH' >> /etc/profile
source /etc/profile
######## initializing mysql ########
/usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/ --user=mysql --initialize
err_dis 'mysql initialization failed'
cat /usr/local/mysql/data/mysql.err &>> /usr/local/mysql/data/.password
######## start server ########
/usr/local/mysql/support-files/mysql.server start
err_dis 'mysql startup failure'
######## add the service to start automatically ########
if [ "$SYSTEM_VERSION" == "CentOS Linux release 7" ];then
cat > /etc/systemd/system/mysqld.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=/usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf
LimitNOFILE=5000
EOF
systemctl enable mysqld
systemctl status mysqld
else
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
chkconfig --add mysqld
chkconfig --list mysqld
fi
echo 'The password is placed: /usr/local/mysql/data/.password'