我是在虚拟机上配置的,用于自学。
虚拟机系统是centos8。
接下来说下操作步骤。

删除原来的mysql

先删除环境中存在的mysql

  1. rpm -qa | grep -i mysql
  2. yum -y remove 上述出现的

把所有出现的目录统统删除

  1. find / -name mysql

删除配置文件

  1. rm -rf /etc/my.cnf

删除mysql的默认密码

  1. rm -rf /root/.mysql_sercret

执行安装

如果是新机器,curl工具需要更新才能下载

  1. yum update curl -y

配置Mysql 8.0安装源

  1. # root或具备sudo特权的用户下执行
  2. sudo dnf install @mysql

安装Mysql 8.0

  1. sudo systemctl enable --now mysqld

启动MySQ服务

  1. sudo systemctl status mysqld

启动安装脚本

  1. sudo mysql_secure_installation

我的选择是这样的

  1. [root@node1 yum.repos.d]# mysql_secure_installation
  2. Securing the MySQL server deployment.
  3. Connecting to MySQL using a blank password.
  4. VALIDATE PASSWORD COMPONENT can be used to test passwords
  5. and improve security. It checks the strength of password
  6. and allows the users to set only those passwords which are
  7. secure enough. Would you like to setup VALIDATE PASSWORD component?
  8. Press y|Y for Yes, any other key for No: n 不使用验证组件
  9. Please set the password for root here.
  10. New password:
  11. Re-enter new password:
  12. By default, a MySQL installation has an anonymous user,
  13. allowing anyone to log into MySQL without having to have
  14. a user account created for them. This is intended only for
  15. testing, and to make the installation go a bit smoother.
  16. You should remove them before moving into a production
  17. environment.
  18. Remove anonymous users? (Press y|Y for Yes, any other key for No) : y 允许匿名用户
  19. Success.
  20. Normally, root should only be allowed to connect from
  21. 'localhost'. This ensures that someone cannot guess at
  22. the root password from the network.
  23. Disallow root login remotely? (Press y|Y for Yes, any other key for No) : n 允许远程连接
  24. ... skipping.
  25. By default, MySQL comes with a database named 'test' that
  26. anyone can access. This is also intended only for testing,
  27. and should be removed before moving into a production
  28. environment.
  29. Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
  30. - Dropping test database...
  31. Success.
  32. - Removing privileges on test database...
  33. Success.
  34. Reloading the privilege tables will ensure that all changes
  35. made so far will take effect immediately.
  36. Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y 快速加载上述信息
  37. Success.
  38. All done!

修改身份验证插件

  1. sudo vim /etc/my.cnf.d/mysql-default-authentication-plugin.cnf
  1. [mysqld]
  2. default_authentication_plugin=mysql_native_password

然后重启mysql

  1. systemctl restart mysqld

更改密码

进入myql命令行。登录成功后输入

  1. ALTER USER 'root'@'localhost' IDENTIFIED BY 'xxxxxxx'; ---xxxxxxx:就是你的密码

客户端连接mysql报错

原因是Mysql默认不允许远程登录,所以需要开启远程访问权限

  1. use mysql;
  2. select user,host from user;


mysql> select host,user from user;
| host | user |
| localhost | mysql.infoschema |
| localhost | mysql.session |
| localhost | mysql.sys |
| localhost | root |

默认都是localhost

  1. -- 更改为%
  2. update user set host = '%' where user = 'root';
  3. flush privileges;

如果不打算停掉防火墙,可以把这个端口加入规则

  1. firewall-cmd --zone=public --add-port=3306/tcp --permanent
  2. firewall-cmd --reload