CentOS7上YUM安装MySQL5.7
1、配置YUM源
在MySQL官网中下载YUM源rpm安装包:http://dev.mysql.com/downloads/repo/yum/
找到以下源,并下载安装1
2
3Red Hat Enterprise Linux 7 / Oracle Linux 7 (Architecture Independent), RPM Package 25.1K
Download
(mysql57-community-release-el7-11.noarch.rpm)
下载MySQL源安装包
wget https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
安装MySQL源
yum localinstall mysql57-community-release-el7-11.noarch.rpm
2、安装MySQL
yum install mysql-community-server
1 | ======================================================================================== |
3、启动MySQL服务
systemctl start mysqld
查看MySQL的启动状态
systemctl status mysqld
1 | ● mysqld.service - MySQL Server |
4、开机启动
systemctl enable mysqld
systemctl daemon-reload
5、修改root本地登录密码
mysql安装完成之后,在/var/log/mysqld.log
文件中给root
生成了一个默认密码。通过下面的方式找到root默认密码
,然后登录mysql
进行修改:
找到密码:
grep 'temporary password' /var/log/mysqld.log
1 | grep 'temporary password' /var/log/mysqld.log |
登录Mysql:
mysql -uroot -p
输入上面找到的随机密码后,修改密码:
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass4!';
或者
mysql> set password for 'root'@'localhost'=password('MyNewPass4!');
注意:
mysql5.7
默认安装了密码安全检查插件(validate_password),默认密码检查策略要求密码必须包含:大小写字母、数字和特殊符号,并且长度不能少于8位。否则会提示ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
错误
通过msyql环境变量可以查看密码策略的相关信息:
mysql> show variables like '%password%';
1 | +---------------------------------------+--------+ |
1 | validate_password_policy:密码策略,默认为MEDIUM策略 |
上述参数是默认策略MEDIUM的密码检查规则。
共有以下几种密码策略:
1 | 策略 检查规则 |
MySQL官网密码策略详细说明:http://dev.mysql.com/doc/refman/5.7/en/validate-password-options-variables.html#sysvar_validate_password_policy
修改密码策略
1 | 在/etc/my.cnf文件添加validate_password_policy配置,指定密码策略 |
如果不需要密码策略,添加my.cnf
文件中添加如下配置禁用即可:
validate_password = off
重新启动mysql服务使配置生效:
systemctl restart mysqld
6、添加远程登录用户
默认只允许root帐户在本地登录,如果要在其它机器上连接mysql,必须修改root允许远程连接:
授权方式:
例如,你想root使用123456从任何主机连接到mysql服务器。
1 | mysql>GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION; |
如果你想允许用户jack从ip为10.10.50.127的主机连接到mysql服务器,并使用654321作为密码:
1 | mysql>GRANT ALL PRIVILEGES ON *.* TO 'jack'@’10.10.50.127’ IDENTIFIED BY '654321' WITH GRANT OPTION; |
或者添加一个允许远程连接的帐户,为了安全起见,我添加一个新的帐户
1 | mysql> GRANT ALL PRIVILEGES ON *.* TO 'yangxin'@'%' IDENTIFIED BY 'Yangxin0917!' WITH GRANT OPTION; |
7、配置默认编码为utf8
修改/etc/my.cnf配置文件,在[mysqld]下添加编码配置,如下所示:
1 | [mysqld] |
重新启动mysql服务,查看数据库默认编码如下所示:1
2
3
4
5
6
7
8
9
10
11
12
13
14mysql> show variables like '%character%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)
默认配置文件路径
配置文件:/etc/my.cnf
日志文件:/var/log//var/log/mysqld.log
服务启动脚本:/usr/lib/systemd/system/mysqld.service
socket文件:/var/run/mysqld/mysqld.pid