Centos7通过yum安装最新MySQL
一:去官网查看最新MySQL安装包
https://dev.mysql.com/downloads/repo/yum/
二:下载MySQL源安装包
wget http://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
三:安装MySql源
yum -y install mysql57-community-release-el7-11.noarch.rpm
查看一下安装效果
yum repolist enabled | grep mysql.*
看到上图所示表示安装成功。
可以修改vim /etc/yum.repos.d/mysql-community.repo源,改变默认安装的mysql版本。比如要安装5.6版本,将5.7源的enabled=1改成enabled=0。然后再将5.6源的enabled=0改成enabled=1即可。改完之后的效果如下所示:
四:启动MySQL服务
shell> systemctl start mysqld
查看MySQL的启动状态
shell> systemctl status mysqld
● mysqld.service - MySQL Server Loaded: loaded (/usr/lib/systemd/system/mysqld.service; disabled; vendor preset: disabled) Active: active (running) since 五 2016-06-24 04:37:37 CST; 35min ago Main PID: 2888 (mysqld) CGroup: /system.slice/mysqld.service └─2888 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
6月 24 04:37:36 localhost.localdomain systemd[1]: Starting MySQL Server… 6月 24 04:37:37 localhost.localdomain systemd[1]: Started MySQL Server.
五:配置MySQL服务开机启动
shell> systemctl enable mysqld
shell> systemctl daemon-reload
六:配置MySQL服密码策略
共有以下几种密码策略:
策略 检查规则 0 or LOW Length 1 or MEDIUM Length; numeric, lowercase/uppercase, and special characters 2 or STRONG Length; numeric, lowercase/uppercase, and special characters; dictionary file MySQL官网密码策略详细说明:http://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需要提供密码字典文件
validate_password_policy=0
如果不需要密码策略,添加my.cnf文件中添加如下配置禁用即可:
validate_password = off
重新启动mysql服务使配置生效:
systemctl restart mysqld
七、修改MySQL root本地登录密码
mysql安装完成之后,在/var/log/mysqld.log文件中给root生成了一个默认密码。通过下面的方式找到root默认密码,然后登录mysql进行修改:
shell> grep 'temporary password' /var/log/mysqld.log
shell> 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错误,. 可以参考第六步:“配置MySQL服密码策略”
通过msyql环境变量可以查看密码策略的相关信息:
mysql> show variables like '%password%';
8、添加mysql远程登录用户
配置mysql可以远程连接:
mysql -u root -p
mysql> use mysql;
mysql> update user set host = '%' where user = 'root';
mysql> select host, user from user;
配置防火墙:
添加:
firewall-cmd --zone=public --add-port=3306/tcp --permanent
重新载入:
firewall-cmd --reload
查看
查看所有打开的端口: firewall-cmd --zone=public --list-ports
删除
firewall-cmd --zone= public --remove-port=3306/tcp --permanent
9、配置默认编码为utf8
修改/etc/my.cnf配置文件,在[mysqld]下添加编码配置,如下所示:
[mysqld] character_set_server=utf8 init_connect=‘SET NAMES utf8’
最后注意重启mysqld systemctl restart mysqld
重新启动mysql服务,查看数据库默认编码如下所示:
**默认配置文件路径 **: 配置文件:/etc/my.cnf 日志文件:/var/log//var/log/mysqld.log 服务启动脚本:/usr/lib/systemd/system/mysqld.service socket文件:/var/run/mysqld/mysqld.pid