[笔记]--CentOS 7 yum安装配置MySQL5.7教程

  1. 配置YUM源

    1.1下载mysql源安装包

    shell> wget http://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm

    1.2安装mysql源

    shell> yum localinstall mysql57-community-release-el7-11.noarch.rpm

    1.3检查mysql源是否安装成功(出现图示表示安装成功)

    shell> yum repolist enabled | grep "mysql.*-community.*"

     
     
     
  2.  

    改变默认安装的mysql版本(适用想安装5.5或5.6版本的用户,安装5.7可跳过)

    修改vim /etc/yum.repos.d/mysql-community.repo源

    比如要安装5.6版本,将5.7源的enabled=1改成enabled=0,并将5.6源的enabled=0改成enabled=1即可,5.5版本同理修改,改完之后的效果如下所示:

     
     
  3.  

    安装MySQL

    shell> yum install mysql-community-server

    安装过程提示Is this ok? [y/d/n]输入y继续即可,安装完成如下图

    查看已安装的mysql

    shell> yum list installed | grep mysql

     
     
  4.  

    启动MySQL服务

    shell> systemctl start mysqld

    查看MySQL的启动状态以验证

    shell> systemctl status mysqld

     
  5.  

    设置开机启动

    shell> systemctl enable mysqld

    shell> systemctl daemon-reload

  6.  

    修改root本地登录密码步骤原理详解(如没有时间可直接跳过本步骤,参考第7步)

    6.1查看安装过程生成的默认密码并登录mysql

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

    shell> grep 'temporary password' /var/log/mysqld.log

    shell> mysql –u root –p按ENTER键输入生成的默认密码进行登录

    6.2修改root本地登录密码

    mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'mysql123';

    或者

    mysql> set password for 'root'@'localhost'= password('mysql123');

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

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

    mysql> show variables like '%password%';

    修改密码策略

    在/etc/my.cnf文件添加validate_password_policy配置,指定密码策略

    shell> vim /etc/my.cnf

    # 设置密码验证策略为LOW

    validate_password_policy=0

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

    shell> systemctl restart mysqld

     
     
     
     
     
  7.  

    修改root本地登录密码总结

    如果在CentOS7下安装mysql只是为个人学习测试使用,需要使用简单密码的话便直接可以修改my.cnf配置文件设置密码策略为LOW,再查看默认密码登录mysql,修改登录密码,步骤如下:

    shell> vim /etc/my.cnf

    # 设置密码策略为LOW

    validate_password_policy=0

    shell> systemctl restart mysqld

    shell> grep 'temporary password' /var/log/mysqld.log

    shell> mysql –u root –p按ENTER键输入生成的默认密码

    mysql> set password for 'root'@'localhost'= password('简单密码');

    当然也可以直接设置一个符合MEDIUM密码策略的密码,就不用繁琐的修改my.cnf配置文件了

  8.  

    开启mysql的远程登录

    mysql> grant all privileges on *.* to 'root' @'%' identified by 'mysql123';

    mysql> flush privileges;

    安全起见也可以设置一个非root远程登录用户

    mysql> GRANT ALL PRIVILEGES ON *.* TO '用户名'@'%' IDENTIFIED BY '登录密码' WITH GRANT OPTION;

     
  9.  

    设置默认编码为utf8

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

    shell> vim /etc/my.cnf

    [mysqld]

    character_set_server=utf8

    init_connect='SET NAMES utf8'

    shell> systemctl restart mysqld         #重启mysql

     
     
     
  10.  

    开放Linux的对外访问的端口3306

    开启mysql的远程登录并不能完成远程登录操作,还需要开放Linux3306端口

    在CentOS6.5中安装MySQL给出的命令一般是

    /sbin/iptables -I INPUT -p tcp --dport 3306 -j ACCEPT

    /etc/rc.d/init.d/iptables save   #将修改永久保存到防火墙中

    但在CentOS7中第2步无法完成,这是因为CentOS7默认使用firewall作为防火墙,而不是iptables,要将默认防火墙修改为iptables可按如下操作:

    10.1关闭firewall

    shell> systemctl stop firewalld.service #停止firewall

    shell> systemctl disable firewalld.service #禁止firewall开机启动

    shell> firewall-cmd --state #查看firewall状态(关闭后显示notrunning)

    10.2安装并配置iptables防火墙

    shell> yum install iptables #安装iptables

    shell> vim /etc/sysconfig/iptables #编辑防火墙配置文件

    # sampleconfiguration for iptables service

    # you can edit thismanually or use system-config-firewall

    # please do not askus to add additional ports/services to this default configuration

    *filter

    :INPUT ACCEPT [0:0]

    :FORWARD ACCEPT[0:0]

    :OUTPUT ACCEPT[0:0]

    -A INPUT -m state--state RELATED,ESTABLISHED -j ACCEPT

    -A INPUT -p icmp -jACCEPT

    -A INPUT -i lo -jACCEPT

    -A INPUT -p tcp -mstate --state NEW -m tcp --dport 22 -j ACCEPT

    -A INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -jACCEPT

    -A INPUT -j REJECT--reject-with icmp-host-prohibited

    -A FORWARD -jREJECT --reject-with icmp-host-prohibited

    COMMIT

    :wq! #保存退出

    shell> service iptables save #保存防火墙规则到/etc/sysconfig/iptables

    shell> systemctl restart iptables.service #最后重启防火墙使配置生效

    shell> systemctl enable iptables.service #设置iptables开机启动

     
     
     
     
     
原文地址:https://www.cnblogs.com/lizhishugen/p/9316415.html