Linux MySQL5.7.26部署

该文详细介绍了如何在Linux环境下手动安装和配置MySQL5.7.26,包括下载安装包、解压、创建数据目录、设置用户权限、配置my.cnf、初始化数据库、设置环境变量、启动服务以及密码管理。此外,还提供了使用脚本自动化部署的步骤。
摘要由CSDN通过智能技术生成

手动部署

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'
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值