rpm -ivh mysql-community-common-8.0.22-1.el7.x86_64.rpm --force --nodeps
rpm -ivh mysql-community-libs-8.0.22-1.el7.x86_64.rpm --force --nodeps
rpm -ivh mysql-community-libs-compat-8.0.22-1.el7.x86_64.rpm --force --nodeps
rpm -ivh mysql-community-client-8.0.22-1.el7.x86_64.rpm --force --nodeps
rpm -ivh mysql-community-embedded-compat-8.0.22-1.el7.x86_64.rpm --force --nodeps
rpm -ivh mysql-community-devel-8.0.22-1.el7.x86_64.rpm --force --nodeps
rpm -ivh mysql-community-client-plugins-8.0.22-1.el7.x86_64.rpm --force --nodeps
rpm -ivh mysql-community-server-8.0.22-1.el7.x86_64.rpm --force --nodeps
rpm -qa | grep mysql
MySQL 8.0安装以及初始化错误解决方法
官网下载
mysql官网下载链接:https://dev.mysql.com/downloads/mysql/
- Select Operating System 中选择 “Red Hat Enterprise Linux / Oracle Linux”
- Select OS Version 中选择 “Red Hat Enterprise Linux / Oracle Linux 7(x86, 64-bit)”
- 点击Download(mysql-8.0.17-1.el7.x86_64.rpm-bundle.tar)
CentOS7环境下的具体安装步骤
- 检测本地是否有mysql已存在的包:
rpm -qa | grep mysql
- 1
- 检测本地是否有mariadb已存在的包:
rpm -qa | grep mariadb
- 1
如果存在,则使用yum命令卸载:
rpm -e --nodeps mariadb-libs-5.5.56-2.el7.x86_64
- 1
- 创建一个文件夹,上传jar包到/opt/software/mysql:
mkdir /opt/software/mysql
- 1
上传包有xftp等工具
- 解压mysql jar包:
mkdir /opt/module/mysql
tar -xvf /opt/software/mysql-8.0.16-2.el7.x86_64.rpm-bundle.tar -C /opt/module/mysql
- 1
- 2
- 查看解压后的文件:
ll /opt/module/mysql
- 1
- 利用rpm包安装mysql:
如官网所述:
In most cases, you need to install the mysql-community-server, mysql-community-client, mysql-community-libs, mysql-community-common, and mysql-community-libs-compat packages to get a functional, standard MySQL installation. To perform such a standard, basic installation, go to the folder that contains all those packages (and, preferably, no other RPM packages with similar names), and issue the following command:
依次安装MySQL的组件
- mysql-community-common
rpm -ivh mysql-community-common-8.0.16-2.el7.x86_64.rpm
- 1
- mysql-community-libs
rpm -ivh mysql-community-libs-8.0.16-2.el7.x86_64.rpm
- 1
- mysql-community-libs-compat
rpm -ivh mysql-community-libs-compat-8.0.16-2.el7.x86_64.rpm
- 1
- mysql-community-client
rpm -ivh mysql-community-client-8.0.16-2.el7.x86_64.rpm
- 1
- mysql-community-server
rpm -ivh mysql-community-server-8.0.16-2.el7.x86_64.rpm
- 1
安装完成后 可以查看已经安装的组件:
rpm -qa | grep mysql
- 1
- 启动mysql(初次启动mysqld,会自动进行初始化工作)
systemctl start mysqld
- 1
初始化后,会产生临时密码,查询指令如下:
cat /var/log/mysqld.log | grep password
- 1
- 登录MySQL服务:
mysql -uroot -p
- 1
输入产生的临时密码
- 更改密码
由于MySQL8.0 有密码验证组件,若希望设置简单的密码,需要修改服务验证条件,可设置的验证条件如官网所示:
# 密码检查等级,0/LOW、1/MEDIUM、2/STRONG
set global validate_password.policy=0;
# 密码的最短长度
set global validate_password.length=6;
# 密码至少要包含的小写字母个数和大写字母个数
set global validate_password.mixed_case_count=0;
# 设置密码
ALTER USER 'root'@'localhost' IDENTIFIED BY 'your password';
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 修改远程登录权限
USE mysql;
UPDATE mysql.user SET host = '%' WHERE user = 'root';
flush privileges;
SELECT host, user FROM user;
- 1
- 2
- 3
- 4
初始化MySQL发生错误的解决方法
使用初始化指令
mysqld --initialize --user=mysql
- 1
若发生错误,可能由于 /etc/my.cnf 中datadir指定的文件夹未清除,本机中为 /var/lib/mysql,具体的datadir查看本机配置文件中指定的路径。
vim /etc/my.cnf
- 1
删除datadir指定的文件夹
rm -rf /var/lib/mysql
- 1
清楚 log-error 指定的日志文件中的内容, 本机中为 /var/log/mysqld.log,具体的datadir查看本机配置文件中指定的路径。
注意,如果删除 /var/log/mysqld.log 了该文件,需要重新创建该文件,并修改文件访问权限
rm -f /var/log/mysqld.log
touch /var/log/mysqld.log
chmod 777 /var/log/mysqld.log
- 1
- 2
- 3
最后重新初始化即可!
忘记数据库root密码
主要通过无密码启动的方式启动mysqld服务,然后进入数据库清除原始密码的加密值,然后再次重启服务,进入数据库设置密码。
- 修改配置文件并重启服务
vim /etc/my.cnf
# 在my.cnf中最后一行添加 skip-grant-tables
skip-grant-tables
# 重启服务
systemctl restart mysqld.service
- 1
- 2
- 3
- 4
- 5
- 进入数据库清除原始root密码的加密记录
# password直接回车
mysql -u root -p
# 清除记录
UPDATE mysql.user SET authentication_string='' WHERE user='root';
SELECT host, user, authentication_string, plugin FROM mysql.user;
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 修改配置文件,再次重启服务,然后进入数据库设置密码
vim /etc/my.cnf
删除 skip-grant-tables
# 重启服务
systemctl restart mysqld.service
# password直接回车
mysql -u root -p
# 设置root密码 我的host项已经修改为%了。如果没有修改,则使用:ALTER user 'root'@'localhost' IDENTIFIED BY '123456'
grant system_user on *.* to 'root';
ALTER user 'root'@'%' IDENTIFIED BY '123456';
# WITH mysql_native_password 这个可以根据自己需要的认证方式修改(caching_sha2_password、mysql_native_password 两种)
ALTER user 'mysql.infoschema'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';
ALTER user 'mysql.session'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';
ALTER user 'mysql.sys'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';
# 完成修改,重启服务。
systemctl restart mysqld.service