解决linux(debian10)中mysql无法远程连接
Centos8今年底停止维护,Centos7到2024年维护期限到达之后停止维护。作为还在使用 Centos的运维,还是有些小伤心呢。白嫖党怎么办?有人说,Debian才是王道。Debian是啥,没怎么听说啊。装个虚拟机玩玩,再装个基本的应用比如Mysql试试。安装教程(教程见引用)一步一步走,安装Debian、Mysql基本都很顺利,就是Mysql无法开启远程连接。怎么解决?
1. 网络通不通?
ping下呢,通的!
telnet呢,不通!要解决的就是这个问题。
2. Mysql是否正常运行?
# systemctl status mysql
● mysql.service - MySQL Community Server
Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2021-04-01 10:06:14 CST; 7s ago
Process: 72107 ExecStartPre=/usr/share/mysql/mysql-systemd-start pre (code=exited, status=0/SUCCESS)
Process: 72142 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid (code=exited, status=0/SUCCESS)
Main PID: 72144 (mysqld)
Tasks: 27 (limit: 2330)
Memory: 181.1M
CGroup: /system.slice/mysql.service
└─72144 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
4月 0110:06:12 debian systemd[1]: Starting MySQL Community Server...
4月 0110:06:14 debian systemd[1]: Started MySQL Community Server.
正常的啊
3. 端口在监听吗?
# netstat -ntlp | grep 3306
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 72144/mysqld
在监听啊
4. 防火墙呢?
# systemctl status firewalld
- firewalld - dynamic firewall daemon
Loaded: loaded (/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2021-04-01 11:35:14 CST; 19s ago
Docs: man:firewalld(1)
Main PID: 103561 (firewalld)
Tasks: 2 (limit: 2330)
Memory: 20.4M
CGroup: /system.slice/firewalld.service
/usr/bin/python3 /usr/sbin/firewalld --nofork --nopid
11:35:14 debian systemd[1]: Starting firewalld - dynamic firewall daemon...
11:35:14 debian systemd[1]: Started firewalld - dynamic firewall daemon.
开启的,怎么办?两种处理方式:
①关掉防火墙;
# systemctl stop firewalld
②加策略;也有两种方式
第一种:开放服务
success
dhcpv6-client mysql ssh
第二种:开放端口
success
3306/tcp
再试下呢,还是不行!
5. Mysql允许远程登录不?
# mysql -uroot -p //登录mysql
Enter password:
Welcome to the MySQL monitor. Commands endwith ; or \g.
Your MySQL connection id is 4
Server version:5.7.33 MySQL Community Server (GPL)
Copyright (c) 2000, 2021, Oracle and/orits affiliates.
Oracle is a registered trademark of Oracle Corporation and/orits
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> use mysql;
Reading table information forcompletion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> grant all privileges on *.* to root@'%'identified by 'xxxxxxxx' with grant option;
Query OK, 0rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0rows affected (0.00 sec)
mysql> select host,user from user;
+-----------+---------------+
| host |user |
+-----------+---------------+
|% | root |
| localhost |mysql.session |
|localhost | mysql.sys |
| localhost |root |
+-----------+---------------+
4 rows inset (0.00 sec)
再试下呢,还是不行!
6. Mysql配置不对吗?
# vim /etc/mysql/my.cnf
并没有百度各种答案里配置的bind-address = 127.0.0.1啊,没有就加上bind-address = 0.0.0.0再试试,还是不行!
注:修改配置文件之后记得
# systemctl restart firewalld
7. Mysql安装安全加固的问题?
哦哦哦,想起Debian在线安装mysql的时候比Centos多执行了一个命令
# sudo mysql_secure_installation
Securing the MySQL server deployment.
Enter password for user root:
The 'validate_password' plugin is installed on the server.
The subsequent steps will run withthe existing configuration
ofthe plugin.
Usingexisting password for root.
Estimated strength ofthe password: 50
Changethe password for root ? ((Press y|Y for Yes, any other key for No) : n
... skipping.
Bydefault, a MySQL installation has an anonymous user,
allowing anyone tolog into MySQL without having to have
a useraccount created for them. This is intended only for
testing, andto make the installation go a bit smoother.
You should remove them beforemoving into a production
environment.
Remove anonymous users? (Press y|Y forYes, any other key for No) : y
Success.
Normally, root should onlybe allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root passwordfrom the network.
Disallowroot login remotely? (Press y|Y for Yes, any other key for No) : n
... skipping.
Bydefault, MySQL comes with a database named 'test' that
anyone can access. This isalso intended only for testing,
andshould be removed before moving into a production
environment.
Remove testdatabase and access to it? (Press y|Y for Yes, any other key for No) : n
... skipping.
Reloading the privilege tableswill ensure that all changes
made so farwill take effect immediately.
Reload privilege tablesnow? (Press y|Y for Yes, any other key for No) : n
... skipping.
Alldone!
其中有个问题“Disallow root login remotely? (Press y|Y for Yes, any other key for No) : n”我明明选择的是No啊,怎么还是不行。难道是我搞错了?再试几遍呢,还是不行!法克!
注:重新配置之后记得重启
8. Mysql配置还是不对吗?
经过上一步,发现Centos7安装和Debian10在线安装的Mysql还是有区别的。配置文件my.cnf位置不一样,Centos7的配置文件在/etc/下,而Debian10的配置文件/etc/mysql/下面。咦……,Debian10还专门给mysql建了配置文件目录啊,看看里面有啥
root/etc/mysql# ls -l :
总用量 16
drwxr-xr-x 2root root 4096 3月 31 15:32 conf.d
lrwxrwxrwx 1root root 24 3月 31 15:32 my.cnf -> /etc/alternatives/my.cnf
-rw-r--r-- 1root root 839 8月 3 2016 my.cnf.fallback
-rw-r--r-- 1root root 1215 4月 1 10:00 mysql.cnf
drwxr-xr-x 2root root 4096 4月 1 10:05 mysql.conf.d
这里面有两个目录conf.d、mysql.conf.d,两个cnf文件,一个.cnf.fallback文件。把所有文件全部打开看看
root@debian:/etc/mysql# cat ./conf.d/mysql.cnf
[mysql]
root@debian:/etc/mysql# cat ./conf.d/mysqldump.cnf
[mysqldump]
quick
quote-names
max_allowed_packet = 16M
root@debian:/etc/mysql# cat my.cnf
# Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License, version 2.0,
# as published by the Free Software Foundation.
#
# This program is also distributed with certain software (including
# but not limited to OpenSSL) that is licensed under separate terms,
# as designated in a particular file or component or in included license
# documentation. The authors of MySQL hereby grant you an additional
# permission to link the program and your derivative works with the
# separately licensed software that they have included with MySQL.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License, version 2.0, for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/
root@debian:/etc/mysql# cat my.cnf.fallback
#
# The MySQL database server configuration file.
#
# You can copy this to one of:
# - "/etc/mysql/my.cnf" to set global options,
# - "~/.my.cnf" to set user-specific options.
#
# One can use all long options that the program supports.
# Run program with --help to get a list of available options and with
# --print-defaults to see which it would actually understand and use.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html
# This will be passed to all mysql clients
# It has been reported that passwords should be enclosed with ticks/quotes
# escpecially if they contain "#" chars...
# Remember to edit /etc/mysql/debian.cnf when changing the socket location.
# Here is entries for some specific programs
# The following values assume you have at least 32M ram
!includedir /etc/mysql/conf.d/
root@debian:/etc/mysql# cat mysql.cnf
# Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License, version 2.0,
# as published by the Free Software Foundation.
#
# This program is also distributed with certain software (including
# but not limited to OpenSSL) that is licensed under separate terms,
# as designated in a particular file or component or in included license
# documentation. The authors of MySQL hereby grant you an additional
# permission to link the program and your derivative works with the
# separately licensed software that they have included with MySQL.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License, version 2.0, for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/
root@debian:/etc/mysql# cat mysql.conf.d/mysqld.cnf
# Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License, version 2.0,
# as published by the Free Software Foundation.
#
# This program is also distributed with certain software (including
# but not limited to OpenSSL) that is licensed under separate terms,
# as designated in a particular file or component or in included license
# documentation. The authors of MySQL hereby grant you an additional
# permission to link the program and your derivative works with the
# separately licensed software that they have included with MySQL.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License, version 2.0, for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# The MySQL Server configuration file.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
log-error = /var/log/mysql/error.log
# By default we only accept connections from localhost
bind-address = 127.0.0.1
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
root@debian:/etc/mysql#
./my.cnf == ./mysql.cnf,内容完全一样。**里面配置了两个目录conf.d、mysql.conf.d,里面包含./conf.d/mysql.cnf、./conf.d/mysqldump.cnf、./mysql.conf.d/mysqld.cnf,这三个文件的内容才是Debian10下mysql的全部配置内容**。在./mysql.conf.d/mysqld.cnf中配置了bind-address = 127.0.0.1,而且这是默认配置。修改为bind-address = 0.0.0.0 或者直接注释掉该行。再试下呢,终于成功了!!!
注:修改配置文件之后永远要记得重启服务