Linux下Mysql更改数据存储路径
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

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

Although Notepad cannot run Java code directly, it can be achieved by using other tools: using the command line compiler (javac) to generate a bytecode file (filename.class). Use the Java interpreter (java) to interpret bytecode, execute the code, and output the result.

I encountered a tricky problem when developing a small application: the need to quickly integrate a lightweight database operation library. After trying multiple libraries, I found that they either have too much functionality or are not very compatible. Eventually, I found minii/db, a simplified version based on Yii2 that solved my problem perfectly.

To view the Git repository address, perform the following steps: 1. Open the command line and navigate to the repository directory; 2. Run the "git remote -v" command; 3. View the repository name in the output and its corresponding address.

The five basic components of the Linux system are: 1. Kernel, 2. System library, 3. System utilities, 4. Graphical user interface, 5. Applications. The kernel manages hardware resources, the system library provides precompiled functions, system utilities are used for system management, the GUI provides visual interaction, and applications use these components to implement functions.

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

To install Laravel, follow these steps in sequence: Install Composer (for macOS/Linux and Windows) Install Laravel Installer Create a new project Start Service Access Application (URL: http://127.0.0.1:8000) Set up the database connection (if required)
