Centos7安装MySql8.0踩坑

934 阅读6分钟

Centos7安装mysql8.0

制作时间:2022-08-11

环境

CentOS Linux release 7.8.2003 (Core)
mysql-8.0.28-1.el7.x86_64.rpm-bundle.tar

一、上传

将mysql上传到服务器中的一个文件夹,并解压(尽量新建一个文件夹,因为解压后文件会非常多,或者在解压时选择解压到某个目录)

# 在 /{Your.Folder} 目录下创建一个空的文件夹 mysql
mkdir /{Your.Folder}/mysql
# 进入这个新建的文件夹下
cd /{Your.Folder}/mysql
# 将文件进行解压
tar -xvf mysql-8.0.28-1.el7.x86_64.rpm-bundle.tar -C mysql-8.0.28

注:此处使用 tar -zxvf命令进行解压会报错,要使用-xvf进行解压(一个是解压.tar的压缩文件,一个是解压tar.gz的压缩文件的)

gzip: stdin: not in gzip format
tar: Child returned status 1
tar: Error is not recoverable: exiting now

二、安装

可以看到解压后的文件都是 rpm 文件,所以需要用到 rpm 包资源管理器相关的指令安装这些 rpm 的安装包,在安装执行 rpm 安装包之前先下载 openssl-devel 插件,因为 mysql 里面有些 rpm 的安装依赖于该插件。

yum install openssl-devel

然后使用rpm进行安装

  1. rpm -ivh mysql-community-common-8.0.28-1.el7.x86_64.rpm
  2. rpm -ivh mysql-community-client-plugins-8.0.28-1.el7.x86_64.rpm
  3. rpm -ivh mysql-community-libs-8.0.28-1.el7.x86_64.rpm
  4. rpm -ivh mysql-community-libs-compat-8.0.28-1.el7.x86_64.rpm
  5. rpm -ivh mysql-community-devel-8.0.28-1.el7.x86_64.rpm
  6. rpm -ivh mysql-community-client-8.0.28-1.el7.x86_64.rpm
  7. rpm -ivh mysql-community-server-8.0.28-1.el7.x86_64.rpm

执行第三条命令时报错

warning: mysql-community-libs-8.0.28-1.el7.x86_64.rpm: Header V4 RSA/SHA256 Signature, key ID 3a79bd29: NOKEY
error: Failed dependencies:
	mariadb-libs is obsoleted by mysql-community-libs-8.0.28-1.el7.x86_64

需要执行,先卸载mysql-libs,再安装

# 先卸载mysql-libs
yum remove mysql-libs
# 再执行安装
rpm -ivh mysql-community-libs-8.0.28-1.el7.x86_64.rpm

执行第6条时出错

warning: mysql-community-server-8.0.28-1.el7.x86_64.rpm: Header V4 RSA/SHA256 Signature, key ID 3a79bd29: NOKEY
error: Failed dependencies:
	libaio.so.1()(64bit) is needed by mysql-community-server-8.0.28-1.el7.x86_64
	libaio.so.1(LIBAIO_0.1)(64bit) is needed by mysql-community-server-8.0.28-1.el7.x86_64
	libaio.so.1(LIBAIO_0.4)(64bit) is needed by mysql-community-server-8.0.28-1.el7.x86_64
	mysql-community-icu-data-files = 8.0.28-1.el7 is needed by mysql-community-server-8.0.28-1.el7.x86_64

解决的方法就是在rpm 语句后面加上 --nodeps --force,安装时忽略依赖关系

# 在rpm -ivh mysql-community-server-8.0.28-1.el7.x86_64.rpm 添加参数--force --nodeps
rpm -ivh mysql-community-server-8.0.28-1.el7.x86_64.rpm --force --nodeps

到此,安装就算基本结束

三、启动

在 Linux 中 MySQL 安装好了之后系统会自动的注册一个服务,服务名称叫做 mysqld,所以可以通过以下命令操作 MySQL:

启动 MySQL 服务:systemctl start mysqld
重启 MySQL 服务:systemctl restart mysqld
关闭 MySQL 服务:systemctl stop mysqld

在使用systemctl start mysqld进行启动的时候,开始出错。

Job for mysqld.service failed because the control process exited with error code. See "systemctl status mysqld.service" and "journalctl -xe" for details.

使用systemctl status mysqld.service查看,一直为错误。

cat /var/log/mysqld.log发现并没有日志。

使用journalctl -xe进行查看错误,发现有报错。

/usr/sbin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory
Linux-centos版本的问题,有的版本缺少libaio.so.1文件
 #检查是否存在libaio.so.1文件
whereis libaio.so.1
libaio.so: /usr/lib64/libaio.so.1 
#若没有出现上面的路径则需要安装libaio.so.1文件
yum install -y libaio 
#重启mysql
systemctl restart mysqld 

