帮你排坑之centos 7使用Yum方式安装 mysql

环境准备

操作系统 :Centos 7  Mysql版本:5.7.X。

安装步骤

第一步:检查当前系统是否已经安装mysql

rpm -qa |grep mysql 

第二步:卸载已安装mysql

yum remove mysql57-community-release-el7-10.noarch

然后再执行第一步,查看是否完全卸载。

第三步:下载mysql5.7版本RPM文件

wget https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm

如果提示wget命令不存在,则可以使用yum安装wget

 yum -y install wget

第四步:安装本地RPM包,加载依赖关系

yum -y localinstall mysql57-community-release-el7-11.noarch.rpm

第五步:安装mysql服务

yum -y install mysql-community-server

如果报错

The GPG keys listed for the "MySQL 5.7 Community Server" repository are already installed but they are not correct for this package.
Check that the correct key URLs are configured for this repository.

 Failing package is: mysql-community-server-5.7.37-1.el7.x86_64
 GPG Keys are configured as: file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql


是RPM-GPG-KEY-mysql版本问题,需要执行以下命令

rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022

第六步:启动mysql服务

systemctl start mysqld

在这一步可能会有一些坑,致使mysql服务无法启动

问题1:mysql初始化错误

1、查看错误信息

根据启动mysql服务时的提示信息,使用 systemctl status mysqld 命令,查看信息

mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled)
   Active: activating (start) since Tue 2021-04-27 20:30:34 PDT; 536ms ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 10359 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
  Control: 10387 (mysqld)
   CGroup: /system.slice/mysqld.service
           ├─10387 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
           └─10390 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid

Apr 27 20:30:34 localhost.localdomain systemd[1]: Starting MySQL Server...
Apr 27 20:30:34 localhost.localdomain mysqld_pre_systemd[10359]: 2021-04-28T03:30:34.574515Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --expli... details).
Apr 27 20:30:34 localhost.localdomain mysqld_pre_systemd[10359]: 2021-04-28T03:30:34.576439Z 0 [ERROR] --initialize specified but the data directory has files in it. Aborting.
Apr 27 20:30:34 localhost.localdomain mysqld_pre_systemd[10359]: 2021-04-28T03:30:34.576492Z 0 [ERROR] Aborting
Hint: Some lines were ellipsized, use -l to show in full.

根据以上提示的错误信息可以看出来,说是mysql的data 目录下包含文件,无法进行初始化,所以需要把mysql的data文件夹删除,然后重新使用 initialize命令进行初始化

2、解决问题方式

mysql的配置文件在 /etc目录下,可以使用  vim  /etc/my.cnf   命令查看mysql的data目录

datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

在删除 /var/lib/mysql文件夹时,需要查看mysql线程是否启动,如果mysql进程已经启动,则使用 kill -9 pid 杀掉进程

然后删除 /var/lib/mysql 文件夹,删除以后查看mysql 是否仍然存在,如果没有删除的话,则使用  

netstat -anpt |grep 3306

命令查看3306端口状态,如果该状态为 LISTEN 状态,则需要使用 lsof -i:3306  命令查看使用3306端口的进程PID,然后将该进程 KILL掉,然后再去删除mysql 文件夹。

删除mysql文件夹后,则使用

mysqld --initialize --user=mysql

进行mysql的初始化操作。

问题二: Linux安全增强模块selinux 阻止mysql启动。

1、问题分析

使用  systemctl status mysqld 命令查询结果:

从以上信息并不能看到mysql服务启动失败的原因,所以就需要查看mysql的启动日志了  可以使用 vim /var/log/mysqld.log  查看mysql启动日志

2021-04-28T03:49:30.528849Z 0 [ERROR] InnoDB: Operating system error number 13 in a file operation.
2021-04-28T03:49:30.528884Z 0 [ERROR] InnoDB: The error means mysqld does not have the access rights to the directory.
2021-04-28T03:49:30.528893Z 0 [ERROR] InnoDB: os_file_get_status() failed on './ibdata1'. Can't determine file permissions
2021-04-28T03:49:30.528904Z 0 [ERROR] InnoDB: Plugin initialization aborted with error Generic error
2021-04-28T03:49:31.135274Z 0 [ERROR] Plugin 'InnoDB' init function returned error.
2021-04-28T03:49:31.135380Z 0 [ERROR] Plugin 'InnoDB' registration as a STORAGE ENGINE failed.
2021-04-28T03:49:31.135399Z 0 [ERROR] Failed to initialize builtin plugins.
2021-04-28T03:49:31.135406Z 0 [ERROR] Aborting

由以上错误信息可以看出,mysqld对目录没有操作权限。

2、解决方法

该问题是由Linux自带的安全增强模块selinux 引起的,所以需要关闭该模块。

setenforce 0   临时关闭selinux安全增强模块 

还可以永久关闭selinux模块

使用 vi /etc/sysconfig/selinux 设置为 SELINUX=disabled

然后使用  sestatus 查看selinux 状态

当以上两个问题解决以后,重启mysql服务。

第七步:修改root用户登录密码

在mysql安装完成以后,会在 /var/log/mysqld.log文件中给root用户生成一个临时默认密码,使用该密码进行第一次登录mysql系统。

mysql -u root -p

然后输入root用户的临时密码,登录mysql系统以后,需要修改密码,没有修改密码会报错

ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

在5.7.6版本之前需要使用

mysql> SET PASSWORD = PASSWORD('new password');

5.7.6版本之后需要使用

ALTER USER USER() IDENTIFIED BY 'new password';

至此,mysql安装步骤就完成了。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值