<?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>MySQL &#8211; 良的世界</title>
	<atom:link href="https://www.lemonary.cn/category/%e5%b7%a5%e4%bd%9c/mysql/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.lemonary.cn</link>
	<description></description>
	<lastBuildDate>Thu, 13 Mar 2025 09:13:38 +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/profile-150x150.jpg</url>
	<title>MySQL &#8211; 良的世界</title>
	<link>https://www.lemonary.cn</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>MariaDB数据库集群部署笔记</title>
		<link>https://www.lemonary.cn/mariadb%e6%95%b0%e6%8d%ae%e5%ba%93%e9%9b%86%e7%be%a4%e9%83%a8%e7%bd%b2%e7%ac%94%e8%ae%b0/</link>
					<comments>https://www.lemonary.cn/mariadb%e6%95%b0%e6%8d%ae%e5%ba%93%e9%9b%86%e7%be%a4%e9%83%a8%e7%bd%b2%e7%ac%94%e8%ae%b0/#respond</comments>
		
		<dc:creator><![CDATA[shine]]></dc:creator>
		<pubDate>Thu, 13 Mar 2025 09:13:35 +0000</pubDate>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[MariaDB]]></category>
		<guid isPermaLink="false">https://www.lemonary.cn/?p=1765</guid>

					<description><![CDATA[一、修改服务器主机名 二、检查selinux是否关闭 关闭方法-编辑配置文件/etc/selinux/conf [&#8230;]]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">一、修改服务器主机名</h2>



<pre class="wp-block-code"><code>hostnamectl set-hostname db1
hostnamectl set-hostname db2</code></pre>



<h2 class="wp-block-heading">二、检查selinux是否关闭</h2>



<pre class="wp-block-code"><code>sestatus</code></pre>



<p class="wp-block-paragraph">关闭方法-编辑配置文件<code>/etc/selinux/config</code>，修改22行内容如下：</p>



<pre class="wp-block-code"><code>SELINUX=disabled</code></pre>



<p class="wp-block-paragraph">重启服务器生效。</p>



<h2 class="wp-block-heading">三、关闭防火墙与开机自启</h2>



<pre class="wp-block-code"><code>systemctl stop firewalld
systemctl disable firewalld</code></pre>



<h2 class="wp-block-heading">四、配置服务器间时间同步</h2>



<p class="wp-block-paragraph"><code>/etc/chrony.conf</code>配置文件：注释第 3 行，新增第 4 行</p>



<pre class="wp-block-code"><code>#pool 2.rocky.pool.ntp.org iburst
pool ntp.aliyun.com iburst	#同步阿里云的时间</code></pre>



<p class="wp-block-paragraph">启动时间同步服务并设置开机自启</p>



<pre class="wp-block-code"><code>systemctl enable --now chronyd</code></pre>



<p class="wp-block-paragraph">查看同步状态</p>



<pre class="wp-block-code"><code>chronyc sources -v</code></pre>



<p class="wp-block-paragraph">查看时间是否一致</p>



<pre class="wp-block-code"><code>date</code></pre>



<p class="wp-block-paragraph">将系统时间写入硬件时钟</p>



<pre class="wp-block-code"><code>clock -w</code></pre>



<h2 class="wp-block-heading">五、安装MariaDB</h2>



<pre class="wp-block-code"><code>dnf -y install mariadb-server-galera</code></pre>



<h2 class="wp-block-heading">六、配置防火墙策略</h2>



<p class="wp-block-paragraph">启动防火墙</p>



<pre class="wp-block-code"><code>systemctl enable --now firewalld</code></pre>



<p class="wp-block-paragraph">防火墙中添加 MySQL 服务</p>



<pre class="wp-block-code"><code>firewall-cmd --add-service=mysql</code></pre>



<p class="wp-block-paragraph">添加集群所需的端口</p>



<pre class="wp-block-code"><code>firewall-cmd --add-port={3306/tcp,4567/tcp,4568/tcp,4444/tcp}</code></pre>



<p class="wp-block-paragraph">将当前运行的防火墙规则保存为永久配置</p>



<pre class="wp-block-code"><code>firewall-cmd --runtime-to-permanent</code></pre>



<h2 class="wp-block-heading">七、编辑集群配置文件[db1]</h2>



<pre class="wp-block-code"><code>vi /etc/my.cnf.d/galera.cnf
34 wsrep_on=1								#开启集群		
43 wsrep_cluster_name="Galera_cluster"		#定义集群的名称：Galera_cluster
46 wsrep_cluster_address="gcomm://"			#集群的地址：gcomm://
54 wsrep_node_address="192.168.241.134"		#集群的本机地址 192.168.241.134</code></pre>



<h2 class="wp-block-heading">八、创建数据库集群[db1]</h2>



<p class="wp-block-paragraph">创建数据库集群</p>



<pre class="wp-block-code"><code>galera_new_cluster</code></pre>



<p class="wp-block-paragraph">查看文件是否生成</p>



<pre class="wp-block-code"><code>&#91;root@db1 ~]# ll /var/lib/mysql/
总用量 254028
-rw-rw---- 1 mysql mysql     24576  3月 13 16:03 aria_log.00000001
-rw-rw---- 1 mysql mysql        52  3月 13 16:03 aria_log_control
-rw-rw---- 1 mysql mysql 134219032  3月 13 16:03 galera.cache
-rw-rw---- 1 mysql mysql       113  3月 13 16:03 grastate.dat
-rw-rw---- 1 mysql mysql       170  3月 13 16:03 gvwstate.dat
-rw-rw---- 1 mysql mysql       972  3月 13 16:03 ib_buffer_pool
-rw-rw---- 1 mysql mysql  12582912  3月 13 16:03 ibdata1
-rw-rw---- 1 mysql mysql 100663296  3月 13 16:03 ib_logfile0
-rw-rw---- 1 mysql mysql  12582912  3月 13 16:03 ibtmp1
-rw-rw---- 1 mysql mysql         0  3月 13 16:03 multi-master.info
drwx------ 2 mysql mysql      4096  3月 13 16:03 mysql
srwxrwxrwx 1 mysql mysql         0  3月 13 16:03 mysql.sock
-rw-rw---- 1 mysql mysql        16  3月 13 16:03 mysql_upgrade_info
drwx------ 2 mysql mysql        20  3月 13 16:03 performance_schema
-rw-rw---- 1 mysql mysql     24576  3月 13 16:03 tc.log</code></pre>



<p class="wp-block-paragraph">设置开机自启</p>



<pre class="wp-block-code"><code>systemctl enable mariadb</code></pre>



<h2 class="wp-block-heading">九、重新编辑集群配置文件[db1]</h2>



<pre class="wp-block-code"><code>vi /etc/my.cnf.d/galera.cnf 
46 wsrep_cluster_address="gcomm://192.168.241.134,192.168.241.135"   #添加集群所有节点的IP</code></pre>



<h2 class="wp-block-heading">十、数据库的安全设置[db1]</h2>



<pre class="wp-block-code"><code>&#91;root@db1 ~]# mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
haven't set the root password yet, you should just press enter here.

Enter current password for root (enter for none): 
OK, successfully used password, moving on...

Setting the root password or using the unix_socket ensures that nobody
can log into the MariaDB root user without the proper authorisation.

You already have your root account protected, so you can safely answer 'n'.

Switch to unix_socket authentication &#91;Y/n] N
 ... skipping.

You already have your root account protected, so you can safely answer 'n'.

Change the root password? &#91;Y/n] Y 
New password: 
Re-enter new password: 
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB 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? &#91;Y/n] 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? &#91;Y/n] Y
 ... Success!

By default, MariaDB 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? &#91;Y/n] Y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? &#91;Y/n] Y
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!</code></pre>



<h2 class="wp-block-heading">十一、编辑集群配置文件[db2]</h2>



<pre class="wp-block-code"><code>vi /etc/my.cnf.d/galera.cnf
34 wsrep_on=1									#开启集群
43 wsrep_cluster_name="Galera_cluster"			#定义数据库集群的名称
46 wsrep_cluster_address="gcomm://192.168.241.134,192.168.241.135"
54 wsrep_node_address="192.168.241.135"</code></pre>



<h2 class="wp-block-heading">十二、启动数据库并设置开机自启[db2]</h2>



<pre class="wp-block-code"><code>systemctl enable --now mariadb</code></pre>



<h2 class="wp-block-heading">十三、数据库的安全设置[db2]同db1一样</h2>



<pre class="wp-block-code"><code>&#91;root@db2 ~]# mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
haven't set the root password yet, you should just press enter here.

Enter current password for root (enter for none): 
OK, successfully used password, moving on...

Setting the root password or using the unix_socket ensures that nobody
can log into the MariaDB root user without the proper authorisation.

You already have your root account protected, so you can safely answer 'n'.

Switch to unix_socket authentication &#91;Y/n] N
 ... skipping.

You already have your root account protected, so you can safely answer 'n'.

Change the root password? &#91;Y/n] Y
New password: 
Re-enter new password: 
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB 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? &#91;Y/n] 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? &#91;Y/n] Y
 ... Success!

By default, MariaDB 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? &#91;Y/n] Y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? &#91;Y/n] Y
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!</code></pre>



<h2 class="wp-block-heading">十四、登录数据库查看集群状态</h2>



<pre class="wp-block-code"><code>MariaDB &#91;(none)]> show status like 'wsrep_%';
……
| wsrep_incoming_addresses      | 192.168.241.135:0,192.168.241.134:0                                                                                                            |
| wsrep_cluster_weight          | 2                                                                                                                                              |
……</code></pre>



<h2 class="wp-block-heading">十五、登录数据库创建测试数据[db2]</h2>



<pre class="wp-block-code"><code>MariaDB &#91;(none)]> create database class;
Query OK, 1 row affected (0.027 sec)

MariaDB &#91;(none)]> use class;
Database changed
MariaDB &#91;class]> create table student (学生编号 int(3) not null,姓名 nvarchar(50),身份证号 varchar(18) primary key,所在班级 int(11),成绩 int(11),check(成绩 >=0 and 成绩 &lt;=100),备注 nvarchar(2000) null);
Query OK, 0 rows affected (0.031 sec)

MariaDB &#91;class]> insert into student (学生编号,姓名,身份证号,所在班级,成绩,备注) VALUES (112,'西毒','223456789123456789',7,95,'爱好蛤蟆功');
Query OK, 1 row affected (0.007 sec)

MariaDB &#91;class]> insert into student (学生编号,姓名,身份证号,所在班级,成绩,备注) VALUES (113,'南帝','323456789123456789',2,99,'一阳指');
Query OK, 1 row affected (0.015 sec)

MariaDB &#91;class]> insert into student (学生编号,姓名,身份证号,所在班级,成绩,备注) VALUES (115,'老顽童','523456789123456789',1,99,'爱好各种武功');
Query OK, 1 row affected (0.009 sec)

MariaDB &#91;class]> insert into student (学生编号,姓名,身份证号,所在班级,成绩,备注) VALUES (116,'黄蓉','623456789123456789',3,88,'打狗棒');
Query OK, 1 row affected (0.012 sec)

MariaDB &#91;class]> insert into student (学生编号,姓名,身份证号,所在班级,成绩,备注) VALUES (117,'郭靖','723456789123456789',1,89,'降龙十八掌');
Query OK, 1 row affected (0.007 sec)

MariaDB &#91;class]> select * from student;
+--------------+-----------+--------------------+--------------+--------+--------------------+
| 学生编号     | 姓名      | 身份证号           | 所在班级     | 成绩   | 备注               |
+--------------+-----------+--------------------+--------------+--------+--------------------+
|          112 | 西毒      | 223456789123456789 |            7 |     95 | 爱好蛤蟆功         |
|          113 | 南帝      | 323456789123456789 |            2 |     99 | 一阳指             |
|          115 | 老顽童    | 523456789123456789 |            1 |     99 | 爱好各种武功       |
|          116 | 黄蓉      | 623456789123456789 |            3 |     88 | 打狗棒             |
|          117 | 郭靖      | 723456789123456789 |            1 |     89 | 降龙十八掌         |
+--------------+-----------+--------------------+--------------+--------+--------------------+
5 rows in set (0.001 sec)

MariaDB &#91;class]> </code></pre>



<h2 class="wp-block-heading">十六、登录数据库查看数据是否同步[db1]</h2>



<pre class="wp-block-code"><code>MariaDB &#91;(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| class              |
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
4 rows in set (0.002 sec)

MariaDB &#91;(none)]> use class;
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

MariaDB &#91;class]> show tables;
+-----------------+
| Tables_in_class |
+-----------------+
| student         |
+-----------------+
1 row in set (0.000 sec)

MariaDB &#91;class]> select * from student;
+--------------+-----------+--------------------+--------------+--------+--------------------+
| 学生编号     | 姓名      | 身份证号           | 所在班级     | 成绩   | 备注               |
+--------------+-----------+--------------------+--------------+--------+--------------------+
|          112 | 西毒      | 223456789123456789 |            7 |     95 | 爱好蛤蟆功         |
|          113 | 南帝      | 323456789123456789 |            2 |     99 | 一阳指             |
|          115 | 老顽童    | 523456789123456789 |            1 |     99 | 爱好各种武功       |
|          116 | 黄蓉      | 623456789123456789 |            3 |     88 | 打狗棒             |
|          117 | 郭靖      | 723456789123456789 |            1 |     89 | 降龙十八掌         |
+--------------+-----------+--------------------+--------------+--------+--------------------+
5 rows in set (0.001 sec)

MariaDB &#91;class]> </code></pre>



<p class="wp-block-paragraph">数据同步成功，集群部署完成。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.lemonary.cn/mariadb%e6%95%b0%e6%8d%ae%e5%ba%93%e9%9b%86%e7%be%a4%e9%83%a8%e7%bd%b2%e7%ac%94%e8%ae%b0/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>MySQL数据库迁移到达梦数据库超出定义长度问题</title>
		<link>https://www.lemonary.cn/mysql%e6%95%b0%e6%8d%ae%e5%ba%93%e8%bf%81%e7%a7%bb%e5%88%b0%e8%be%be%e6%a2%a6%e6%95%b0%e6%8d%ae%e5%ba%93%e8%b6%85%e5%87%ba%e5%ae%9a%e4%b9%89%e9%95%bf%e5%ba%a6%e9%97%ae%e9%a2%98/</link>
					<comments>https://www.lemonary.cn/mysql%e6%95%b0%e6%8d%ae%e5%ba%93%e8%bf%81%e7%a7%bb%e5%88%b0%e8%be%be%e6%a2%a6%e6%95%b0%e6%8d%ae%e5%ba%93%e8%b6%85%e5%87%ba%e5%ae%9a%e4%b9%89%e9%95%bf%e5%ba%a6%e9%97%ae%e9%a2%98/#respond</comments>
		
		<dc:creator><![CDATA[shine]]></dc:creator>
		<pubDate>Fri, 10 Jan 2025 07:03:02 +0000</pubDate>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[DM]]></category>
		<category><![CDATA[DTS]]></category>
		<category><![CDATA[LENGTH_IN_CHAR]]></category>
		<guid isPermaLink="false">https://www.lemonary.cn/?p=1324</guid>

					<description><![CDATA[一、问题背景 一般从MySQL迁移到DM数据库时，由于MySQL字符串的长度是以字符为单位，导致迁移过程中有可 [&#8230;]]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">一、问题背景</h2>



<p class="wp-block-paragraph">一般从MySQL迁移到DM数据库时，由于MySQL字符串的长度是以字符为单位，导致迁移过程中有可能遇到报错：超出定义长度。</p>



<p class="wp-block-paragraph">在DM8的早期阶段，针对从MySQL迁移至DM的场景，由于当时DM尚未原生支持变长字符类型（如VARCHAR(N CHAR)）的数据存储需求，故引入了LENGTH_IN_CHAR参数作为临时解决方案。旨在兼容并处理数据字符串截断的特定情境，确保迁移过程中的数据完整性。</p>



<p class="wp-block-paragraph">随着项目应用的深入及服务团队的持续反馈，LENGTH_IN_CHAR 参数的启用被发现与多种兼容性问题相关联，核心问题总结如下：</p>



<ol class="wp-block-list">
<li>LIKE查询异常
<ul class="wp-block-list">
<li>当数据库初始化配置为使用UTF-8字符集且不区分大小写，并激活 LENGTH_IN_CHAR 参数时，用户在执行 SELECT 语句采用 LIKE 模式匹配查询字符串时，曾遇到数据检索不准确的问题。</li>



<li>此问题已在DM8的 8.1.2.135 版本中得到修复。</li>
</ul>
</li>



<li>DBLINK查询字符集转换错误
<ul class="wp-block-list">
<li>在相似的配置环境下（UTF-8字符集，不区分大小写，启用 LENGTH_IN_CHAR ），通过数据库链接（DBLINK）执行 SELECT 查询时，该参数导致内部字符集转换过程发生异常，引发了字符集截断错误。</li>



<li>此技术障碍亦已在DM8的 8.1.1.117 版本中获得解决。</li>
</ul>
</li>



<li>存储差异性问题
<ul class="wp-block-list">
<li>若数据库实例配置启用了 LENGTH_IN_CHAR 参数，且字段定义采用 VARCHAR(10)，不同字符集的采用将直接影响到实际可存储的数据量。</li>



<li>这进一步凸显了该参数对数据存储一致性可能产生的不利影响，具体表现依字符编码而异。</li>
</ul>
</li>
</ol>



<p class="wp-block-paragraph">综上所述，鉴于 LENGTH_IN_CHAR 参数引发的多方面兼容性和一致性问题，结合已有的版本更新修复情况，其停用被视为提升系统稳定性和兼容性的必要举措，并且在 8.1.3.167 版本正式废弃。</p>



<p class="wp-block-paragraph">本文就是介绍目前新版DM数据库MySQL利用DTS迁移到DM数据库避免超出定义长度问题的方法。</p>



<h2 class="wp-block-heading">二、DTS迁移MySQL</h2>



<h3 class="wp-block-heading">2.1 设置数据类型映射</h3>



<figure class="wp-block-image size-full"><img fetchpriority="high" decoding="async" width="198" height="433" src="https://www.lemonary.cn/wp-content/uploads/2025/01/image-9.png" alt="" class="wp-image-1326" srcset="https://www.lemonary.cn/wp-content/uploads/2025/01/image-9.png 198w, https://www.lemonary.cn/wp-content/uploads/2025/01/image-9-137x300.png 137w" sizes="(max-width: 198px) 100vw, 198px" /></figure>



<figure class="wp-block-image size-large"><img decoding="async" width="1024" height="440" src="https://www.lemonary.cn/wp-content/uploads/2025/01/image-11-1024x440.png" alt="" class="wp-image-1328" srcset="https://www.lemonary.cn/wp-content/uploads/2025/01/image-11-1024x440.png 1024w, https://www.lemonary.cn/wp-content/uploads/2025/01/image-11-300x129.png 300w, https://www.lemonary.cn/wp-content/uploads/2025/01/image-11-768x330.png 768w, https://www.lemonary.cn/wp-content/uploads/2025/01/image-11.png 1506w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<ol class="wp-block-list">
<li>添加映射</li>



<li>配置：源数据类型名-VARCHAR，目的数据类型名-VARCHAR，强制为字符存储选择-是</li>



<li>保存</li>
</ol>



<figure class="wp-block-image size-large"><img decoding="async" width="1024" height="440" src="https://www.lemonary.cn/wp-content/uploads/2025/01/image-12-1024x440.png" alt="" class="wp-image-1329" srcset="https://www.lemonary.cn/wp-content/uploads/2025/01/image-12-1024x440.png 1024w, https://www.lemonary.cn/wp-content/uploads/2025/01/image-12-300x129.png 300w, https://www.lemonary.cn/wp-content/uploads/2025/01/image-12-768x330.png 768w, https://www.lemonary.cn/wp-content/uploads/2025/01/image-12.png 1504w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<h3 class="wp-block-heading">2.2 迁移过程</h3>



<p class="wp-block-paragraph">源端数据库</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="726" height="580" src="https://www.lemonary.cn/wp-content/uploads/2025/01/image-14.png" alt="" class="wp-image-1338" srcset="https://www.lemonary.cn/wp-content/uploads/2025/01/image-14.png 726w, https://www.lemonary.cn/wp-content/uploads/2025/01/image-14-300x240.png 300w" sizes="auto, (max-width: 726px) 100vw, 726px" /></figure>



<p class="wp-block-paragraph">目的端数据库</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="726" height="580" src="https://www.lemonary.cn/wp-content/uploads/2025/01/image-15.png" alt="" class="wp-image-1339" srcset="https://www.lemonary.cn/wp-content/uploads/2025/01/image-15.png 726w, https://www.lemonary.cn/wp-content/uploads/2025/01/image-15-300x240.png 300w" sizes="auto, (max-width: 726px) 100vw, 726px" /></figure>



<p class="wp-block-paragraph">迁移选项※</p>



<p class="wp-block-paragraph">取消单选框，不使用默认数据类型映射关系，便能调用到我们所配置的数据类型映射。</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="966" height="673" src="https://www.lemonary.cn/wp-content/uploads/2025/01/image-16.png" alt="" class="wp-image-1340" srcset="https://www.lemonary.cn/wp-content/uploads/2025/01/image-16.png 966w, https://www.lemonary.cn/wp-content/uploads/2025/01/image-16-300x209.png 300w, https://www.lemonary.cn/wp-content/uploads/2025/01/image-16-768x535.png 768w" sizes="auto, (max-width: 966px) 100vw, 966px" /></figure>



<p class="wp-block-paragraph">指定模式</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="997" height="726" src="https://www.lemonary.cn/wp-content/uploads/2025/01/image-17.png" alt="" class="wp-image-1341" srcset="https://www.lemonary.cn/wp-content/uploads/2025/01/image-17.png 997w, https://www.lemonary.cn/wp-content/uploads/2025/01/image-17-300x218.png 300w, https://www.lemonary.cn/wp-content/uploads/2025/01/image-17-768x559.png 768w" sizes="auto, (max-width: 997px) 100vw, 997px" /></figure>



<p class="wp-block-paragraph">指定对象</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1024" height="640" src="https://www.lemonary.cn/wp-content/uploads/2025/01/image-18-1024x640.png" alt="" class="wp-image-1342" srcset="https://www.lemonary.cn/wp-content/uploads/2025/01/image-18-1024x640.png 1024w, https://www.lemonary.cn/wp-content/uploads/2025/01/image-18-300x187.png 300w, https://www.lemonary.cn/wp-content/uploads/2025/01/image-18-768x480.png 768w, https://www.lemonary.cn/wp-content/uploads/2025/01/image-18-1536x960.png 1536w, https://www.lemonary.cn/wp-content/uploads/2025/01/image-18.png 1930w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure>



<p class="wp-block-paragraph">审阅迁移任务</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1024" height="640" src="https://www.lemonary.cn/wp-content/uploads/2025/01/image-19-1024x640.png" alt="" class="wp-image-1343" srcset="https://www.lemonary.cn/wp-content/uploads/2025/01/image-19-1024x640.png 1024w, https://www.lemonary.cn/wp-content/uploads/2025/01/image-19-300x187.png 300w, https://www.lemonary.cn/wp-content/uploads/2025/01/image-19-768x480.png 768w, https://www.lemonary.cn/wp-content/uploads/2025/01/image-19-1536x960.png 1536w, https://www.lemonary.cn/wp-content/uploads/2025/01/image-19.png 1930w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure>



<p class="wp-block-paragraph">点击“完成”，进行迁移，即可完成数据类型的映射。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.lemonary.cn/mysql%e6%95%b0%e6%8d%ae%e5%ba%93%e8%bf%81%e7%a7%bb%e5%88%b0%e8%be%be%e6%a2%a6%e6%95%b0%e6%8d%ae%e5%ba%93%e8%b6%85%e5%87%ba%e5%ae%9a%e4%b9%89%e9%95%bf%e5%ba%a6%e9%97%ae%e9%a2%98/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<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 class="wp-block-paragraph">对于一个Linux上新部署的MySQL数据库，往往在连接的时候有可能会碰到连接不上的问题。我就是在近日，写一篇MySQL数据库迁移到DM数据库的博客时，出奇的发现我博客所用的MySQL数据库居然只能从Linux本地连接。由于对MySQL不熟悉，这个问题困扰了我两三个小时。下面本文就来总结一下，MySQL连接不上可能涉及的一些配置。</p>



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



<p class="wp-block-paragraph">（1）MySQL的配置文件my.cnf</p>



<p class="wp-block-paragraph">如果你找不到你的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 class="wp-block-paragraph">可以看到有多个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 class="wp-block-paragraph">注意：0.0.0.0 表示任何人都可以连接，如果没有特殊要求，为了安全起见，可以配置成具体的IP。</p>
</blockquote>



<p class="wp-block-paragraph">重启数据库生效</p>



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



<p class="wp-block-paragraph">（2）配置数据库的用户访问权限</p>



<p class="wp-block-paragraph">新部署的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 class="wp-block-paragraph">（3）防火墙firewalld和iptables</p>



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



<p class="wp-block-paragraph">关闭firewalld</p>



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



<p class="wp-block-paragraph">我不清楚为什么我的服务器有两个防火墙，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 class="wp-block-paragraph">所以我把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 class="wp-block-paragraph">重启iptables服务</p>



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



<p class="wp-block-paragraph">（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 class="wp-block-paragraph">登录成功。</p>



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



<p class="wp-block-paragraph">万事要认真仔细。</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>
