环境说明
centos7
注意:这是我安装很多次失败中总结,希望你可以按照我的步骤,一步一步来,应该是不会有问题的。
如果你已经安装了mysql,需要先卸载,卸载请参考:
Centos7 完全卸载mysql
检查 MariaDB 是否安装
yum list installed | grep mariadb
卸载全部 MariaDB 相关
yum -y remove mariadb*
数据准备
下载mysql安装包
[root@localhost ~]# cd /usr/local/src/
[root@localhost src]# wget https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
下载完后可以目录中看到:
安装mysql源
rpm -ivh mysql80-community-release-el7-3.noarch.rpm
检查 MySQL 的 YUM 源是否安装成功
yum repolist enabled | grep "mysql.*-community.*"
查看 MySQL 版本
yum repolist all | grep mysql
安装 MySQL
yum install mysql-community-server
启动mysql服务
systemctl start mysqld
附加命令
查看MySQL状态 systemctl status mysqld
关闭MySQL服务 systemctl stop mysqld
重启MySQL服务 systemctl restart mysqld
查看自动生成的临时密码
grep 'temporary password' /var/log/mysqld.log
这个密码先自己保存下
登录mysql
使用刚才生成的临时密码
mysql -uroot -p
这时候对数据的操作会报错,必须先改密码
mysql8.0修改密码和以往不同,我看过别的教程,修改成功后,无法使用该密码进行登录,正确方式如下(引号后面是密码)
密码需要包含大小写字母、数字、符号长度也不能过短,暂时先弄一个很长的,一会再改。
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY "#20as3SElk3@wwerwer232342432342343223243sdfasfafasdsds0ew98";
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY "#20as3SElk3@wwerwer232342432342343223243sdfasfafasdsds0ew98";
刷新
FLUSH privileges;
此时我们退出,重新登录看看效果
(输入密码我们用复制的,在xshell中操作)
mysql> quit;
Bye
[root@localhost src]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.17 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>
可以登录!
设置安全策略
因为要把刚才的密码改的简单一点,这里需要先修改安全策略,mysql8.0修改安全策略的命令和5.7不同,具体如下
先选择数据库:
use mysql;
5.7语法:
set global validate_password_policy=0;set global validate_password_length=1;
8.0语法:
set global validate_password.policy=0;
set global validate_password.length=1;
最后都要刷新:
FLUSH privileges;
修改密码
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> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY "123456";
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
退出重新登录
输入刚才修改的密码
mysql> quit;
Bye
[root@localhost src]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 8.0.17 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>
设置远程访问
mysql> CREATE USER 'root'@'%' IDENTIFIED BY '123456';
Query OK, 0 rows affected (2.30 sec)
mysql> GRANT ALL ON *.* TO 'root'@'%';
Query OK, 0 rows affected (0.01 sec)
mysql> ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
Query OK, 0 rows affected (0.00 sec)
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.03 sec)
mysql>
开启防火墙
[root@localhost src]# firewall-cmd --zone=public --add-port=3306/tcp --permanent
success
[root@localhost src]# firewall-cmd --reload
success