CentOS 7上安装MySQL

前端之家收集整理的这篇文章主要介绍了CentOS 7上安装MySQL前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

一.安装MysqL

1.配置YUM源

MysqL官网中下载YUM源rpm安装包:http://dev.mysql.com/downloads/repo/yum/

# 下载MysqL源安装包
shell> wget http://dev.MysqL.com/get/MysqL57-community-release-el7-8.noarch.rpm //get之后就是图片上的后缀
# 安装MysqL
shell> yum localinstall MysqL57-community-release-el7-8.noarch.rpm//后面这个是文件

检查MysqL源是否安装成功

yum repolist enabled | grep "MysqL.*-community.*"


看到上图所示表示安装成功

2.安装MysqL

yum install MysqL-community-server

接下来就要等待一段时间了

3.启动MysqL服务

systemctl start MysqLd

4.查看MysqL的启动状态

systemctl status MysqLd

反应如下

MysqLd.service - MysqL Server
   Loaded: loaded (/usr/lib/systemd/system/MysqLd.service; enabled)
   Active: active (running) since Sat 2017-07-01 13:12:06 CST; 14min ago
     Docs: man:MysqLd(8)
           http://dev.MysqL.com/doc/refman/en/using-systemd.html
  Process: 17903 ExecStart=/usr/sbin/MysqLd --daemonize --pid-file=/var/run/MysqLd/MysqLd.pid $MysqLD_OPTS (code=exited,status=0/SUCCESS)
  Process: 17885 ExecStartPre=/usr/bin/MysqLd_pre_systemd (code=exited,status=0/SUCCESS)
 Main PID: 17907 (MysqLd)
   CGroup: /system.slice/MysqLd.service
           `-17907 /usr/sbin/MysqLd --daemonize --pid-file=/var/run/MysqLd/MysqLd.pid

Jul 01 13:12:06 VM_94_49_centos systemd[1]: Starting MysqL Server...
Jul 01 13:12:06 VM_94_49_centos systemd[1]: Started MysqL Server.

5.设置开机自动启动

systemctl enable MysqLd
systemctl daemon-reload

二.对MysqL的一些配置

1.修改MysqL默认密码

安装成功后的MysqL后,默认用户名为root
而默认密码需要去/var/log/MysqLd.log去查看
可以用这个命令去查看

vim /var/log/MysqLd.log
2017-07-01T04:54:06.926059Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2017-07-01T04:54:07.440020Z 0 [Warning] InnoDB: New log files created,LSN=45790
2017-07-01T04:54:07.505909Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2017-07-01T04:54:07.565856Z 0 [Warning] No existing UUID has been found,so we assume that this is the first time that this server has been started. Generating a new UUID: 507d18ba-5e19-11e7-b797-525400b871c7.
2017-07-01T04:54:07.567446Z 0 [Warning] Gtid table is not ready to be used. Table 'MysqL.gtid_executed' cannot be opened.
2017-07-01T04:54:07.568083Z 1 [Note] A temporary password is generated for root@localhost: Lr+/fXrhW8xC//默认密码

也可以这样

grep 'temporary password' /var/log/MysqLd.log

接下来我们就登录MysqL

第一次登录MysqL的时候,它要求你必须要修改密码,才能进行其他的操作,所以我们要乖乖的修改密码

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

修改密码可以使用这个

ALTER USER 'root'@'localhost' IDENTIFIED BY 'Tt471912619!';

也可以使用这个

set password for 'root'@'localhost'=password('Tt471912619!');

通常我们在学习过程中,设置的密码都很简单,方便记忆,所以我们下面就要说说怎么样修改密码策略

2.修改MysqL默认密码策略

(1).查看MysqL密码策略

show variables like '%password%';

(2).特殊值的说明

validate_password_dictionary_file:密码策略文件,策略为STRONG才需要

validate_password_length:密码最少长度 

validate_password_mixed_case_count:大小写字符长度,至少1个

validate_password_number_count :数字至少1个  validate_password_special_char_count:特殊字符至少1validate_password_policy:密码安全策略,默认MEDIUM策略
@H_47_403@ @H_869_404@(3).修改策略
set global 策略名=策略值;


如此这般我就把密码策略修改成如下情况了

于是我们就像上面一样修改密码即可了,这次只需要符合长度大于8的数字就行了

3.配置默认编码为utf8

修改/etc/my.cnf配置文件,在相应位置添加编码配置如下

[client]
default-character-set=utf8
[MysqLd]
character_set_server=utf8
init_connect='SET NAMES utf8'

重新启动MysqL服务使配置生效

systemctl restart MysqLd

注意:你如果在MysqL命令使用
set来设置编码的话,再用这个查询出来也是这样的,不过这是一个假象,等你下次重新登录数据库的时候,在查询编码发现又变回来了,所以还是乖乖去配置文件中去修改

三.MysqL的远程连接

首先要赋予root用户所有权限

grant all privileges on *.* to 'root' @'%' identified by 'T471912619';

刷新权限

flush privileges;

当然配置完这些有可能还是无法远程连接,那么可能就是防火墙的问题了
对于CentOS 7 来说防火墙是由firewalld来管理
你可以使用如下命令直接关闭防火墙之后就可以连接了

systemctl stop firewalld

或者使用开启端口命令

firewall-cmd --zone=public --add-port=端口号/tcp --permanent
重新载入
firewall-cmd --reload
查看
firewall-cmd --zone= public --query-port=端口号/tcp
删除
firewall-cmd --zone= public --remove-port=端口号/tcp --permanent

如果你使用如下命令会报错误

service iptables save 
Failed to restart iptables.service: Unit iptables.service Failed to load: No such file or directory.

查看iptables的状态

systemctl status iptables
iptables.service
   Loaded: not-found (Reason: No such file or directory)
   Active: inactive (dead)//休眠,并未启动
原文链接:https://www.f2er.com/centos/376964.html

猜你在找的CentOS相关文章

策略 检查规则
0 or LOW Length
1 or MEDIUM Length; numeric,lowercase/uppercase,and special characters
2 or STRONG Length; numeric,and special characters; dictionary file