1.下载
wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.37-linux-glibc2.28-x86_64.tar.xz
2.解压
# 解压
tar -xvf mysql-8.0.37-linux-glibc2.28-x86_64.tar.xz
# 移动到指定目录
mv mysql-8.0.37-linux-glibc2.28-x86_64 /opt/mysql
3.创建用户
groupadd mysql
useradd -r -g mysql -s /bin/false mysql
4.创建数据目录
mkdir -p /opt/mysql/data
chown -R mysql:mysql /opt/mysql
# 创建日志文件
touch /opt/mysql/error.log
# 修改权限
chown mysql:mysql /opt/mysql/error.log
# 确保目录权限也对
chown -R mysql:mysql /opt/mysql
5.初始化数据库
/opt/mysql/bin/mysqld --initialize --user=mysql --basedir=/opt/mysql --datadir=/opt/mysql/data
执行后会输出:
# 需要记住这个输出的密码
A temporary password is generated for root@localhost: xxxx
6.配置 my.cnf
vim /etc/my.cnf
# 写入:
[client]
socket=/opt/mysql/mysql.sock
[mysqld]
basedir=/opt/mysql
datadir=/opt/mysql/data
socket=/opt/mysql/mysql.sock
pid-file=/opt/mysql/mysqld.pid
port=3306
character-set-server=utf8mb4
collation-server=utf8mb4_general_ci
# 关闭DNS解析(强烈建议)
skip-name-resolve
# 日志
log-error=/opt/mysql/error.log
7.启动 MySQL
启动后,新建或复制会话,在新窗口继续第 8 步一下操作
/opt/mysql/bin/mysqld_safe --user=mysql &
8.登录 MySQL
/opt/mysql/bin/mysql -uroot -p
# 输入刚才记下来的临时密码
9.修改 root 密码
ALTER USER 'root'@'localhost' IDENTIFIED BY 'Root@123456';
10.设置允许远程连接
CREATE USER 'root'@'%' IDENTIFIED BY 'Root@123456';
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;
FLUSH PRIVILEGES;
11.设置开机启动
cp /opt/mysql/support-files/mysql.server /etc/init.d/mysql
chkconfig --add mysql
chkconfig mysql on service mysql restart
service mysql status