Mysql:Symbolic Links:软链接(符号链接)
You can move databases or tables from the database directory to other locations and replace them with symbolic links to the new locations. You might want to do this, for example, to move a database to a file system with more free space or increase the speed of your system by spreading your tables to different disks.
For InnoDB
tables, use the DATA DIRECTORY
clause of the CREATE TABLE
statement instead of symbolic links, as explained in Section 14.6.1.2, “Creating Tables Externally”. This new feature is a supported, cross-platform technique.
The recommended way to do this is to symlink entire database directories to a different disk. Symlink MyISAM
tables only as a last resort.
To determine the location of your data directory, use this statement:
SHOW VARIABLES LIKE 'datadir';
On Unix, the way to symlink a database is first to create a directory on some disk where you have free space and then to create a soft link to it from the MySQL data directory.
mkdir /dr1/databases/test ln -s /dr1/databases/test /path/to/datadir /path/to/datadir
MySQL does not support linking one directory to multiple databases. Replacing a database directory with a symbolic link works as long as you do not make a symbolic link between databases. Suppose that you have a database db1
under the MySQL data directory, and then make a symlink db2
that points to db1
:
cd /path/to/datadir /path/to/datadir ln -s db1 db2
The result is that, for any table tbl_a
in db1
, there also appears to be a table tbl_a
in db2
. If one client updates db1.tbl_a
and another client updates db2.tbl_a
, problems are likely to occur.
Symlinks are fully supported only for MyISAM
tables. For files used by tables for other storage engines, you may get strange problems if you try to use symbolic links. For InnoDB
tables, use the alternative technique explained in Section 14.6.1.2, “Creating Tables Externally” instead.
Do not symlink tables on systems that do not have a fully operational realpath()
call. (Linux and Solaris support realpath()
). To determine whether your system supports symbolic links, check the value of the have_symlink
system variable using this statement:
SHOW VARIABLES LIKE 'have_symlink';
The handling of symbolic links for MyISAM
tables works as follows:
-
In the data directory, you always have the table format (
.frm
) file, the data (.MYD
) file, and the index (.MYI
) file. The data file and index file can be moved elsewhere and replaced in the data directory by symlinks. The format file cannot. -
You can symlink the data file and the index file independently to different directories.
-
To instruct a running MySQL server to perform the symlinking, use the
DATA DIRECTORY
andINDEX DIRECTORY
options toCREATE TABLE
. See Section 13.1.18, “CREATE TABLE Statement”. Alternatively, if mysqld is not running, symlinking can be accomplished manually using ln -s from the command line.NoteThe path used with either or both of the
DATA DIRECTORY
andINDEX DIRECTORY
options may not include the MySQLdata
directory. (Bug #32167) -
myisamchk does not replace a symlink with the data file or index file. It works directly on the file to which the symlink points. Any temporary files are created in the directory where the data file or index file is located. The same is true for the
ALTER TABLE
,OPTIMIZE TABLE
, andREPAIR TABLE
statements. -
Note
When you drop a table that is using symlinks, both the symlink and the file to which the symlink points are dropped. This is an extremely good reason not to run mysqld as the
root
operating system user or permit operating system users to have write access to MySQL database directories. -
If you rename a table with
ALTER TABLE ... RENAME
orRENAME TABLE
and you do not move the table to another database, the symlinks in the database directory are renamed to the new names and the data file and index file are renamed accordingly. -
If you use
ALTER TABLE ... RENAME
orRENAME TABLE
to move a table to another database, the table is moved to the other database directory. If the table name changed, the symlinks in the new database directory are renamed to the new names and the data file and index file are renamed accordingly. -
If you are not using symlinks, start mysqld with the
--skip-symbolic-links
option to ensure that no one can use mysqld to drop or rename a file outside of the data directory.
These table symlink operations are not supported:
-
ALTER TABLE
ignores theDATA DIRECTORY
andINDEX DIRECTORY
table options. -
As indicated previously, only the data and index files can be symbolic links. The
.frm
file must never be a symbolic link. Attempting to do this (for example, to make one table name a synonym for another) produces incorrect results. Suppose that you have a databasedb1
under the MySQL data directory, a tabletbl1
in this database, and in thedb1
directory you make a symlinktbl2
that points totbl1
:cd /path/to/datadir/db1 /path/to/datadir ln -s tbl1.frm tbl2.frm ln -s tbl1.MYD tbl2.MYD ln -s tbl1.MYI tbl2.MYI Problems result if one thread reads
db1.tbl1
and another thread updatesdb1.tbl2
:-
The query cache is “fooled” (it has no way of knowing that
tbl1
has not been updated, so it returns outdated results). -
ALTER
statements ontbl2
fail.
-
On Windows, symbolic links can be used for database directories. This enables you to put a database directory at a different location (for example, on a different disk) by setting up a symbolic link to it. Use of database symlinks on Windows is similar to their use on Unix, although the procedure for setting up the link differs.
Suppose that you want to place the database directory for a database named mydb
at D:\data\mydb
. To do this, create a symbolic link in the MySQL data directory that points to D:\data\mydb
. However, before creating the symbolic link, make sure that the D:\data\mydb
directory exists by creating it if necessary. If you already have a database directory named mydb
in the data directory, move it to D:\data
. Otherwise, the symbolic link will be ineffective. To avoid problems, make sure that the server is not running when you move the database directory.
On Windows, you can create a symlink using the mklink command. This command requires administrative privileges.
-
Change location into the data directory:
cd \path\to\datadir \path\to\datadir -
In the data directory, create a symlink named
mydb
that points to the location of the database directory:C:\>
mklink /d mydb D:\data\mydb
After this, all tables created in the database mydb
are created in D:\data\mydb
.
-
YES
if symbolic link support is enabled,NO
if not. This is required on Unix for support of theDATA DIRECTORY
andINDEX DIRECTORY
table options. If the server is started with the--skip-symbolic-links
option, the value isDISABLED
.This variable has no meaning on Windows.
-
--symbolic-links
,--skip-symbolic-links
Property Value Command-Line Format --symbolic-links[={OFF|ON}]
Type Boolean Default Value ON
Enable or disable symbolic link support. On Unix, enabling symbolic links means that you can link a
MyISAM
index file or data file to another directory with theINDEX DIRECTORY
orDATA DIRECTORY
option of theCREATE TABLE
statement. If you delete or rename the table, the files that its symbolic links point to also are deleted or renamed. See Section 8.12.3.2, “Using Symbolic Links for MyISAM Tables on Unix”.This option has no meaning on Windows.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)