CentOS7_YUM安装mysql5.7.19

环境:CentOS7.3
mysql版本:5.7.19

1、首先到官网下载yum安装所需的rpm包
https://dev.mysql.com/downloads/repo/yum/

可以自己在web界面点击下载,也可以通过wget工具下载:
wget https://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm

web页的话在官网的Community 或Yum Repository页面下就有列表。

2、安装mysql源
rpm -Uvh mysql57-community-release-el7-8.noarch.rpm

或yum localinstall mysql57-community-release-el7-8.noarch.rpm

检查mysql源是否安装成功:
[yunwei_wwz@tielemao ~]$ sudo yum repolist enabled | grep “mysql.*-community.*”
mysql-connectors-community/x86_64 MySQL Connectors Community 42
mysql-tools-community/x86_64 MySQL Tools Community 51
mysql57-community/x86_64 MySQL 5.7 Community Server 207


[yunwei_wwz@tielemao ~]$ sudo yum repolist enabled | grep mysql
mysql-connectors-community/x86_64 MySQL Connectors Community 42
mysql-tools-community/x86_64 MySQL Tools Community 51
mysql57-community/x86_64 MySQL 5.7 Community Server 207

3、开始安装mysql:
yum install mysql-community-server 或 yum install mysql 也可以

[yunwei_wwz@tielemao ~]$ sudo yum install mysql-community-server

可以看到安装时显示大概有164M大小。

4、启动MySQL服务:
systemctl start mysqld

查看mysql启动状态:systemctl status mysqld
查看mysql版本:mysql -V

[yunwei_wwz@tielemao ~]$ 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-09-25 10:30:11 CST; 1h 13min ago
Docs: man:mysqld(8)
https://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 24037 ExecStart=/usr/sbin/mysqld –daemonize –pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
Process: 23960 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 24041 (mysqld)
CGroup: /system.slice/mysqld.service
└─24041 /usr/sbin/mysqld –daemonize –pid-file=/var/run/mysqld/mysqld.pid

[yunwei_wwz@tielemao ~]$ mysql -V
mysql Ver 14.14 Distrib 5.7.19, for Linux (x86_64) using EditLine wrapper

设置开机启动:
systemctl enable mysqld

5、修改root默认密码
mysql5.7安装完成之后,有一个挺有意思的地方就是它会自动生成一个默认密码,在/var/log/mysqld.log文件中可以找到明文显示,可以直接vim日志文件查看,不过推荐还是直接用grep命令查看最省事了:

[yunwei_wwz@tielemao ~]$ grep ‘temporary password’ /var/log/mysqld.log
2017-09-25T02:30:07.167639Z 1 [Note] A temporary password is generated for root@localhost: V_/xqg>De4WM

知道密码后就可以sudo mysql -uroot -p 去登录上mysql修改默认密码了:
登录进去后如果不先修改默认密码而是做其它操作的话,都会被提示:你必须先修改默认密码
例:
mysql> status;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

另外,mysql5.7默认安装了密码安全检查插件(validate_password),默认密码检查策略要求密码必须包含:大小写字母、数字和特殊符号,并且长度不能少于8位。否则会提示:你的密码不符合安全检查策略。
例:
mysql> set password for root@loclhost=password(‘Kb27241f’);
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

至于修改密码的命令可以用ALTER USER,也可以直接set password.
例:
ALTER USER root@localhost IDENTIFIED BY ‘MyPass43!’;

set password for root@localhost=password(‘MyPass43!’);

通过mysql环境变量可以查看密码策略相关的信息
show variables like ‘%password%’

validate_password_policy:密码策略,默认为MEDIUM(中等级别?);
validate_password_dictionary_file:密码策略文件,策略为STRONG(健壮)才需要 ;
validate_password_length :密码最少长度,默认8;
validate_password_mixed_case_count:大小写字符长度,至少1个;
validate_password_number_count:数字同样至少1个;
validate_password_special_char_count:特殊字符同样1个。

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

修改密码策略
在/etc/my.cnf文件添加validate_password_policy配置,指定密码策略
# 选择0(LOW),1(MEDIUM),2(STRONG)其中一种,选择2需要提供密码字典文件,LOW则是最低水平的密码策略。
validate_password_policy=1

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

重新启动mysql服务使配置生效:
systemctl restart mysqld

6、添加远程登录用户
默认只允许root帐户在本地登录,如果要在其它客户端上连接mysql,必须修改root允许远程连接,或者添加一个允许远程连接的帐户,为了安全起见,建议是添加一个新的帐户:
mysql> GRANT ALL PRIVILEGES ON *.* TO ‘yunwei’@’%’ IDENTIFIED BY ‘KC520F33a#’ WITH GRANT OPTION;
命令有点长,可以通过记忆相关的英文单词来帮助记忆:
grant:授予、允许;privileges:特权;Identified:验证。

7、配置默认编码为utf8
修改默认配置文件/etc/my.cnf,在[mysqld]下添加编码配置:
[mysqld]
character_set_server=utf8
init_connect=’SET NAMES utf8′

其它创建数据库、表等操作就不在此文叙述了。

文:铁乐与猫
2017-9-25

发表评论

电子邮件地址不会被公开。 必填项已用*标注

1 + 3 =