• 【MySQL 系列】在 CentOS 上安装 MySQL


    CentOS 是一个使用非常广泛的 Linux 发行版,CentOS 属于 RedHat 架构。本篇文章中,我们展示了在 CentOS 8/7/6 上安装 MySQL 8 的详细步骤。



    1、先决条件

    使用 root 用户,或者具有管理员权限的用户登录系统,完成以下操作。

    2、在 CentOS 中安装 MySQL
    2.1、下载安装 MySQL Yum 仓库

    按照自己不同的系统执行以下命令下载安装 MySQL Yum 仓库:

    CentOS 8

    wget https://repo.mysql.com/mysql80-community-release-el8-1.noarch.rpm
    yum localinstall mysql80-community-release-el8-1.noarch.rpm
    
    • 1
    • 2

    CentOS 7

    wget https://repo.mysql.com/mysql80-community-release-el7-1.noarch.rpm
    yum localinstall mysql80-community-release-el7-1.noarch.rpm
    
    • 1
    • 2

    CentOS 6

    wget https://dev.mysql.com/get/mysql80-community-release-el6-1.noarch.rpm
    yum localinstall mysql80-community-release-el6-1.noarch.rpm
    
    • 1
    • 2
    2.2、安装 MySQL 8 社区服务器

    执行以下命令安装 MySQL 8:

    yum install mysql-community-server -y
    
    • 1
    2.3、启动 MySQL 服务

    根据自己不同的系统版本使用以下命令启动 MySQL 服务:

    CentOS 8 或 CentOS 7

    systemctl start mysql
    
    • 1

    CentOS 6

    service mysqld start
    
    • 1
    2.4、显示 root 用户的默认密码

    安装 MySQL 8.0 时,会自动为 root 用户生成一个临时密码,并记录在日志文件里。使用以下命令查看 root 用户的临时密码:

    grep "A temporary password" /var/log/mysqld.log
    
    • 1

    这是输出:

    [Note] A temporary password is generated for root@localhost: Liaka*(Dka&^Kjs
    
    • 1

    请注意,我们本地的临时密码是不同的。要根据此密码来更改 root 用户的密码。

    2.5、MySQL 安全配置

    执行以下 mysql_secure_installation 命令来保护 MySQL 服务器:

    mysql_secure_installation
    
    • 1

    它会提示我们输入 root 帐户的当前密码:

    Enter password for user root:
    
    • 1

    输入上面的临时密码,然后按下回车键。将显示以下消息:

    The existing password for the user account root has expired. Please set a new password.
    
    New password:
    Re-enter new password:
    
    • 1
    • 2
    • 3
    • 4

    输入 root 用户的新密码和确认密码。

    配置过程中它会提示配置一些安全选项,为了服务器的安全,应该选择 y。这些问题包括:

    • Remove anonymous users? (Press y|Y for Yes, any other key for No) : y

      删除匿名用户?(按 y|Y 表示是,任何其他键表示否):y

    • Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y

      禁止远程 root 登录?(按 y|Y 表示是,任何其他键表示否):y

    • Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y

      删除测试数据库并访问它?(按 y|Y 表示是,任何其他键表示否):y

    • Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y

      现在重新加载权限表?(按 y|Y 表示是,任何其他键表示否):y

    2.6、MySQL 服务控制命令

    安装完成后,MySQL 服务就会自动启动。我们可以通过以下几个命令查看 MySQL 服务的状态,启动、停止、重启 MySQL 服务器:

    • CentOS 8 或 CentOS 7
      • 查看 MySQL 服务器状态: systemctl status mysqld
      • 启动 MySQL 服务器: systemctl start mysqld
      • 停止 MySQL 服务器: systemctl stop mysqld
      • 重启 MySQL 服务器: systemctl restart mysqld
      • 配置 MySQL 服务器自启动: systemctl enable mysqld
    • CentOS 6
      • 查看 MySQL 服务器状态: servie mysqld status
      • 启动 MySQL 服务器: servie mysqld start
      • 停止 MySQL 服务器: servie mysqld stop
      • 重启 MySQL 服务器: servie mysqld restart
      • 配置 MySQL 服务器自启动: chkconfig mysqld on
    2.7、连接到 MySQL 服务器

    使用以下命令连接到 MySQL 服务器:

    mysql -u root -p
    
    • 1

    然后根据提示输入 root 帐户的密码,并按 Enter 键。验证通过后,将显示以下输出代表进入了 MySQL 控制台:

    mysql>
    
    • 1

    使用 SHOW DATABASES 显示当前服务器中的所有数据库:

    mysql> show databases;
    
    • 1

    这是输出:

    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | mysql              |
    | performance_schema |
    | sys                |
    +--------------------+
    4 rows in set (0.05 sec)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    上面显示的数据库,是 MySQL 服务器自带数据库。

  • 相关阅读:
    关于C++11静态成员变量的类内初始化
    算法竞赛入门【码蹄集进阶塔335题】(MT2311-2315)
    Spring系统学习 -Spring IOC 的XML管理Bean之P命名空间、实现引入MySQL外部链接属性文件
    转行做程序员,从月薪5k到30k,45岁测试员道出了一路的心酸
    iOS之crash分析篇--符号化
    Python语言:字典的使用
    【从0到1开发一个初级DBMS】(task3)索引结构(更新中)
    强化服务器安全!CentOS 7如何使用fail2ban防范SSH暴力破解攻击?
    【Python计算机视觉】Python全栈体系(二十四)
    Nlog详解---非常详细
  • 原文地址:https://blog.csdn.net/weixin_45187434/article/details/136470124