<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>iptables &#8211; 良的世界</title>
	<atom:link href="https://www.lemonary.cn/tag/iptables/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.lemonary.cn</link>
	<description></description>
	<lastBuildDate>Mon, 13 Jan 2025 08:25:31 +0000</lastBuildDate>
	<language>zh-Hans</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	

<image>
	<url>https://www.lemonary.cn/wp-content/uploads/2024/12/logo-150x150.jpg</url>
	<title>iptables &#8211; 良的世界</title>
	<link>https://www.lemonary.cn</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>MySQL数据库远程telnet不通只能本地连接</title>
		<link>https://www.lemonary.cn/mysql%e6%95%b0%e6%8d%ae%e5%ba%93%e8%bf%9c%e7%a8%8btelnet%e4%b8%8d%e9%80%9a%e5%8f%aa%e8%83%bd%e6%9c%ac%e5%9c%b0%e8%bf%9e%e6%8e%a5/</link>
					<comments>https://www.lemonary.cn/mysql%e6%95%b0%e6%8d%ae%e5%ba%93%e8%bf%9c%e7%a8%8btelnet%e4%b8%8d%e9%80%9a%e5%8f%aa%e8%83%bd%e6%9c%ac%e5%9c%b0%e8%bf%9e%e6%8e%a5/#respond</comments>
		
		<dc:creator><![CDATA[shine]]></dc:creator>
		<pubDate>Fri, 10 Jan 2025 06:27:19 +0000</pubDate>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[firewalld]]></category>
		<category><![CDATA[iptables]]></category>
		<guid isPermaLink="false">https://www.lemonary.cn/?p=1332</guid>

					<description><![CDATA[一、问题概述 对于一个Linux上新部署的MySQL数据库，往往在连接的时候有可能会碰到连接不上的问题。我就是 [&#8230;]]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading" id="一、问题概述">一、问题概述</h2>



<p>对于一个Linux上新部署的MySQL数据库，往往在连接的时候有可能会碰到连接不上的问题。我就是在近日，写一篇MySQL数据库迁移到DM数据库的博客时，出奇的发现我博客所用的MySQL数据库居然只能从Linux本地连接。由于对MySQL不熟悉，这个问题困扰了我两三个小时。下面本文就来总结一下，MySQL连接不上可能涉及的一些配置。</p>



<h2 class="wp-block-heading" id="二、问题解决">二、问题解决</h2>



<p>（1）MySQL的配置文件my.cnf</p>



<p>如果你找不到你的my.cnf文件在哪，或者你的机器上有多个my.cnf文件导致你不清楚哪一个才是当前MySQL用到的my.cnf，可以使用如下命令找到my.cnf文件：</p>



<pre class="wp-block-code"><code>&#91;root@dameng ~]# mysql --help|grep my.cnf
                      order of preference, my.cnf, $MYSQL_TCP_PORT,
/etc/my.cnf /etc/mysql/my.cnf ~/.my.cnf </code></pre>



<p>可以看到有多个my.cnf文件，先后顺序表明了他们的优先级，所以只需要配置第一个/etc/my.cnf：</p>



<pre class="wp-block-code"><code>## 找到&#91;mysqld]，在下方添加 bind-address = 0.0.0.0。没有就自己新增。
&#91;mysqld]
bind-address = 0.0.0.0

## 找到 skip-networking，将其注释或删除，没有就可以不用管。
# 取消注释或删除 skip-networking 以启用网络连接
# skip-networking</code></pre>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p>注意：0.0.0.0 表示任何人都可以连接，如果没有特殊要求，为了安全起见，可以配置成具体的IP。</p>
</blockquote>



<p>重启数据库生效</p>



<pre class="wp-block-code"><code>systemctl restart mysqld</code></pre>



<p>（2）配置数据库的用户访问权限</p>



<p>新部署的MySQL数据库，root用户默认是127.0.0.1/localhost，那么这个用户就只能本地访问，其它机器用这个用户访问会提示没有权限，所以要将host改为%，表示允许所有机器访问。</p>



<pre class="wp-block-code"><code>mysql&gt; 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&gt; select Host,User from user;
+-----------+------------------+
| Host      | User             |
+-----------+------------------+
| localhost | mysql.infoschema |
| localhost | mysql.session    |
| localhost | mysql.sys        |
| localhost | root             |
+-----------+------------------+
4 rows in set (0.01 sec)

mysql&gt; update user set host='%' where host='localhost';
Query OK, 4 rows affected (0.01 sec)
Rows matched: 4  Changed: 4  Warnings: 0

mysql&gt; commit;
Query OK, 0 rows affected (0.00 sec)

mysql&gt; flush privileges;
Query OK, 0 rows affected (0.00 sec)</code></pre>



<p>（3）防火墙firewalld和iptables</p>



<p>由于之前我就将firewalld关闭了，所以这个方面就没有多想。没想到，问题恰恰就出在这里。所以，今后在解决问题的时候一定要认真仔细，才不会疏漏掉任何的细节，才能更准确地定位问题的所在。</p>



<p>关闭firewalld</p>



<pre class="wp-block-code"><code>## 关闭防火墙
systemctl stop firewalld
## 禁用防火墙开机自启
systemctl disable firewalld</code></pre>



<p>我不清楚为什么我的服务器有两个防火墙，firewalld和iptables同时存在，所以导致我漏掉了iptables。而且我从iptables的配置文件/etc/sysconfig/iptables中看到了如下配置：</p>



<pre class="wp-block-code"><code>-A INPUT -p tcp -m tcp --dport 3306 -j DROP</code></pre>



<p>所以我把DROP修改成了ACCEPT，并且新增一行，开启3306端口：</p>



<pre class="wp-block-code"><code>-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT</code></pre>



<p>重启iptables服务</p>



<pre class="wp-block-code"><code>systemctl restart iptables</code></pre>



<p>（4）验证登录-通过网络IP连接</p>



<pre class="wp-block-code"><code>&#91;root@dameng ~]# mysql -h ip -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 397
Server version: 8.0.26 Source distribution

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&gt; </code></pre>



<p>登录成功。</p>



<h2 class="wp-block-heading" id="三、问题总结">三、问题总结</h2>



<p>万事要认真仔细。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.lemonary.cn/mysql%e6%95%b0%e6%8d%ae%e5%ba%93%e8%bf%9c%e7%a8%8btelnet%e4%b8%8d%e9%80%9a%e5%8f%aa%e8%83%bd%e6%9c%ac%e5%9c%b0%e8%bf%9e%e6%8e%a5/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
