CentOS7安装MySQL5.7[YUM方式]

本文主要演示在 CentOS 下用 yum 工具安装mysql5.7。

日常的项目环境搭建免不了要使用到数据库,用 yum 搭建无疑是最便捷的。因此我对安装过程做了整理,方便日后翻阅。

配置yum源(首先确保网络通畅)

MySQL官网中下载YUM源rpm安装包:点这里获取

或使用mysql源的安装包进行安装。

准备安装的Linux系统版本信息:

[[email protected] ~]# cat /etc/os-release 
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:7"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"

CENTOS_MANTISBT_PROJECT="CentOS-7"
CENTOS_MANTISBT_PROJECT_VERSION="7"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="7"
[[email protected] ~]# 

下载mysql源

[[email protected] tools]# wget http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm

安装

[[email protected] tools]# yum localinstall -y mysql57-community-release-el7-8.noarch.rpm

检查mysql源

[[email protected] tools]# yum repolist enabled | grep "mysql.*-community.*"
mysql-connectors-community/x86_64 MySQL Connectors Community                  42
mysql-tools-community/x86_64      MySQL Tools Community                       55
mysql57-community/x86_64          MySQL 5.7 Community Server                 227
[[email protected] tools]# 

安装mysql

[[email protected] tools]# yum -y install mysql-community-server

启动mysql

[[email protected] tools]# systemctl start mysqld

查看mysql的运行状态

[[email protected] tools]# systemctl status mysqld
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since Mon 2017-12-04 00:04:24 EST; 19s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 1629 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
  Process: 1555 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
 Main PID: 1632 (mysqld)
   CGroup: /system.slice/mysqld.service
           └─1632 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid

Dec 04 00:04:18 amesy systemd[1]: Starting MySQL Server...
Dec 04 00:04:24 amesy systemd[1]: Started MySQL Server.
[[email protected] tools]# 

设置mysql服务开机自启

[[email protected] tools]# systemctl enable mysqld
# 重新载入:
[[email protected] tools]# systemctl daemon-reload  

修改mysql默认密码

首先,mysql安装完成之后,自动在/var/log/mysqld.log文件中给root生成了一个默认密码。通过下面的方式找到root默认密码,然后登录mysql进行修改:

[[email protected] tools]# grep 'temporary password' /var/log/mysqld.log
2017-12-04T05:04:19.539001Z 1 [Note] A temporary password is generated for [email protected]: WfPLuukdq1=+
[[email protected] tools]# 

可以看到mysql的root用户的默认密码是: WfPLuukdq1=+

修改密码:

首先登录mysql内,然后使用sql命令修改:

[[email protected] tools]# mysql -uroot -p  # 回车,然后输入默认密码登录。
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass4!';  #"MyNewPass4!"为将要设置的新密码。
Query OK, 0 rows affected (0.00 sec)
mysql> 

或使用另一条sql命令修改:

mysql> set password for 'root'@'localhost'=password('MyNewPass4!');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> 

注意:mysql5.7默认安装了密码安全检查插件(validate_password),默认密码检查策略要求密码必须包含:大小写字母、数字和特殊符号,并且长度不能少于8位。否则会提示ERROR 1819 (HY000): **Your password does not satisfy the current policy requirements错误. **

通过msyql环境变量可以查看密码策略的相关信息:

mysql> show variables like '%password%';
+---------------------------------------+--------+
| Variable_name                         | Value  |
+---------------------------------------+--------+
| default_password_lifetime             | 0      |
| disconnect_on_expired_password        | ON     |
| log_builtin_as_identified_by_password | OFF    |
| mysql_native_password_proxy_users     | OFF    |
| old_passwords                         | 0      |
| report_password                       |        |
| sha256_password_proxy_users           | OFF    |
| validate_password_check_user_name     | OFF    |
| validate_password_dictionary_file     |        |
| validate_password_length              | 8      |
| validate_password_mixed_case_count    | 1      |
| validate_password_number_count        | 1      |
| validate_password_policy              | MEDIUM |
| validate_password_special_char_count  | 1      |
+---------------------------------------+--------+
14 rows in set (0.01 sec)
mysql> 

