Home Database Mysql Tutorial Linux下Mysql更改数据存储路径

Linux下Mysql更改数据存储路径

Jun 07, 2016 pm 03:47 PM
linux mysql storage data Change path

MYSQL默认的数据文件存储目录为/var/lib/mysql。假如要把目录移到/home/data下需要进行下面几步: 1、home目录下建立data目录 cd /homemkdir data 2、把MySQL服务进程停掉: mysqladmin -u root -p shutdown 3、把/var/lib/mysql整个目录移到/home/data mv /

MYSQL默认的数据文件存储目录为/var/lib/mysql。假如要把目录移到/home/data下需要进行下面几步:

1、home目录下建立data目录

cd /homemkdir data
2、把MySQL服务进程停掉:

mysqladmin -u root -p shutdown
3、把/var/lib/mysql整个目录移到/home/data

mv /var/lib/mysql /home/data/
这样就把MySQL的数据文件移动到了/home/data/mysql下

4、找到my.cnf配置文件

如果/etc/目录下没有my.cnf配置文件,请到/usr/share/mysql/下找到*.cnf文件,拷贝其中一个到/etc/并改名为my.cnf)中。命令如下:

[root@test1 mysql]# cp /usr/share/mysql/my-medium.cnf /etc/my.cnf
5、编辑MySQL的配置文件/etc/my.cnf

为保证MySQL能够正常工作,需要指明mysql.sock文件的产生位置。修改socket=/var/lib/mysql/mysql.sock一行中等号右边的值为:/home/mysql/mysql.sock 。操作如下:

vi  my.cnf  (用vi工具编辑my.cnf文件,找到下列数据修改之)# The MySQL server[mysqld]  port = 3306#socket  = /var/lib/mysql/mysql.sock(原内容,为了更稳妥用“#”注释此行)socket  = /home/data/mysql/mysql.sock (加上此行)
6、修改MySQL启动脚本/etc/init.d/mysql

最后,需要修改MySQL启动脚本/etc/init.d/mysqld,把其中datadir=/var/lib/mysql一行中,等号右边的路径改成你现在的实际存放路径:home/data/mysql。

[root@test1 etc]# vi /etc/init.d/mysql#datadir=/var/lib/mysql(注释此行)datadir=/home/data/mysql (加上此行)

如果是CentOS还要改 /usr/bin/mysqld_safe 相关文件位置;

最后 做一个mysql.sock 链接:
in -s /home/data/mysql/mysql.sock /var/lib/mysql/mysql.sock

7、重新启动MySQL服务
/etc/init.d/mysqld start

或用reboot命令重启Linux

如果工作正常移动就成功了,否则对照前面的7步再检查一下。还要注意目录的属主和权限。
复制内容到剪贴板
代码:

[root@sample ~]# chown -R mysql:mysql /home/data/mysql/  ← 改变数据库的归属为mysql

[root@sample ~]# chmod 700 /home/data/mysql/test/  ← 改变数据库目录属性为700

[root@sample ~]# chmod 660 /home/data/mysql/test/*  ← 改变数据库中数据的属性为660


错误解决方案:

1.在CentOS上,如果mysql是通过yum安装的,那么可能使用上面的方法不能完全凑效。

原因:mysql的配置文件有多处,除了更改/etc/my.cnf文件之外,还需要更改/usr/lib64/mysql/mysql_config

这个文件里面有一行“ldata='/var/lib/mysql'”和"socket=/var/lib/mysql/mysql.sock",这里也需要改掉


2.另外,还有权限问题,查看/var/log/mysqld.log,发现Can't create test file /xxx/mysql/centos5.lower-test

这里是因为没有权限创建或读取文件。

解决办法就是使用setenforce 0 这个命令,让系统关闭权限校验,然后再运行命令 service mysqld start 发现OK了。

另外一个办法是restorecon -FRv /home/data/mysql,或者chcon -R -t mysqld_db_t /home/data/mysql

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What computer configuration is required for vscode What computer configuration is required for vscode Apr 15, 2025 pm 09:48 PM

VS Code system requirements: Operating system: Windows 10 and above, macOS 10.12 and above, Linux distribution processor: minimum 1.6 GHz, recommended 2.0 GHz and above memory: minimum 512 MB, recommended 4 GB and above storage space: minimum 250 MB, recommended 1 GB and above other requirements: stable network connection, Xorg/Wayland (Linux)

How to switch Chinese mode with vscode How to switch Chinese mode with vscode Apr 15, 2025 pm 11:39 PM

VS Code To switch Chinese mode: Open the settings interface (Windows/Linux: Ctrl, macOS: Cmd,) Search for "Editor: Language" settings Select "Chinese" in the drop-down menu Save settings and restart VS Code

vscode Previous Next Shortcut Key vscode Previous Next Shortcut Key Apr 15, 2025 pm 10:51 PM

VS Code One-step/Next step shortcut key usage: One-step (backward): Windows/Linux: Ctrl ←; macOS: Cmd ←Next step (forward): Windows/Linux: Ctrl →; macOS: Cmd →

What is the main purpose of Linux? What is the main purpose of Linux? Apr 16, 2025 am 12:19 AM

The main uses of Linux include: 1. Server operating system, 2. Embedded system, 3. Desktop operating system, 4. Development and testing environment. Linux excels in these areas, providing stability, security and efficient development tools.

How to use VSCode How to use VSCode Apr 15, 2025 pm 11:21 PM

Visual Studio Code (VSCode) is a cross-platform, open source and free code editor developed by Microsoft. It is known for its lightweight, scalability and support for a wide range of programming languages. To install VSCode, please visit the official website to download and run the installer. When using VSCode, you can create new projects, edit code, debug code, navigate projects, expand VSCode, and manage settings. VSCode is available for Windows, macOS, and Linux, supports multiple programming languages ​​and provides various extensions through Marketplace. Its advantages include lightweight, scalability, extensive language support, rich features and version

vscode setting Chinese tutorial vscode setting Chinese tutorial Apr 15, 2025 pm 11:45 PM

VS Code supports Chinese settings, which can be completed by following the steps: Open the settings panel and search for "locale". Set "locale.language" to "zh-CN" (Simplified Chinese) or "zh-TW" (Traditional Chinese). Save settings and restart VS Code. The settings menu, toolbar, code prompts, and documents will be displayed in Chinese. Other language settings can also be customized, such as file tag format, entry description, and diagnostic process language.

How to execute code with vscode How to execute code with vscode Apr 15, 2025 pm 09:51 PM

Executing code in VS Code only takes six steps: 1. Open the project; 2. Create and write the code file; 3. Open the terminal; 4. Navigate to the project directory; 5. Execute the code with the appropriate commands; 6. View the output.

vscode terminal usage tutorial vscode terminal usage tutorial Apr 15, 2025 pm 10:09 PM

vscode built-in terminal is a development tool that allows running commands and scripts within the editor to simplify the development process. How to use vscode terminal: Open the terminal with the shortcut key (Ctrl/Cmd). Enter a command or run the script. Use hotkeys (such as Ctrl L to clear the terminal). Change the working directory (such as the cd command). Advanced features include debug mode, automatic code snippet completion, and interactive command history.

See all articles