本文最后更新于 952 天前,其中的信息可能已经有所发展或是发生改变。
官网下载
- 进入 MySQL官网。
- 选择DOWNLOADS跃迁点击MySQL Community (GPL) Downloads,在新页面点击MySQL Community Server。
- 在Select Operating System单选框中选择Red Hat Enterprise Linux / Oracle Linux,在Select OS Version单选框中选择Red Hat Enterprise Linux 7 / Oracle Linux 7 (x86, 64-bit)。
- 在Download Packages选择Compressed TAR Archive (mysql-8.0.26-el7-x86_64.tar.gz)点击Download下载。
安装MySQL
- 将
mysql-8.0.26-el7-x86_64.tar.gz移动到/usr/local文件夹下并解压tar -zxvf mysql-8.0.26-el7-x86_64.tar.gz - 添加
mysql用户
groupadd mysql #添加mysql用户组
useradd -r -g mysql mysql #给mysql用户组添加mysql用户
chown -R mysql:mysql /usr/local/mysql-8.0.26-el7-x86_64
- 进入
bin文件夹下执行
./mysqld --initialize --user=mysql --basedir=/usr/local/mysql-8.0.26-el7-x86_64 --datadir=/usr/local/mysql-8.0.26-el7-x86_64/data
注意:记录下代码第三行 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: XXXXXXXXXXXX 显示的临时密码(此处用X表示临时密码)。若是没记住也没事儿,删掉初始化的 datadir 目录,再执行一遍步骤1、2又会重新生成。(临时密码会在第一次使用后修改,如果没有修改密码的话会导致以后登陆不进去,具体修改密码方法会在之后说明)
- 编辑
/etc/my.cnf文件
[mysqld]
basedir=/usr/local/mysql-8.0.26-el7-x86_64
datadir=/usr/local/mysql-8.0.26-el7-x86_64/data
socket=/usr/local/mysql-8.0.26-el7-x86_64/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
[mysqld_safe]
log-error=/var/log/mysql/mysql.log
pid-file=/var/run/mysql/mysql.pid
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
查看所有的配置项,可参考:https://dev.mysql.com/doc/refman/8.0/en/mysqld-option-tables.html。
- 进入
support-files文件夹下执行./mysql.server start
登录MySQL
- 执行
mysql -u root -p,此时会提示输入密码,填入步骤3记录下的密码即可。 - 为 root 用户设置新密码
alter user 'root'@'localhost' identified by 'xxxxxx';。 - 在 MySQL8.0.4 以前,执行
SET PASSWORD=PASSWORD('[修改的密码]');就可以更改密码,但是从 MySQL8.0.4 开始这样不行了。因为之前,MySQL的密码认证插件是 mysql_native_password,而现在使用的是 caching_sha2_password。 因为当前有很多数据库工具和链接包都不支持 caching_sha2_password ,为了方便,我暂时还是改回了 mysql_native_password 认证插件。 - 在MySQL中执行命令:
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'xxxxxx';修改密码验证插件,同时修改密码(此处用x表示新密码)。 如果想默认使用 mysql_native_password 插件认证,可以在配置文件中配置 default_authentication_plugin 项。具体细节如下:
[mysqld]
default_authentication_plugin = mysql_native_password
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
- 设置允许远程连接
update user set user.Host='%' where user.User='root'。 - 修改完 my.cnf 文件需要重新启动MySQL服务
./mysql.server restart。
