• CentOS7安装MySQL8(Red Hat版)


    一、 下载资源包

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

    注意:资源包的名称的解读:mysql-8.0.30-1.el9.x86_64.rpm-bundle.tar 其中el9指的linux9,我们用centos应选el7的版本。
    在这里插入图片描述
    下载后是个tar压缩包,解压后得到以下文件:
    在这里插入图片描述
    使用FTP、SecureFX等工具上传的服务器。安装过程用到的有5-6个文件,但是都要上传到服务器

    二、安装

    2.1 先卸载MariaDB

    在CentOS中默认安装有MariaDB,是MySQL的一个分支,主要由开源社区维护。
    CentOS 7及以上版本已经不再使用MySQL数据库,而是使用MariaDB数据库。
    如果直接安装MySQL,会和MariaDB的文件冲突。
    因此,需要先卸载自带的MariaDB,再安装MySQL。

    [liangyan@localhost ~]$ su root   # 切换到root
    密码:
    [liangyan@localhost ~]$ rpm -qa|grep mariadb       # 查看 mariadb 版本
    mariadb-devel-5.5.68-1.el7.x86_64
    mariadb-libs-5.5.68-1.el7.x86_64
    [root@localhost liangyan]# rpm -e --nodeps mariadb-libs-5.5.68-1.el7.x86_64   # 删除
    [root@localhost liangyan]# rpm -e --nodeps mariadb-devel-5.5.68-1.el7.x86_64  # 全部删除
    [liangyan@localhost ~]$ rpm -qa|grep mariadb   # 检查是否删除干净
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    2.2检查依赖

    [root@iZm5e67d7rpuq85thz5q5iZ home]# rpm -qa|grep libaio  # 检查依赖libaio,
    [root@iZm5e67d7rpuq85thz5q5iZ home]# yum install libaio  # 如果没有就安装依赖
    [root@iZm5e67d7rpuq85thz5q5iZ home]# rpm -qa|grep net-tools  # 检查依赖net-tools
    net-tools-2.0-0.25.20131004git.el7.x86_64    # 依赖存在不用安装,否则yum install net-tools 安装依赖
    
    • 1
    • 2
    • 3
    • 4

    2.3 安装mysql

    将上述rpm包上传到服务器后,在文件目录下,依次执行以下命令(注意必须按顺序执行,否则可能会包缺少相关依赖的错误)

    rpm -ivh mysql-community-common-8.0.30-1.el7.x86_64.rpm
    rpm -ivh mysql-community-client-plugins-8.0.30-1.el7.x86_64.rpm
    rpm -ivh mysql-community-libs-8.0.30-1.el7.x86_64.rpm
    rpm -ivh mysql-community-client-8.0.30-1.el7.x86_64.rpm
    rpm -ivh mysql-community-server-8.0.30-1.el7.x86_64.rpm
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    2.3.1在安装第三个包的时候如果出现如下错误:

    在这里插入图片描述
    需要解除之前安装过的依赖即可,执行下面命令:

    yum remove mysql-libs
    
    • 1

    2.3.2 在安装地五个包的时候如果出现如下错误:

    在这里插入图片描述
    需要安装libnuma依赖,执行如下命令解决:

    yum install libnuma*
    
    • 1

    2.3.3 在安装地五个包的时候如果出现如下错误:

    在这里插入图片描述
    需要先安装包mysql-community-icu-data-files-8.0.30-1.el7.x86_64.rpm

     rpm -ivh mysql-community-icu-data-files-8.0.30-1.el7.x86_64.rpm  # 先安装依赖包
     rpm -ivh mysql-community-server-8.0.30-1.el7.x86_64.rpm  # 再安装第五个包
    
    • 1
    • 2

    2.4 检测是否安装成功

    [root@iZm5e67d7rpuq85thz5q5iZ mysql-8.0.30-1.el7.x86_64.rpm-bundle]# mysql --version  # 查看版本
    mysql  Ver 8.0.30 for Linux on x86_64 (MySQL Community Server - GPL)
    [root@iZm5e67d7rpuq85thz5q5iZ mysql-8.0.30-1.el7.x86_64.rpm-bundle]# mysqladmin --version
    mysqladmin  Ver 8.0.30 for Linux on x86_64 (MySQL Community Server - GPL)
    [root@iZm5e67d7rpuq85thz5q5iZ mysql-8.0.30-1.el7.x86_64.rpm-bundle]# rpm -qa|grep -i mysql   # 查看安装的相关包
    mysql-community-common-8.0.30-1.el7.x86_64
    mysql-community-icu-data-files-8.0.30-1.el7.x86_64
    mysql-community-client-plugins-8.0.30-1.el7.x86_64
    mysql-community-client-8.0.30-1.el7.x86_64
    mysql-community-server-8.0.30-1.el7.x86_64
    mysql-community-libs-8.0.30-1.el7.x86_6
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    rpm 是Redhat Package Manage缩写,通过RPM的管理,用户可以把源代码包装成以rpm为扩展名的文件形式,易于安装。
    -i , —install 安装软件包
    -v , —verbose 提供更多的详细信息输出
    -h , —hash 软件包安装的时候列出哈希标记 (和 -v 一起使用效果更好),展示进度条

    三、mysql初始化

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

    mysqld --initialize --user=mysql
    
    • 1

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

    cat /var/log/mysqld.log 
    
    • 1

    在这里插入图片描述
    root@localhost: 后面就是初始化的密码

    四、启动MySQL,查看状态

    systemctl start mysqld.service   # 启动  可以不带后缀.service
    systemctl stop mysqld.service   # 关闭: 可以不带后缀.service
    systemctl restart mysqld.service   # 重启: 可以不带后缀.service
    systemctl status mysqld.service   # 查看状态  可以不带后缀.service
    
    • 1
    • 2
    • 3
    • 4

    4.1查看状态有 active(running)代表正常运行

    在这里插入图片描述

    4.2查看进程: 有下面两行正常运行

    [root@iZm5e67d7rpuq85thz5q5iZ mysql]# ps -ef | grep -i mysql
    mysql     7255     1  0 18:26 ?        00:00:02 /usr/sbin/mysqld
    root      7416  4776  0 18:43 pts/0    00:00:00 grep --color=auto -i mysql
    
    • 1
    • 2
    • 3

    4.3 查看MySQL服务是否自启动

    systemctl list-unit-files|grep mysqld.service

    [root@iZm5e67d7rpuq85thz5q5iZ mysql]# systemctl list-unit-files|grep mysqld.service
    mysqld.service                                enabled 
    
    • 1
    • 2

    enabled 是自启动,如不是enabled可以运行如下命令设置自启动:

    systemctl enable mysqld.service
    
    • 1

    如果希望不进行自启动,运行如下命令设置

    systemctl disable mysqld.service
    
    • 1

    五、登录

    5.1 mysql -hlocalhost -P3306 -uroot -p 或者 mysql -uroot -p 回车输入上面的临时密码登录

    [root@iZm5e67d7rpuq85thz5q5iZ mysql]# mysql -uroot -p
    Enter password: 
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 13
    Server version: 8.0.30 MySQL Community Server - GPL
    
    Copyright (c) 2000, 2022, Oracle and/or its affiliates.
    
    Oracle is a registered trademark of Oracle Corporation and/or its
    affiliates. Other names may be trademarks of their respective
    owners.
    
    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    
    mysql> 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    5.2 修改密码

    ALTER USER 'root'@'localhost' IDENTIFIED BY 'mypassword';
    
    • 1

    注意:通过临时密码登录时可以使用上面的语句进行修改密码,如果使用免密登录上面语句不能使用,重置密码为空,刷新权限,退出重新登录后再使用上面方式设置密码。

    5.3 远程登录设置

    先进入mysql 后,执行命令

    use mysql; 
    select Host,User from user;
    
    
    • 1
    • 2
    • 3

    可以看到root用户的当前主机配置信息为localhost(仅允许本机登录),修改为通配符%表示所有IP都有连接权限。注意:在生产环境下不能为了省事将host设置为%,这样做会存在安全问题,具体的设置可以根据生产环境的IP进行设置。 安全起见可以设置为某个指定ip,或者通过服务器的防火墙设置仅某个ip可以访问3306端口。

    update user set host = '%' where user ='root';
    select Host,User from user;
    flush privileges;    # 不要忘记刷新权限
    
    • 1
    • 2
    • 3

    六 补充:

    6.1如果是 MySQL8 版本,navicat连接时可能出现无法连接的2058错误。

    分析是 mysql 密码加密方法变了。
    登录mysql 执行语句:

    ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'my_password';
    
    • 1

    重新配置navicat的连接即可。

    6.2 关闭防火墙

    如果是云服务器一般用不到

    systemctl start firewalld.service 
    systemctl status firewalld.service 
    systemctl stop firewalld.service 
    #设置开机启用防火墙 
    systemctl enable firewalld.service 
    #设置开机禁用防火墙 
    systemctl disable firewalld.service
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    6.3 检查运行端口

    netstat -lntp #查看监听(Listen)的端口
    netstat -antp #查看所有建立的TCP连接
    netstat -anpt | grep 3306 # 查看端口是否被监听

    6.4 centos7 使用rpm安装 Red Hat mysql8各文件的位置

    my.cnf 在etc/my.cnf
    mysql.sock 在 /var/lib/mysql/mysql.sock(注意这个mysql.sock开始是没有的启动后自动创建,如果没有创建在这里就需要做个软连接过来)

    rpm -qal |grep mysql #查看mysql所有安装包的文件存储位置
    yum search mysql #查找对应可以安装的软件包
    find / -name mysql #查找所有包含mysql服务的文件路径
    which 文件名 #查看指定文件路径
    Whereis 文件名 #通过本地架构好的数据库索引查找
    确定这个软件已经开启,则可以通过ps -aux |grep mysql,找出对一个的可执行文件所在的目录
    [root@localhost yum.repos.d]# ps -aux |grep mysql

  • 相关阅读:
    Python之字符串常用操作
    Windows OpenGL 图像色调
    ESP8266_01S+刷入AT固件+保姆级教学+USB验证AT指令
    [MAUI]集成高德地图组件至.NET MAUI Blazor项目
    【Git进阶】基于文件(夹)拆分大PR
    postgreSql 和mysql的一些区别
    LeetCode //C - 69. Sqrt(x)
    使用 MySQL 日志 | 二进制日志 - Part 2
    go 并发编程之-工作池
    STL笔记
  • 原文地址:https://blog.csdn.net/wuwei_201/article/details/126319478