在安装过程中,有很多细节问题,也找了好几个帖子,每个帖子都是只解决一部分问题。现在整理的这个文档,相当于从安装到后面的连接,授权远程连接全部是亲测可用的文档。
一、MySql下载
先从mysql网站下载repo源
wget https://dev.mysql.com/get/mysql80-community-release-el7-2.noarch.rpm
安装repo包
rpm -ivh mysql80-community-release-el7-2.noarch.rpm
开始安装mysql-server
yum install mysql-server
安装过程中,有两次提示按y就可以了
二、MySql启动、重启,查看状态
# 启动mysql
service mysqld start
# 输出
Redirecting to /bin/systemctl start mysqld.service
# 重启mysql
service mysqld restart
# 查看mysql状态
service mysqld status
# 输出
Redirecting to /bin/systemctl status mysqld.service
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since Fri 2019-03-22 17:20:03 CST; 33s ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 9129 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 9373 (mysqld)
Status: "SERVER_OPERATING"
CGroup: /system.slice/mysqld.service
└─9373 /usr/sbin/mysqld
三、MySql配置
为了加强安全性,MySQL5.7为root用户随机生成了一个密码,在error log中,关于error log的位置,如果安装的是RPM包,则默认是/var/log/mysqld.log。
只有启动过一次mysql才可以查看临时密码
查看临时密码
# 如果之前安装过MySQL则这里可能会有多个密码,用最后一个,注意这个密码输入时是可以粘贴的
grep 'temporary password' /var/log/mysqld.log
# 输出
2019-03-22T09:19:56.137378Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: 4uuEtigGF5<=
# 密码是 4uuEtigGF5<=
使用默认密码登录,注意随机密码可能有字符问题,所以先登录再输入密码
mysql -uroot -p
# 粘贴密码
Enter password:
登录后马上修改密码
ALTER USER 'root'@'localhost' IDENTIFIED BY 'password';
# 密码太简单出现1819提示
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
# mysql8.0以前可以使用
mysql> set global validate_password_policy=0;
ERROR 1193 (HY000): Unknown system variable 'validate_password_policy'
mysql> set global validate_password_length=1;
ERROR 1193 (HY000): Unknown system variable 'validate_password_length'
# mysql8.0更改了配置名称
mysql> set global validate_password.policy=0;
Query OK, 0 rows affected (0.00 sec)
mysql> set global validate_password.length=1;
Query OK, 0 rows affected (0.00 sec)
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'password';
Query OK, 0 rows affected (0.03 sec)
四、授权远程登录
# 数据库装的是8.0所以之前的设置远程登录的方式已经不适用了。
# 远程连接,不推荐root用户连接
# 选择数据库
use mysql;
# 创建一个用户,@ '%'表示任意ip连接
CREATE USER 'test'@'%' IDENTIFIED BY 'password';
# 修改加密规则
ALTER USER 'test'@'%' IDENTIFIED BY 'password' PASSWORD EXPIRE NEVER;
# 更新下用户的密码
ALTER USER 'test'@'%' IDENTIFIED WITH mysql_native_password BY 'password';
# 刷新权限
flush privileges;
远程连接测试
# 注意端口前面的-P是大写,最后那个-p是小写
mysql -h 192.168.11.85 -P 3306 -u test-p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 32
Server version: 8.0.15 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> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.01 sec)
以上安装过程,亲测可以,安装过程是在腾讯云主机上操作的。
文章里的ip换成你自己的就可以了。
如果跟着文档还有问题,那可能就是最后授权远程登录那里有问题了。需要自己解决下。