CentOS7下安装Mysql5.7详细步骤

  • 原创
  • 作者:程序员三丰
  • 发布时间:2021-05-22 01:24
  • 浏览量:807
MySQL 是最流行的关系型数据库管理系统,在 WEB 应用方面 MySQL 是最好的 RDBMS(Relational Database Management System:关系数据库管理系统)应用软件之一。在本教程中,会让大家快速掌握在CentOS下安装 MySQL ,并轻松使用 MySQL 数据库。

下载

一、官方下载地址

https://downloads.mysql.com/archives/community/

MySQL5.7官方下载

将下载的安装上传到CentOS7虚拟机系统中某一目录下。

当然也可以登录进入CentOS7虚拟机系统内通过wget下载(不过不建议采用种方式,主要是安装包有些大)

[root@localhost ~]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.21-1.el7.x86_64.rpm-bundle.tar

二、安装

  1. 卸载系统自带的 mariadb-lib
    [root@localhost ~]# rpm -qa | grep mariadb
    mariadb-libs-5.5.56-2.el7.x86_64
    [root@localhost ~]# 
    [root@localhost ~]# rpm -e mariadb-libs-5.5.56-2.el7.x86_64 --nodeps
    [root@localhost ~]# 
    [root@localhost ~]# rpm -qa | grep mariadb
    [root@localhost ~]#
    
  2. 安装我们下载好的Mysql5.7

    进入我们下载Mysql5.7安装包所在目录

    [root@localhost ~]# 
    [root@localhost ~]# ls
    anaconda-ks.cfg  mysql-5.7.21-1.el7.x86_64.rpm-bundle.tar
    

    然后解压

    [root@localhost ~]# ls
    anaconda-ks.cfg  mysql-5.7.21-1.el7.x86_64.rpm-bundle.tar
    [root@localhost ~]# tar -xvf mysql-5.7.21-1.el7.x86_64.rpm-bundle.tar
    

    依次执行下面安装命令

    rpm -ivh mysql-community-common-5.7.21-1.el7.x86_64.rpm
    rpm -ivh mysql-community-libs-5.7.21-1.el7.x86_64.rpm
    rpm -ivh mysql-community-client-5.7.21-1.el7.x86_64.rpm
    rpm -ivh mysql-community-server-5.7.21-1.el7.x86_64.rpm
    rpm -ivh mysql-community-libs-compat-5.7.21-1.el7.x86_64.rpm
    

    以安全模式初始化mysql

    mysqld --initialize --user=mysql
    

    查看 初始化密码

    [root@localhost ~]# grep "password" /var/log/mysqld.log 
    2020-05-26T04:08:07.475037Z 1 [Note] A temporary password is generated for root@localhost: y&F6MP<hhlSf
    

    启动Mysql并检查其服务状态

    [root@localhost ~]# systemctl start mysqld
    [root@localhost ~]# systemctl status mysqld
    ● mysqld.service - MySQL Server
       Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
       Active: active (running) since 二 2020-05-26 12:10:02 CST; 4s ago
         Docs: man:mysqld(8)
               http://dev.mysql.com/doc/refman/en/using-systemd.html
      Process: 2434 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
      Process: 2417 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
     Main PID: 2437 (mysqld)
       CGroup: /system.slice/mysqld.service
               └─2437 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
    
    5月 26 12:10:02 localhost.localdomain systemd[1]: Starting MySQL Server...
    5月 26 12:10:02 localhost.localdomain systemd[1]: Started MySQL Server.
    

    设置 mysql 服务自启动

    [root@localhost ~]# systemctl enable mysqld
    // 设置完成后,通过下面代码查看是否设置成功
    [root@localhost ~]# systemctl list-unit-files --type=service | grep mysqld
    mysqld.service                                enabled 
    mysqld@.service                               disabled
    
  3. 修改初始化密码

    查看初始化密码

    [root@localhost ~]# grep "password" /var/log/mysqld.log
    2020-05-26T04:08:07.475037Z 1 [Note] A temporary password is generated for root@localhost: y&F6MP<hhlSf
    

    使用初始化密码登入mysql

    [root@localhost ~]# mysql -uroot -p
    Enter password: 
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 3
    Server version: 5.7.21
    
    Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
    
    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> show databases;
    ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
    mysql>
    

    修改初始化密码

    mysql> alter user 'root'@'localhost' identified by 'root';
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> show databases;
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | mysql              |
    | performance_schema |
    | sys                |
    +--------------------+
    4 rows in set (0.00 sec)
    

    Mysql安装完成

三、开始使用

使用修改后的密码,正式登录使用Mysql

[root@localhost ~]# mysql -uroot -proot
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.21 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

mysql>

修改 root 登录限制为允许开放所有登录方式(远程登录)

mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select user,host from user;
+---------------+-----------+
| user          | host      |
+---------------+-----------+
| mysql.session | localhost |
| mysql.sys     | localhost |
| root          | localhost |
+---------------+-----------+
3 rows in set (0.00 sec)

mysql> update user set host = '%' where user = 'root';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql>

远程客户端登录测试
远程客户端登录成功测试

声明:本文为原创文章,51blog.xyz和作者拥有版权,如需转载,请注明来源于51blog.xyz并保留原文链接:https://mp.51blog.xyz/article/7.html

文章归档

推荐文章

buildadmin logo
Thinkphp8 Vue3 Element PLus TypeScript Vite Pinia

🔥BuildAdmin是一个永久免费开源,无需授权即可商业使用,且使用了流行技术栈快速创建商业级后台管理系统。

热门标签

PHP ThinkPHP ThinkPHP5.1 Go Mysql Mysql5.7 Redis Linux CentOS7 Git HTML CSS CSS3 Javascript JQuery Vue LayUI VMware Uniapp 微信小程序 docker wiki Confluence7 学习笔记 uView ES6 Ant Design Pro of Vue React ThinkPHP6.0 chrome 扩展 翻译工具 Nuxt SSR 服务端渲染 scrollreveal.js ThinkPHP8.0 Mac webman 跨域CORS vscode GitHub ECharts Canvas