Qingquan-Li / blog

My Blog
https://Qingquan-Li.github.io/blog/
132 stars 16 forks source link

Ubuntu 安装 MySQL 5.7 并设置被允许远程连接 #151

Open Qingquan-Li opened 4 years ago

Qingquan-Li commented 4 years ago

环境:


一、安装 MySQL 5.7

参考:

  1. 简书 - Ubuntu 16.04 安装 MySQL 5.7,并设置远程访问
  2. FossTechNix - How to Install MySQL 5.7 on Ubuntu 18.04 LTS
  3. MySQL Document - A Quick Guide to Using the MySQL APT Repository
  4. 个人博客 - ubuntu 18.04 安装 mysql-server 5.7
# 更新 Ubuntu 源列表,并查看现有的 MySQL 官方包
$ sudo apt-get update
$ apt-cache search mysql-server
mysql-server - MySQL database server (metapackage depending on the latest version)
mysql-server-5.7 - MySQL database server binaries and system database setup
mysql-server-core-5.7 - MySQL database server binaries
auth2db - Powerful and eye-candy IDS logger, log viewer and alert generator
mariadb-server-10.0 - MariaDB database server binaries
mariadb-server-core-10.0 - MariaDB database core server files
percona-server-server-5.6 - Percona Server database server binaries
percona-xtradb-cluster-server-5.6 - Percona XtraDB Cluster database server binaries
# 根据上面的输出可以看到Ubuntu 16.04 LTS下,默认将会安装最新版mysql-server-5.7
# 否则需要参考上面参考链接2和3,下载MySQL APT存储库,并选择MySQL 5.7进行安装
# 开始安装
$ sudo apt-get install mysql-server
...
确定安装、设置root用户密码等操作...
...
# 查看MySQL版本
$ mysql -V
mysql  Ver 14.14 Distrib 5.7.29, for Linux (x86_64) using  EditLine wrapper


$ mysql -h host -u user -p 连接 MySQL ,查看有哪些数据库:

mysql> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.02 sec)

mysql> QUIT;
Bye


二、设置 MySQL 允许被远程连接

参考:https://www.ghacks.net/2009/12/27/allow-remote-connections-to-your-mysql-server/


1. 2003错误:Can't connect to MySQL server on mysql-server的IP地址

假如此处的 Ubuntu 为服务器,在本地远程连接 mysql-server 将会报错 2003 , 原因是 mysql-server 只允许本地连接,不允许被远程连接:

$ mysql -h 129.xxx.xx.148 -u root -p
ERROR 2003 (HY000): Can't connect to MySQL server on '129.xxx.xx.148' (61)

ssh 连接到 Ubuntu 服务器,然后在 Ubuntu 服务器,使用以下命令查看 3306 端口允许哪个 IP 连接:

$ netstat -an|grep 3306
# 127.0.0.1

解决方案:

参考:https://stackoverflow.com/questions/1673530/error-2003-hy000-cant-connect-to-mysql-server-on-127-0-0-1-111

使用以下命令进入 MySQL 配置文件,使用 # 注释掉默认配置的 bind-address = 127.0.0.1 (或者将 bind-address 设置为 bind-address = 0.0.0.0 )。 bind-address = 127.0.0.1 配置行在 my.cnf 或者 mysqld.cnf 文件中:

$ sudo vim /etc/mysql/my.cnf
$ sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf

重启 MySQL,使修改后的配置生效:

$ sudo /etc/init.d/mysql restart


2. 1130错误:Host 发起连接的IP地址 is not allowed to connect to this MySQL server

解决上面的 2003 错误后,再次尝试在本地进行连接远程的 Ubuntu 服务器的 mysql-server ,报错 1130 ,本地的 IP 地址不被允许连接到 mysql-server :

$ mysql -h 129.xxx.xx.148 -u root -p
ERROR 1130 (HY000): Host '183.x.xxx.163' is not allowed to connect to this MySQL server

ssh 连接到 Ubuntu 服务器,然后在 Ubuntu 服务器上连接 MySQL $ mysql -u root -p 。 可以看到,使用 root 用户连接到 mysql-server 只有 localhost host 被允许,其他 IP 不被允许使用 root 用户连接到 mysql-server :

mysql> SELECT host FROM mysql.user WHERE User = 'root';
+-----------+
| host      |
+-----------+
| localhost |
+-----------+

解决方案:

参考:https://stackoverflow.com/questions/19101243/error-1130-hy000-host-is-not-allowed-to-connect-to-this-mysql-server

You will need to add the IP address of each system that you want to grant access to, and then grant privileges:

CREATE USER 'root'@'ip_address' IDENTIFIED BY 'some_pass';
GRANT ALL PRIVILEGES ON *.* TO 'root'@'ip_address';

If however you do want any/all systems to connect via root, use the % wildcard to grant access:

CREATE USER 'root'@'%' IDENTIFIED BY 'some_pass';
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%';

Finally, reload the permissions, and you should be able to have remote access:

FLUSH PRIVILEGES;