继续报错,再次使用journalctl -xe进行查看错误。

chmod -R 777 /var/lib/mysql

执行了上面加权的命令,也不知道有没有用

这时查看mysqld.log,发现日志里面有东西了。

 [Server] /usr/sbin/mysqld (mysqld 8.0.28) initializing of server in progress as process 25745
 [Server] --initialize specified but the data directory has files in it. Aborting.
 [Server] The designated data directory /var/lib/mysql/ is unusable. You can remove all files that the server added to it.
 [Server] Aborting
 [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.28)  MySQL Community Server - GPL.
 [Server] /usr/sbin/mysqld (mysqld 8.0.28) starting as process 25747
 [Server] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new 
 [Server] Failed to find valid data directory.
 [Server] Data Dictionary initialization failed.
 [Server] Aborting
 [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.28)  MySQL Community Server - GPL.

vi /etc/my.cnf,对配置文件中数据存储的位置进行更改

并同时新建该目录

将datadir=/var/lib/mysql改为datadir=/var/lib/mysql/data
同时新建data文件夹

此时再次对mysql进行启动,启动成功。

四、登录

使用mysql -u root -p进行登录,一开始教程上说,日志中会有一个临时密码,但是我没有,别的教程说是可以直接回车,不用密码进入,但是同样报错。

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

如果你是以root身份安装和允许mysql服务,为了保证数据库目录与文件(默认/var/lib/mysql下)的所有者为mysql用户,需要执行下面的命令初始化。

--initialize选项默认以安全模式来初始化,则会为root用户生成一个临时密码,并将该密码标记为过期,登录后不能做其他任何操作,需要重新设置一个新的密码。生成的临时密码会记录在日志中,使用如下命令查看日志:

1、mysqld --initialize --user=mysql


2、cat /var/log/mysqld.log
A temporary password is generated for root@localhost: Chylwjiyz3,G
中Chylwjiyz3,G为密码

2、cat /var/log/mysqld.log A temporary password is generated for root@localhost: Chylwjiyz3,G 中Chylwjiyz3,G为密码

使用用户名和密码进行登录

登陆后,首先进行修改密码

ALTER USER 'root'@'localhost' IDENTIFIED BY '{Your.New.Password}'

注意password的格式,默认密码检查策略要求密码必须包含:大小写字母、数字和特殊符号。

使用flush privileges;让密码生效。

下一步是让root用户不在本地也可以登录(方便使用mysql客户端连接3306端口管理mysql),mysql8之中下面类似这条命令已经无效:

grant all privileges on test.* to root@'%' identified '123456';

提示:

RROR 1064(4200): you have an error in you SQL syntax; **near 'identified '123456'' at line 1

mysql8规定,分配权限不能带密码。于是使用下面这条命令:

grant all privileges on test.* to root@'%'

提示:

You are not allowed to create a user with GRANT;

产生用户不能授权的原因是mysql 数据库中user 表中的特定用户(root) 的host 的属性值为localhost.

解决方法:

# 使用mysql 数据库
mysql > use mysql;
# 特定用户的host 修改
mysql > update user set host='%' where user='root';
# 指定用户的授权
mysql > grant all privileges on test.* to root@'%'

 

五、卸载

卸载 MySQL 前需要先停止 MySQL

命令:systemctl stop mysqld

停止 MySQL 之后查询 MySQL 的安装文件:rpm -qa | grep -i mysql

卸载上述查询出来的所有的 MySQL 安装包

1. rpm -e mysql-community-client-plugins-8.0.26-1.el7.x86_64 --nodeps
2. rpm -e mysql-community-server-8.0.26-1.el7.x86_64 --nodeps
3. rpm -e mysql-community-common-8.0.26-1.el7.x86_64 --nodeps
4. rpm -e mysql-community-libs-8.0.26-1.el7.x86_64 --nodeps
5. rpm -e mysql-community-client-8.0.26-1.el7.x86_64 --nodeps
6. rpm -e mysql-community-libs-compat-8.0.26-1.el7.x86_64 --nodeps
删除MySQL的数据存放目录(因为一开始安装时更改郭数据存放目录,个人认为应该rm -rf /var/lib/mysql/data)
rm -rf /var/lib/mysql/
删除MySQL的配置文件备份
rm -rf /etc/my.cnf.rpmsave

六、尾巴

开机让MySQL自动启动

systemctl enable mysqld

通过客户端使用用户名和密码连接服务器的3360端口,注意centos的iptable以及服务器安全组设置,是否开放了3306端口。

参考文章:

Linux_Centos7在安装Mysql常见错误依赖时失败
Linux-安装MySQL(详细教程)