【Mysql】Linux RPM 方式安装 MySQL

 ̄綄美尐妖づ 提交于 2020-01-21 04:35:20

Linux RPM 方式安装 MySQL

(记得使用 root 账户进行操作,若使用普通用户,那么请修改相应文件夹权限)

1 检查以前是否有安装Mysql,卸载

  1. 检查以前是否装过 MySQL

    rpm -qa|grep -i mysql
    

    centos7默认会安装mariadb,也要卸载,避免冲突

    [root@hdp01 ~]# rpm -qa |grep -i mariadb
    mariadb-libs-5.5.60-1.el7_5.x86_64
    [root@hdp01 ~]# rpm -e --nodeps mariadb-libs
    
  2. 发现有的话就都卸载
    在这里插入图片描述

  3. 删除老版本 mysql 的开发头文件和库

    rm -fr /usr/lib/mysql #数据库目录
    rm -fr /usr/include/mysql
    rm -f /etc/my.cnf
    rm -fr /var/lib/mysql
    
    

注意:卸载后/var/lib/mysql 中的数据及/etc/my.cnf 不会删除,确定没用后就手工删除

2 下载安装包

https://dev.mysql.com/downloads/mysql/5.6.html#downloads
在这里插入图片描述
但是官网下载太慢了,我们还是从清华镜像下载

[root@hdp01 apps]# wget https://mirrors.tuna.tsinghua.edu.cn/mysql/downloads/MySQL-5.7/mysql-5.7.25-1.el7.x86_64.rpm-bundle.tar
[root@hdp01 apps]# tar -xvf mysql-5.7.25-1.el7.x86_64.rpm-bundle.tar 
mysql-community-libs-5.7.25-1.el7.x86_64.rpm
mysql-community-libs-compat-5.7.25-1.el7.x86_64.rpm
mysql-community-embedded-devel-5.7.25-1.el7.x86_64.rpm
mysql-community-embedded-5.7.25-1.el7.x86_64.rpm
mysql-community-client-5.7.25-1.el7.x86_64.rpm
mysql-community-server-5.7.25-1.el7.x86_64.rpm
mysql-community-embedded-compat-5.7.25-1.el7.x86_64.rpm
mysql-community-test-5.7.25-1.el7.x86_64.rpm
mysql-community-devel-5.7.25-1.el7.x86_64.rpm
mysql-community-common-5.7.25-1.el7.x86_64.rpm

3 开始安装

在这里插入图片描述

[root@hdp01 mysql-rpm]# rpm -ivh mysql-community-common-5.7.25-1.el7.x86_64.rpm 
[root@hdp01 mysql-rpm]# rpm -ivh mysql-community-libs-5.7.25-1.el7.x86_64.rpm 
[root@hdp01 mysql-rpm]# rpm -ivh mysql-community-client-5.7.25-1.el7.x86_64.rpm 
[root@hdp01 mysql-rpm]# rpm -ivh mysql-community-server-5.7.25-1.el7.x86_64.rpm 

找到初始密码
vi /var/log/mysqld.log
2019-12-29T12:39:26.213233Z 1 [Note] A temporary password is generated for root@localhost: !%;foq4hiKT4

如果找不到这个密码文件,就用下面方式重置

4 重置数据库密码

编辑my.cnf允许空密码登录

vi /etc/my.cnf
#在[mysqld]下添加
skip-grant-tables=1

重新启动Mysql服务
使用Root登录数据库、使用mysql数据库、修改root密码、退出数据库

[root@hdp01 etc]# systemctl restart mysqld.service
[root@hdp01 etc]# mysql -uroot
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.25 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> update user set authentication_string=password('123456'),password_last_changed=now() where user='root';
Query OK, 1 row affected, 1 warning (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 1

mysql> exit
Bye

再次打开my.cnf,将skip-grant-tables=1删掉,保存退出
重启mysql服务
再次试验登录OK

[root@hdp01 etc]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.25

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

5 修改mysql密码过期问题(无则跳过)

登录发现无论执行什么命令都报错:

mysql> show datables;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'datables' at line 1

查看过期

mysql> select host,user,authentication_string,password_expired from user;
+-----------+---------------+-------------------------------------------+------------------+
| host      | user          | authentication_string                     | password_expired |
+-----------+---------------+-------------------------------------------+------------------+
| localhost | root          | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | Y                |
| localhost | mysql.session | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | N                |
| localhost | mysql.sys     | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | N                |
+-----------+---------------+-------------------------------------------+------------------+
3 rows in set (0.00 sec)

修改过期

mysql> update user set password_expired='N' where user='root';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

6 解决Your password does not satisfy the current policy requirements

mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION; 
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements

原因:

原来MySQL5.6.6版本之后增加了密码强度验证插件validate_password,相关参数设置的较为严格。
使用了该插件会检查设置的密码是否符合当前设置的强度规则,若不满足则拒绝设置。影响的语句和函数有:create user,grant,set password,password(),old password。

解决办法:

  1. 查看全局参数

    mysql> select @@validate_password_policy; 
    +----------------------------+
    | @@validate_password_policy |
    +----------------------------+
    | MEDIUM                     |
    +----------------------------+
    1 row in set (0.00 sec)
    
    mysql> SHOW VARIABLES LIKE 'validate_password%';  
    +--------------------------------------+--------+
    | Variable_name                        | Value  |
    +--------------------------------------+--------+
    | validate_password_check_user_name    | OFF    |
    | validate_password_dictionary_file    |        |
    | validate_password_length             | 8      |
    | validate_password_mixed_case_count   | 1      |
    | validate_password_number_count       | 1      |
    | validate_password_policy             | MEDIUM |
    | validate_password_special_char_count | 1      |
    +--------------------------------------+--------+
    7 rows in set (0.00 sec)
    
    
  2. 参数解释

    参数 解释
    validate_password_dictionary_file 插件用于验证密码强度的字典文件路径
    alidate_password_length 密码最小长度,参数默认为8,它有最小值的限制,最小值为:validate_password_number_count + validate_password_special_char_count + (2 * validate_password_mixed_case_count)
    alidate_password_mixed_case_count 密码至少要包含的小写字母个数和大写字母个数。
    alidate_password_number_count 密码至少要包含的数字个数。
    alidate_password_policy 密码强度检查等级,0/LOW、1/MEDIUM、2/STRONG。
    alidate_password_special_char_count 密码至少要包含的特殊字符数。

    在这里插入图片描述

  3. 修改配置

mysql> set global validate_password_policy=0;  
Query OK, 0 rows affected (0.00 sec)

mysql> set global validate_password_mixed_case_count=0;  
Query OK, 0 rows affected (0.00 sec)

mysql>  set global validate_password_number_count=3;  
Query OK, 0 rows affected (0.00 sec)

mysql> set global validate_password_special_char_count=0;  
Query OK, 0 rows affected (0.00 sec)

mysql> set global validate_password_length=3;  
Query OK, 0 rows affected (0.00 sec)

mysql> SHOW VARIABLES LIKE 'validate_password%';  
+--------------------------------------+-------+
| Variable_name                        | Value |
+--------------------------------------+-------+
| validate_password_check_user_name    | OFF   |
| validate_password_dictionary_file    |       |
| validate_password_length             | 3     |
| validate_password_mixed_case_count   | 0     |
| validate_password_number_count       | 3     |
| validate_password_policy             | LOW   |
| validate_password_special_char_count | 0     |
+--------------------------------------+-------+
7 rows in set (0.00 sec)

7 增加远程登陆权限

mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION; 
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!