跳转至

CentOS

安装MySQL5.7

官方文档

yum install -y libaio
wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.44-linux-glibc2.12-x86_64.tar.gz
tar zxvf mysql-5.7.44-linux-glibc2.12-x86_64.tar.gz
mv mysql-5.7.44-linux-glibc2.12-x86_64 /opt/mysql
mkdir /opt/mysql/{data,mysql-files,log}

cat <<EOF >/etc/my.cnf
[mysqld]
datadir=/opt/mysql/data
socket=/tmp/mysql.sock
port=3306
log-error=/opt/mysql/log/localhost.localdomain.err
user=mysql
secure_file_priv=/opt/mysql/mysql-files
bind-address=0.0.0.0
EOF

cat <<EOF >/usr/lib/systemd/system/mysql.service
[Unit]
Description=MySQL Server
Documentation=man:mysqld(7)
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
Type=forking
PIDFile=/opt/mysql/mysql.pid

# Disable service start and stop timeout logic of systemd for mysqld service.
TimeoutSec=0

# Start main service
ExecStart=/opt/mysql/bin/mysqld --basedir=/opt/mysql --datadir=/opt/mysql/data --daemonize --pid-file=/opt/mysql/mysql.pid

# Use this to switch malloc implementation
EnvironmentFile=-/etc/sysconfig/mysql

# Sets open_files_limit
LimitNOFILE = 5000
Restart=on-failure
RestartPreventExitStatus=1
PrivateTmp=false
EOF

/opt/mysql/bin/mysqld --basedir=/opt/mysql --datadir=/opt/mysql/data --user=mysql --initialize

systemctl enable --now mysql
cat /opt/mysql/log/localhost.localdomain.err |grep password
mysql -uroot -p
## 修改默认密码
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'yourpassword';
mysql> use mysql;
## 允许外部访问
mysql> update user set host = '%' where user = 'root';
mysql> FLUSH PRIVILEGES;

## 配置MySQL环境变量
cat <<EOF >/etc/profile.d/mysql.sh
export MYSQL_HOME=/opt/mysql
export CLASSPATH=.:$MYSQL_HOME/lib
export PATH=$PATH:$MYSQL_HOME/bin
EOF

source /etc/profile.d/mysql.sh