cherish's blog
大道至简
Linux-MySQL-Install

1.准备

① 检测系统是否自带安装 MySQL

rpm -qa | grep mysql

如有,类似mysql-libs-5.1.52-1.el6_0.1.x86_64那可以选择进行卸载:

rpm -e mysql-libs-5.1.52-1.el6_0.1.x86_64  // 普通删除模式
rpm -e --nodeps mysql-libs-5.1.52-1.el6_0.1.x86_64  // 强力删除模式,如果使用上面命令删除时,提示有依赖的其它文件,则用该命令可以对其进行强力删除

② 检查否存在 mariadb 数据库,如有,卸载之

rpm -qa | grep mariadb

如有,类似

mariadb-libs-5.5.56-2.el7.x86_64

则卸载

rpm -e --nodeps mariadb-libs-5.5.56-2.el7.x86_64

2.安装yum源

# 下载
[root@VM_0_6_centos ~]# wget https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm

# 安装mysql的yum源
[root@VM_0_6_centos ~]# sudo yum install mysql57-community-release-el7-11.noarch.rpm

检查是否安装成功

[root@VM_0_6_centos ~]# yum repolist enabled | grep "mysql.*-community.*"
mysql-connectors-community/x86_64    MySQL Connectors Community              131
mysql-tools-community/x86_64         MySQL Tools Community                   100
mysql57-community/x86_64             MySQL 5.7 Community Server              384

3.安装MySQL

使用yum install命令安装。注意:服务器在国外,下载会很慢,耐心等待即可。

[root@VM_0_6_centos ~]# sudo yum install -y mysql-community-server
# 安装完成
Installed:
  mysql-community-server.x86_64 0:5.7.28-1.el7                                                                                          

Dependency Installed:
  mysql-community-client.x86_64 0:5.7.28-1.el7 mysql-community-common.x86_64 0:5.7.28-1.el7 mysql-community-libs.x86_64 0:5.7.28-1.el7
  numactl-libs.x86_64 0:2.0.12-3.el7_7.1      

Complete!

4.启动MySQL服务

因为只有启动了MySQL服务,才会产生/var/log/mysqld.log文件,初始root密码在这个文件目录下

启动mysql服务(在CentOS7下,启动和关闭服务的命令是systemctl start|stop)

# 现在我们来配置mysql的启动,个人比较推荐使用服务的方式启动(centos7特有)。
# 这样就把mysqld这个服务添加到系统中了,我们可以用service进行启动了
[root@VM_0_6_centos ~]# systemctl start mysqld.service

启动,停止,重启的命令如下

# 使用 service 启动:service mysqld start
[root@VM_0_6_centos ~]# service mysqld stop
# 查看mysql状态
[root@VM_0_6_centos ~]# systemctl status mysqld
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since Thu 2019-12-19 18:29:51 CST; 5s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 2514 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
  Process: 2497 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
 Main PID: 2517 (mysqld)
   CGroup: /system.slice/mysqld.service
           └─2517 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid

Dec 19 18:29:51 VM_0_6_centos systemd[1]: Starting MySQL Server...
Dec 19 18:29:51 VM_0_6_centos systemd[1]: Started MySQL Server.
# 使用 service 关闭:service mysqld stop
[root@VM_0_6_centos ~]# service mysqld stop
# 重启
[root@VM_0_6_centos ~]# service mysqld restart

[root@VM_0_6_centos ~]# sudo systemctl start mysqld

查看mysql服务的启动状态

[root@VM_0_6_centos ~]# systemctl status mysqld
# 如下图显示则表示已经开启
[root@VM_0_6_centos ~]# systemctl status mysqld
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since Thu 2019-12-19 17:29:42 CST; 25s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 26171 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
  Process: 26108 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
 Main PID: 26174 (mysqld)
   CGroup: /system.slice/mysqld.service

5.查看mysql的初始密码

# S&3ytX**5rA8为默认密码,修改之
[root@VM_0_6_centos ~]# sudo cat /var/log/mysqld.log | grep password
2019-12-19T09:29:37.272653Z 1 [Note] A temporary password is generated for root@localhost: S&3ytX**5rA8

6. 登录mysql,修改密码

# 登录mysql
[root@VM_0_6_centos ~]# 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.28

Copyright (c) 2000, 2019, 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> 

# 设置密码。设置的密码必须符合长度,且必须含有数字,小写或大写字母,特殊字符。
mysql> set password=password("kjh*adb%");
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> 
# 刷新权限
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> 

7.开启远程访问

  • 进入数据库:mysql -u root -p
  • 切换到 mysql库:use mysql;
  • 查看用户表select host,user from user;,看到当前host是localhost,只允许本地访问。
# 可以看到只允许localhost访问
mysql> select host,user from user;
+-----------+---------------+
| host      | user          |
+-----------+---------------+
| localhost | mysql.session |
| localhost | mysql.sys     |
| localhost | root          |
+-----------+---------------+
3 rows in set (0.00 sec)

mysql> 
  • 更新user用户表
  mysql> update user set `host` = '%'  where `user` = 'root'  LIMIT 1;
  Query OK, 1 row affected (0.00 sec)
  Rows matched: 1  Changed: 1  Warnings: 0
  
  mysql> ^C
  • 强制刷新权限
  mysql> flush privileges;
  • 再次查看用户表。
# root用户的host变成%,即允许所有的ip远程访问,如果需要指定具体的ip,就填具体的ip即可。
mysql> select host,user from user;
+-----------+---------------+
| host      | user          |
+-----------+---------------+
| %         | root          |
| localhost | mysql.session |
| localhost | mysql.sys     |
+-----------+---------------+
3 rows in set (0.00 sec)

mysql> 
  • 查看端口监听状态netstat -an|grep 3306,修改mysql配置文件my.cnf
# 这里可以看到 0 0.0.0.0:3306 表示所有IP均可访问。
# 如果显示为 127.0.0.1:3306 说明只允许本地访问,需要修改配置文件。
mysql> exit
Bye
[root@VM_0_6_centos ~]# netstat -an|grep 3306
tcp6       0      0 :::3306                 :::*                    LISTEN     
[root@VM_0_6_centos ~]# 
  • 修改my.cnf。

注释掉bind-address 127.0.0.1属性。重启mysql服务service mysql restart,再次使用命令netstat -an|grep 3306查看端口监听状态:

[root@VM_0_6_centos ~]# netstat -an|grep 3306
tcp6       0      0 :::3306                 :::*                    LISTEN     
[root@VM_0_6_centos ~]# 

最后,服务器开放3306端口