部分默认策略MEDIUM的密码检查规则翻译如下:

validate_password_policy:密码策略,默认为MEDIUM策略。 lidate_password_dictionary_file:密码策略文件,策略为STRONG才需要。 validate_password_length:密码最少长度。 alidate_password_mixed_case_count:大小写字符长度,至少1个。 alidate_password_number_count :数字至少1个。
lidate_password_special_char_count:特殊字符至少1个

MySQL官网密码策略详细说明:http://dev.mysql.com/doc/refman/5.7/en/validate-password-options-variables.html#sysvar_validate_password_policy

修改密码策略:即不使用mysql规定的密码复杂度策略,自己随意设置密码 在/etc/my.cnf文件添加validate_password_policy配置,指定密码策略

# 选择0(LOW),1(MEDIUM),2(STRONG)其中一种,如果选择2需要提供密码字典文件。
validate_password_policy=0

如果不需要密码策略,在my.cnf文件中添加如下配置禁用即可:

validate_password = off

修改完之后重启mysql服务使配置生效:

[[email protected] ~]# systemctl restart mysqld

添加远程登录用户

mysql默认只允许root帐户在本地登录,如果要在其它机器上连接mysql,必须修改root为允许远程连接,或者添加一个允许远程连接的帐户,为了安全起见,此处添加一个新的帐户:

mysql> GRANT ALL PRIVILEGES ON *.* TO 'mysqlcli'@'172.16.250.161' IDENTIFIED BY '12345678' WITH GRANT OPTION;

可以看到: 添加的用户名为”mysqlcli”,目标主机ip为”172.16.250.161”,登录密码为”12345678”。 通过sql命令查看添加的信息:

mysql> select Host,User from mysql.user;
+----------------+---------------+
| Host           | User          |
+----------------+---------------+
| 172.16.250.161 | mysqlcli      |
| localhost      | mysql.session |
| localhost      | mysql.sys     |
| localhost      | root          |
+----------------+---------------+
4 rows in set (0.00 sec)
mysql> 

然后,我又打开了一台新的虚拟机,ip为”172.16.250.161”,用于远程连接mysql服务器。 查看ip:

[[email protected] ~]# ifconfig ens37
ens37: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.16.250.161  netmask 255.255.0.0  broadcast 172.16.255.255
        inet6 fe80::7cc0:c9b9:a0e7:cd26  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:54:1d:e0  txqueuelen 1000  (Ethernet)
        RX packets 30091  bytes 39335601 (37.5 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 17038  bytes 1326911 (1.2 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[[email protected] ~]# 

配置mysql源,安装mysql client端:

[[email protected] ~]# wget http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm 
[[email protected] ~]# yum -y install mysql-community-client

关闭iptables,selinux等服务,然后进行连接:

[[email protected] ~]# iptables -F
[[email protected] ~]# setenforce 0
[[email protected] ~]# mysql -umysqlcli -h172.16.252.190 -p  # "172.16.252.190"为服务端机器ip。
Enter password:   # 此处的登录密码为服务端设置的"12345678"。
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.20 MySQL Community Server (GPL)
...
mysql> 

登录成功!

配置默认编码为utf8

修改/etc/my.cnf配置文件,在[mysqld]最底下下添加如下编码配置,如下所示:

[client]
default-character-set=utf8

注: mysql配置文件my.cnf中,每个[]表示一个区域,所以[client]要添加在[mysqld]及其他区域外,否则会报错。

重启服务:

[[email protected] tools]# systemctl restart mysqld

登录mysql查看已生效的编码:

mysql> show variables like 'character_set_database';
+------------------------+-------+
| Variable_name          | Value |
+------------------------+-------+
| character_set_database | utf8  |
+------------------------+-------+
1 row in set (0.01 sec)
mysql> 

mysql配置文件路径

配置文件:/etc/my.cnf
日志文件:var/log/mysqld.log
服务启动脚本:/usr/lib/systemd/system/mysqld.service
pid文件:/var/run/mysqld/mysqld.pid

—END

PREVIOUSCentOS7安装MySQL5.7[ 二进制方式]
NEXTGo语言编程环境搭建