解决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月 01 10:06:12 debian systemd[1]: Starting MySQL Community Server...
    4月 01 10: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.service - 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
               └─103561 /usr/bin/python3 /usr/sbin/firewalld --nofork --nopid
    
    4月 01 11:35:14 debian systemd[1]: Starting firewalld - dynamic firewall daemon...
    4月 01 11:35:14 debian systemd[1]: Started firewalld - dynamic firewall daemon.
    

    开启的,怎么办?两种处理方式:

    ①关掉防火墙;

    # systemctl stop firewalld
    

    ②加策略;

    第一种:开放服务

    # firewall-cmd --zone=public --add-service=mysql --permanent //开放mysql服务
    # firewall-cmd --reload //重载生效
    success
    # firewall-cmd --zone=public --list-services //查看添加效果
    dhcpv6-client mysql ssh
    

    第二种:开放端口

    # firewall-cmd --zone=public --query-port=3306/tcp --permanent //开放3306端口
    # firewall-cmd --reload //重载生效
    success
    # firewall-cmd --zone=public --list-ports //查看添加效果
    3306/tcp
    

    再试下呢,还是不行!

  5. Mysql允许远程登录不?

    # mysql -uroot -p //登录mysql
    Enter password:
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 4
    Server version: 5.7.33 MySQL Community Server (GPL)
    
    Copyright (c) 2000, 2021, Oracle and/or its affiliates.
    
    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> 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> grant all privileges on *.* to root@'%' identified by 'xxxxxxxx' with grant option;
    Query OK, 0 rows affected, 1 warning (0.00 sec)
    
    mysql> flush privileges;
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> select host,user from user;
    +-----------+---------------+
    | host      | user          |
    +-----------+---------------+
    | %         | root          |
    | localhost | mysql.session |
    | localhost | mysql.sys     |
    | localhost | root          |
    +-----------+---------------+
    4 rows in set (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 with the existing configuration
    of the plugin.
    Using existing password for root.
    
    Estimated strength of the password: 50
    Change the password for root ? ((Press y|Y for Yes, any other key for No) : n
    
     ... skipping.
    By default, a MySQL installation has an anonymous user,
    allowing anyone to log into MySQL without having to have
    a user account created for them. This is intended only for
    testing, and to make the installation go a bit smoother.
    You should remove them before moving into a production
    environment.
    
    Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
    Success.
    
    
    Normally, root should only be allowed to connect from
    'localhost'. This ensures that someone cannot guess at
    the root password from the network.
    
    Disallow root login remotely? (Press y|Y for Yes, any other key for No) : n
    
     ... skipping.
    By default, MySQL comes with a database named 'test' that
    anyone can access. This is also intended only for testing,
    and should be removed before moving into a production
    environment.
    
    
    Remove test database and access to it? (Press y|Y for Yes, any other key for No) : n
    
     ... skipping.
    Reloading the privilege tables will ensure that all changes
    made so far will take effect immediately.
    
    Reload privilege tables now? (Press y|Y for Yes, any other key for No) : n
    
     ... skipping.
    All done!
    

    其中有个问题“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@debian:/etc/mysql# ls -l
    总用量 16
    drwxr-xr-x 2 root root 4096 3月  31 15:32 conf.d
    lrwxrwxrwx 1 root root   24 3月  31 15:32 my.cnf -> /etc/alternatives/my.cnf
    -rw-r--r-- 1 root root  839 8月   3  2016 my.cnf.fallback
    -rw-r--r-- 1 root root 1215 4月   1 10:00 mysql.cnf
    drwxr-xr-x 2 root 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 或者直接注释掉该行。再试下呢,终于成功了!!!

    **注:**修改配置文件之后永远要记得重启服务

引用

  1. CentOS 8 成为绝版!到底发生了什么?

  2. 如何安装Debian 10(Buster)最小服务器

  3. 如何在Debian 10(Buster)Linux上安装MySQL_数据库

  4. 开启MySQL远程访问权限允许远程连接

  5. CentOS7 下使用 Firewall防火墙系统封禁允许IP和端口的访问 端口转发 IP转发方法

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值