Centos8使用yum安装mysql8.x

1: 查询在centos8  中是否安装了  mysql ;   安装过则直接卸载在安装, 没有安装过则直接开始安装。

     命令:  rpm -qa | grep mysql ,  如果安装了就会出现如下结果。

     Centos8使用yum安装mysql8.x

    卸载:使用如下命令进行移除安装的包。

   yum  remove  XXX

Centos8使用yum安装mysql8.x

 

Centos8使用yum安装mysql8.x

 

移除安装的配置:使用如下命令查找配置:

find / -name mysql

可能显示的结果如下: 使用   rm  -rf  删除 查找到的配置。

Centos8使用yum安装mysql8.x

 

查看是否安装了mariadb;

rpm -pa | grep mariadb

如果有安装的话使用如下命令移除:

rpm -e XXX

rpm -e --nodeps xxx  //强制删除

 

 

2: 安装

centos的yum 源中默认是没有mysql的,所以我们需要先去官网下载mysql的repo源并安装;

地址:https://dev.mysql.com/downloads/repo/yum/

Centos8使用yum安装mysql8.x

 

 

   将下载的 mysql80-community-release-el8-1.noarch.rpm  上传至 liunx

  Centos8使用yum安装mysql8.x

  安装   mysql80-community-release-el8-1.noarch.rpm

  rpm  -ivh  mysql80-community-release-el8-1.noarch.rpm;

  执行结果: 会在 /etc/yum.repos.d/ 目录下生成两个文件:

Centos8使用yum安装mysql8.x

更新  yum 命令

   1:  yum clean all 

   2: yum makecache

 

安装 mysql

   命令: yum -y install mysql-community-server

Centos8使用yum安装mysql8.x

安装完成

Centos8使用yum安装mysql8.x

Centos8使用yum安装mysql8.x

 

加入 开机启动:systemctl enable mysqld.service

停止服务: systemctl stop mysqld.service

启动服务: systemctl start  mysqld.service

重启服务: service mysqld restart

 

centos7  默认的防火墙是  firewall 

放开  3306端口:

firewall-cmd --zone=public --add-port=3306/tcp --permanent

firewall-cmd --reload;  //重新加载配置

我的是华为云的服务器,已经在安全组中配置好了,这里不需要自己配置开放端口。

 

开启服务:systemctl start mysqld.service

mysql5.8  有初始密码:

  命令:  grep "password" /var/log/mysqld.log

Centos8使用yum安装mysql8.x

 

登录: mysql -u root -p        

输入上图红色框的密码, 可以进入数据库中,但是必须要修改密码才行。如果登陆不进去,那就是密码没有输对,实在不行就先按照以下步骤进行

1.vim /etc/my.cnf [mysqld]后面加入skip-grant-tables,这个是用来跳过密码验证的,前面的#给放开

Centos8使用yum安装mysql8.x

2.重启mysql  service mysqld restart

3.登录到数据库mysql -u root -p,passwrod:直接回车;

4.mysql>use mysql ; (不要去掉;必须要选定数据库才行,不然会报错)

5.update user set authentication_string='' where user='root';(先置为空,不可直接设置密码,切记!)

6.退出mysql, 删除/etc/my.cnf文件下面的 skip-grant-tables或者注释掉,然后重启mysql服务;

7.再次登录到数据库mysql -u root -p

8.passwrod:直接回车;

登陆进去后,接下来就是修改密码,这里坑很多,

输入:alter user 'root'@'%' identified  by '你的密码';

可能会出错,ERROR 1396 (HY000): Operation ALTER USER failed for 'root'@'localhost',

原因为MySql 8.0.11 换了新的身份验证插件(caching_sha2_password), 原来的身份验证插件为(mysql_native_password)。而客户端工具Navicat Premium12 中找不到新的身份验证插件(caching_sha2_password),对此,我们将mysql用户使用的  登录密码加密规则  还原成  mysql_native_password,即可登陆成功。

 

mysql> use mysql;

通过命令 select user,plugin from user where user='root'; 我们可以发现加密方式是caching_sha2_password

Centos8使用yum安装mysql8.x

mysql> select user,host from user;

Centos8使用yum安装mysql8.x

注意我的root,host是'%' ,这个%代表所有ip都可以连接,如果这里改成localhost,就只有本机才能连接,修改加密方式, 让 navicat  可以远程连接。 不能使用update, 会出现登录不上的情况。plugin 代表加密方式     需要修改为     mysql_native_password

然后使用命令:alter user '你的用户名'@'%' identified with mysql_native_password by '你的密码';

再次查看,就发现加密方式已经更改

Centos8使用yum安装mysql8.x

Centos8使用yum安装mysql8.x

接着找到my.cnf文件,并加上加密方式:我通过yum安装,默认的就在/etc/my.cnf,

Centos8使用yum安装mysql8.x

保存后,重启mysql服务命令:service mysqld restart

需要注意的点:

1.从mysql5.8开始,修改密码用authentication_string,而不是password;

2.update user set authentication_string='Qzy1234#' where user='root';如果这样修改,可以执行成功,不会报错,但是修改后的密码是明文,通过select host, user, authentication_string, plugin from user;可以查看相关用户的访问权限、加密方式等,密码没加密,mysql是拒绝访问的,因为authentication_string字段下只能是mysql加密后的41位字符串密码;所以这样修改不报错,但是还是不能登录!

3.关于密码加密,5.8.0默认用caching_sha2_password,8.0之前用的mysql_native_password,有些客户端还不支持caching_sha2_password,可以改成mysql_native_password==>同样是登录mysql后执行:ALTER USER 'root'@'localhost' IDENTIFIED with mysql_native_password BY '你的密码' ;

 

 

 

 

 

远程连接:  root  的  host  是  localhost 需要修改

 flush  privileges 刷新配置

update user set host='%' where user = 'root';   改为任何IP可连接

update user set host='192.168.0.41' where user = 'root';   改为IP为192.168.0.41 可连接  

Centos8使用yum安装mysql8.x

Centos8使用yum安装mysql8.x

 

至此,navicat 就能连接上了。

Centos8使用yum安装mysql8.x


mysql  创建数据库:

 

create schema [数据库名称] default character set utf8 collate utf8_general_ci;--创建数据库

 

密码忘记不能登录:   在/etc/my.cnf 文件中加入:   skip-grant-tables    可以免密登录。 修改完密码就要删除或注释这个参数

Centos8使用yum安装mysql8.x

 

//出现如下错误时可以在mysql下输入:  flush  privileges  刷新配置信息。

Centos8使用yum安装mysql8.x