zoukankan      html  css  js  c++  java
  • CentOS7安装MySQL以及密码修改

    在CentOS中默认安装有MariaDB,这个是MySQL的分支,但为了需要,还是要在系统中安装MySQL,而且安装完成之后可以直接覆盖掉MariaDB。

    1 下载并安装MySQL官方的 Yum Repository

    wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
    

    使用上面的命令就直接下载了安装用的Yum Repository,大概25KB的样子,然后就可以直接yum安装了。

        yum -y install mysql57-community-release-el7-10.noarch.rpm
    

    之后就开始安装MySQL服务器。

        yum -y install mysql-community-server
    

    这步可能会花些时间,安装完成后就会覆盖掉之前的mariadb。

     
    image

    至此MySQL就安装完成了,然后是对MySQL的一些设置。

    2 MySQL数据库设置

    首先启动MySQL

    [root@localhost ~]# systemctl start  mysqld.service
    

    查看MySQL运行状态,运行状态如图:

        systemctl status mysqld.service
    
     
    image

    此时MySQL已经开始正常运行,不过要想进入MySQL还得先找出此时root用户的密码,通过如下命令可以在日志文件中找出密码:

    grep "password" /var/log/mysqld.log
    
     
    image

    如下命令进入数据库:

     mysql -u root -p
    

    输入初始密码,此时不能做任何事情,因为MySQL默认必须修改密码之后才能操作数据库:

    mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'new password';
    

    这里有个问题,新密码设置的时候如果设置的过于简单会报错:

     
    image

    原因是因为MySQL有密码设置的规范,具体是与validate_password_policy的值有关:

     
    image

    MySQL完整的初始密码规则可以通过如下命令查看:

    SHOW VARIABLES LIKE 'validate_password%';
    
     
    image

    密码的长度是由validate_password_length决定的,而validate_password_length的计算公式是:

    validate_password_length = validate_password_number_count +validate_password_special_char_count + (2* validate_password_mixed_case_count)
    

    我的是已经修改过的,初始情况下第一个的值是ON,validate_password_length是8。可以通过如下命令修改:

    mysql> set global validate_password_policy=0;
    
    mysql> set global validate_password_length=1;
    

    设置之后就是我上面查出来的那几个值了,此时密码就可以设置的很简单,例如1234之类的。到此数据库的密码设置就完成了。

    但此时还有一个问题,就是因为安装了Yum Repository,以后每次yum操作都会自动更新,需要把这个卸载掉:

    [root@localhost ~]#yum -y remove mysql57-community-release-el7-10.noarch
    

    此时才算真的完成了。

    mysql允许外部链接

    mysql -u root -p
    
    mysql>use mysql;
    
    mysql>select 'host' from user where user='root';
    
    mysql>update user set host = '%' where user ='root';
    
    mysql>flush privileges;
    
    mysql>select 'host'  from user where user='root';
    

    第一句是以权限用户root登录

    第二句:选择mysql库

    第三句:查看mysql库中的user表的host值(即可进行连接访问的主机/IP名称)

    第四句:修改host值(以通配符%的内容增加主机/IP地址),当然也可以直接增加IP地址

    第五句:刷新MySQL的系统权限相关表

    第六句:再重新查看user表时,有修改。。

    重起mysql服务即可完成。

    打开mysql配置文件vi /etc/mysql/mysql.conf.d/mysqld.cnf

    将bind-address = 127.0.0.1注销​
    
    bind-address = 0.0.0.0 # 表示允许任何主机登陆MySQL
    
    port=3306 # 表示MySQL运行端口为3306
    

    2、支持root用户允许远程连接mysql数据库

    grant all privileges on *.* to 'root'@'%' identified by '123456' with grant option;
    
    flush privileges;
    
    /etc/init.d/mysql start
    

    5.6 5.7 密码及权限设置

    mysql5.6
    grant all privileges on *.* to root@'%' identified by "mysql_pwd" with grant option;
    grant all privileges on *.* to root@'localhost' identified by "mysql_pwd" with grant option;
    grant all privileges on *.* to root@'127.0.0.1' identified by "mysql_pwd" with grant option;
    drop database if exists test;
    use mysql;
    delete from user where not (user='root');
    delete from db where user='';
    UPDATE user SET password=PASSWORD('mysql_pwd') WHERE user='root' AND host='127.0.0.1' OR host='%' OR host='localhost';
    delete from user where password='';
    flush privileges;
    select user,password,host from mysql.user;
    exit;
    
    
    mysql5.7:
    grant all privileges on *.* to root@'%' identified by "mysql_pwd" with grant option;
    grant all privileges on *.* to root@'localhost' identified by "mysql_pwd" with grant option;
    grant all privileges on *.* to root@'127.0.0.1' identified by "mysql_pwd" with grant option;
    drop database if exists test;
    use mysql;
    delete from user where not (user='root');
    delete from db where user='';
    delete from user where authentication_string='';
    update mysql.user set authentication_string=password('mysql_pwd') where user='root' AND host='127.0.0.1' OR host='%' OR host='localhost';
    flush privileges;
    select user,authentication_string,host from mysql.user;
    exit;


    作者:小梨的十三
    链接:https://www.jianshu.com/p/ad76ac7db32b
    来源:简书
    著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
  • 相关阅读:
    Java 开发 gRPC 服务和客户端
    Springcloud中的region和zone的使用
    Spring Boot中使用AOP记录请求日志
    JDK1.8中的Lambda表达式和Stream
    机器学习之SVM
    机器学习之决策树
    【iOS开发】emoji表情的输入思路
    block为什么要用copy,runtime的简单使用
    swift和oc混编
    iOS之AFN错误代码1016(Error Domain=com.alamofire.error.serialization.response Code=-1016 "Request failed: unacceptable)
  • 原文地址:https://www.cnblogs.com/shaozhu520/p/12830607.html
Copyright © 2011-2022 走看看