CentOS 7.4 安装 MySQL 5.7 教程

爷,独闯天下 提交于 2020-12-16 11:09:41

确定系统版本

命令:cat /etc/centos-release 输入:CentOS Linux release 7.4.1708 (Core)

安装方式

  1. yum 安装
  2. 编译安装(以后有时间介绍,不同的系统不同的版本,编译安装会有各种不同的问题)

yum 安装方式介绍

CentOS 7 默认安装了mariadb数据库,但是我们要使用MySQL

  • 进入本机的源文件目录
    • cd /usr/local/src/
  • 下载MySQL的repo源
    • wget http://repo.mysql.com/mysql57-community-release-el7-8.noarch.rpm
  • 安装MySQL源
    • rpm -ivh mysql57-community-release-el7-8.noarch.rpm
  • 使用yum安装mysql-server(这个过程可能要30分钟左右)
    • yum -y install mysql-server
  • 安装完成
总下载量:208 M
Downloading packages:
Delta RPMs disabled because /usr/bin/applydeltarpm not installed.
(1/7): libaio-0.3.109-13.el7.x86_64.rpm                      |  24 kB  00:00:00
(2/7): mysql-community-common-5.7.29-1.el7.x86_64.rpm        | 311 kB  00:00:04
(3/7): mysql-community-libs-5.7.29-1.el7.x86_64.rpm          | 2.5 MB  00:01:39     
(4/7): mysql-community-libs-compat-5.7.29-1.el7.x86_64.rpm   | 1.3 MB  00:00:44     
(5/7): postfix-2.10.1-7.el7.x86_64.rpm                       | 2.4 MB  00:00:00     
(6/7): mysql-community-client-5.7.29-1.el7.x86_64.rpm        |  26 MB  00:09:19     
(7/7): mysql-community-server-5.7.29-1.el7.x86_64.rpm        | 175 MB  00:28:37
已安装:
  mysql-community-libs.x86_64 0:5.7.29-1.el7
  mysql-community-libs-compat.x86_64 0:5.7.29-1.el7
  mysql-community-server.x86_64 0:5.7.29-1.el7
  
作为依赖被安装:
  libaio.x86_64 0:0.3.109-13.el7
  mysql-community-client.x86_64 0:5.7.29-1.el7
  mysql-community-common.x86_64 0:5.7.29-1.el7
  
作为依赖被升级:
  postfix.x86_64 2:2.10.1-7.el7
  
替代:
  mariadb-libs.x86_64 1:5.5.56-2.el7
  
完毕!

数据库配置

  • 启动MySQL:systemctl start mysqld.service
  • 查看MySQL状态:systemctl status mysqld.service
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since 日 2020-03-29 01:28:50 CST; 16s ago
  • 查找数据库root用户的 临时密码:grep "password" /var/log/mysqld.log
2020-03-28T17:28:47.341327Z 1 [Note] A temporary password is generated for root@localhost: :8#I;as5F/yw
  • 使用临时密码进入数据库:mysql -uroot -p
  • 如果不修改密码,会有如下报错
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 '123';
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
  • 调整密码后:
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'Limm123!!';
Query OK, 0 rows affected (0.00 sec)
  • 配置访问权限
mysql> update mysql.user set host='%' where host='localhost';
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3  Changed: 3  Warnings: 0
  • 刷新权限(重要 !!!)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
  • Done !!!

MySQL 操作命令

  • 启动: systemctl start mysqld.service
  • 重启: systemctl restart mysqld.service
  • 停止: systemctl stop mysqld.service
  • 状态: systemctl status mysqld.service
  • 设置MySQL开机启动: systemctl enable mysqld.service
  • 停止MySQL开机启动: systemctl disable mysqld.service

常见问题

  • MySQL开启外网访问权限后还是无法访问 如果是阿里云服务器,找到对应实例的安全组 > 配置规则 > 添加安全组规则:如下图
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!