Centos7安装/卸载MySQL8

目录

  • 1 卸载MySQL
    • 1.1 关闭服务
    • 1.2 卸载MySQL
    • 1.3 删除MySQL相关文件
  • 2 安装MySQL
    • 2.1 下载安装包
    • 2.2 检查依赖
    • 2.3 安装
    • 2.4 初始化
    • 2.5 允许远程连接
      • 2.5.1 centos7允许访问端口
      • 2.5.2 MySQL允许用户远程登录

1 卸载MySQL

1.1 关闭服务

查看是否安装过MySQL

rpm -qa | grep -i mysql

检查MySQL service

systemctl status mysqld

关闭MySQL服务

systemctl stop mysqld

1.2 卸载MySQL

查看当前MySQL安装情况

rpm -qa | grep -i mysql
# 或
yum list installed | grep mysql

卸载上述命令查询出的已安装程序

yum remove -y mysql-xxxx mysql-xxxx

1.3 删除MySQL相关文件

查找相关文件

find / -name mysql

删除

rm -rf /usr/lib64/mysql

删除 my.cnf

rm -rf /etc/my.cnf

2 安装MySQL

2.1 下载安装包

https://dev.mysql.com/downloads/mysql/

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ys1aOsHR-1680326839812)(D:\RenYi\typora_picture\image-20230331180919442.png)]

解压后选择需要用的安装包上传至Centos

mysql-community-client-8.0.25-1.el7.x86_64.rpm
mysql-community-client-plugins-8.0.25-1.el7.x86_64.rpm
mysql-community-common-8.0.25-1.el7.x86_64.rpm
mysql-community-libs-8.0.25-1.el7.x86_64.rpm
mysql-community-server-8.0.25-1.el7.x86_64.rpm

2.2 检查依赖

检查/tmp目录权限

chmod -R 777 /tmp

检查依赖

rpm -qa | grep libaio
rpm -qa | grep net-tools

2.3 安装

按顺序执行

rpm -ivh mysql-community-common-8.0.25-1.el7.x86_64.rpm
rpm -ivh mysql-community-client-plugins-8.0.25-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-8.0.25-1.el7.x86_64.rpm
rpm -ivh mysql-community-client-8.0.25-1.el7.x86_64.rpm
rpm -ivh mysql-community-server-8.0.25-1.el7.x86_64.rpm

执行rpm -ivh mysql-community-libs-8.0.25-1.el7.x86_64.rpm时报错

错误:依赖检测失败:
        mariadb-libs 被 mysql-community-libs-8.0.25-1.el7.x86_64 取代

解决办法:执行

yum -y remove mysql-libs

查看MySQL版本

mysql --version
rpm -qa | grep -i mysql

2.4 初始化

服务的初始化

为了保证数据库目录与文件的所有者为mysql登录用户,如果是以root身份运行mysql服务,需执行下面命令初始化

mysqld --initialize --user=mysql

说明:–initialize选项默认以安全模式初始化,会为root用户生成一个密码并将该密码标记为过期,登录后需要设置一个新的密码。生成的临时密码会在日志中记录一份。

查看密码

cat /var/log/mysqld.log

root@localhost:后面为初始密码

检查MySQL服务是否启动

systemctl status mysqld

启动MySQL服务

systemctl start mysqld

检查MySQL服务是否启动

systemctl status mysqld

查看MySQL服务是否自启动

systemctl list-unit-files | grep mysqld.service

若不是enabled,执行如下命令设置自启动

systemctl enable mysqld.service

登录MySQL

mysql -uroot -p

输入初始密码即可。

show databases;报错

mysql> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

修改密码

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';

退出MySQL Client,使用新密码重新登录即可。

2.5 允许远程连接

2.5.1 centos7允许访问端口

方式一:关闭防火墙

查看防火墙状态

systemctl status firewalld

关闭防火墙

systemctl stop firewalld

关闭防火墙自启动

systemctl disable firewalld

方式二:开放端口

查看开放的端口号

firewall-cmd --list-all

设置开放端口号

firewall-cmd --add-service=http --permanent
firewall-cmd --add-port=3306/tcp --permanent

重启防火墙

firewall-cmd --reload

2.5.2 MySQL允许用户远程登录

use mysql;
select host,user from user;
update user set host='%' where user = 'root';
flush privileges;

如果报错

Plugin caching_sha2_password could not be loaded;

MySQL执行

mysql> ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';

你可能感兴趣的:(mysql,数据库,centos)