WHCSRL 技术网

Centos7安装MySQL5.x版本详细步骤

1、下载地址

MySQL :: Download MySQL Community Server (Archived Versions)https://downloads.mysql.com/archives/community/

2、使用 yum

命令安装MySql组件

使用命令yum install -y {-file-name}进行安装操作。

按照依赖关系依次安装rpm包 依赖关系依次为common→libs→client→server!!!

  1. yum install mysql-community-common-5.7.35-1.el7.x86_64.rpm
  2. yum install -y mysql-community-libs-5.7.35-1.el7.x86_64.rpm
  3. yum install -y mysql-community-client-5.7.35-1.el7.x86_64.rpm
  4. yum install -y mysql-community-server-5.7.35-1.el7.x86_64.rpm

在安装 mysql-community-libs-5.7.35-1.el7.x86_64.rpm 时有可能会报错:mysql依赖错误:

  1. conflicts with file from package mariadb-libs-1:5.5.68-1.el7.x86_64
  2. file /usr/share/mysql/charsets/macce.xml from install of mysql-community-common-5.7.35-1.el7.x86_64 conflicts with file from package mariadb-libs-1:5.5.68-1.el7.x86_64
  3. file /usr/share/mysql/charsets/macroman.xml from install of mysql-community-common-5.7.35-1.el7.x86_64 conflicts with file from package mariadb-libs-1:5.5.68-1.el7.x86_64
  4. file /usr/share/mysql/charsets/swe7.xml from install of mysql-community-common-5.7.35-1.el7.x86_64 conflicts with file from package mariadb-libs-1:5.5.68-1.el7.x86_64

 解决办法:

  1. rpm -qa|grep mysql
  2. yum remove -y mysql-libs
  3. 或者
  4. rpm -qa | grep mariadb-libs
  5. yum remove -y mariadb-libs

3、启停mysql并开机自启

  1. #启动 mysql
  2. systemctl start mysqld
  3. #重启 mysql
  4. systemctl restart mysqld
  5. #停止 mysql
  6. systemctl stop mysqld 
  7. #设置 mysql 开机启动
  8. systemctl enable mysqld

4、mysql常用文件路径

  1. /etc/my.cnf   这是mysql的主配置文件
  2. /var/lib/mysql   mysql数据库的数据库文件存放位置
  3. /var/logs/mysqld.log  数据库的日志输出存放位置

5、进入mysql

此时如果要进入MySQL得找出root用户的密码,输入命令

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

得到密码后,登录mysql,输入命令

mysql -uroot -p

此时,你需要给你的MySql重新设置密码,因为MySQL默认的就是必须修改密码后才能操作数据库。

alter user 'root'@'localhost' identified by '123';

 以修改密码设置规范,毕竟你不登录你就不能修改规范。

总之呢,我们先将密码设置成比较复杂的就好,比如 Abc@1234567890...,先登录了再说。

alter user 'root'@'localhost' identified by 'Abc@1234567890';

 查看密码规范

SHOW VARIABLES LIKE 'validate_password%%';

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

validate_password_length = validate_password_number_count + validate_password_special_char_count + (2 * validate_password_mixed_case_count)

这时候我们将密码设置规范修改一下:

  1. set global validate_password_policy=0; # 关闭密码复杂性策略
  2. set global validate_password_length=4; # 设置密码最低长度为4


 

6、然后进行密码重置

mysql_secure_installation命令:
  1. [root@mysql5 ~]# mysql_secure_installation
  2. Securing the MySQL server deployment.
  3. Enter password for user root:
  4. Error: Access denied for user 'root'@'localhost' (using password: YES)
  5. [root@mysql5 ~]# mysql_secure_installation
  6. Securing the MySQL server deployment.
  7. Enter password for user root:
  8. Error: Access denied for user 'root'@'localhost' (using password: YES)
  9. [root@mysql5 ~]# mysql_secure_installation
  10. Securing the MySQL server deployment.
  11. Enter password for user root:
  12. The 'validate_password' plugin is installed on the server.
  13. The subsequent steps will run with the existing configuration
  14. of the plugin.
  15. Using existing password for root.
  16. Estimated strength of the password: 100
  17. Change the password for root ? ((Press y|Y for Yes, any other key for No) : y
  18. New password:
  19. Re-enter new password:
  20. Estimated strength of the password: 50
  21. Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : n
  22. New password:
  23. Re-enter new password:
  24. Estimated strength of the password: 50
  25. Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
  26. By default, a MySQL installation has an anonymous user,
  27. allowing anyone to log into MySQL without having to have
  28. a user account created for them. This is intended only for
  29. testing, and to make the installation go a bit smoother.
  30. You should remove them before moving into a production
  31. environment.
  32. Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
  33. Success.
  34. Normally, root should only be allowed to connect from
  35. 'localhost'. This ensures that someone cannot guess at
  36. the root password from the network.
  37. Disallow root login remotely? (Press y|Y for Yes, any other key for No) : n
  38. ... skipping.
  39. By default, MySQL comes with a database named 'test' that
  40. anyone can access. This is also intended only for testing,
  41. and should be removed before moving into a production
  42. environment.
  43. Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
  44. - Dropping test database...
  45. Success.
  46. - Removing privileges on test database...
  47. Success.
  48. Reloading the privilege tables will ensure that all changes
  49. made so far will take effect immediately.
  50. Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
  51. Success.
  52. All done!

7、授权远程登录

  1. use mysql;
  2. select user,host,grant_priv from user;
create user 'root'@'%%' identified by 'YOU_PASSWORD';

更新授权数据库:

  1. update user set grant_priv='Y' where user='root';
  2. flush privileges;

现在就能给用户赋予权限了

授权登录
登录mysql之后执行

GRANT ALL ON *.* TO 'root'@'%%';

 如果使用客户端连接提示了plugin caching_sha2_password错误,这是因为MySQL8.0的密码策略默认为caching_sha2_password

 使用命令修改策略

ALTER USER 'root'@'%%' IDENTIFIED WITH mysql_native_password BY 'XXXXXXXXXXXXXX';

即可远程登录了!!!
 

推荐阅读