1. 程式人生 > >MySQL 更改資料庫資料儲存目錄

MySQL 更改資料庫資料儲存目錄

MySQL資料庫預設的資料庫檔案位於/var/lib/mysql下,有時候由於儲存規劃等原因,需要更改MySQL資料庫的資料儲存目錄。下文總結整理了實踐過程的操作步驟。

1:確認MySQL資料庫儲存目錄

123 [root@DB-Server tmp
]# mysqladmin -u root -p variables | grep datadirEnter password:|datadir|/var/lib/mysql/

2:關閉MySQL服務

在更改MySQL的資料目錄前,必須關閉MySQL服務。

方式1

123456789 [root@DB-Server~]# service mysql statusMySQL running(9411)[OK][root@DB-Server~]# service mysql stopShutting down MySQL..[OK][root@DB-Server~]#

clip_image001

方式2

123456789 [root@DB-Server~]# /etc/rc.d/init.d/mysql statusMySQL running(8900)[OK][root@DB-Server~]# /etc/rc.d/init.d/mysql stopShutting down MySQL..[OK][root@DB-Server~]#

3:建立新的資料庫儲存目錄

12 [root@DB-Server~]# cd /u01[root@DB-Server u01]# mkdir mysqldata

4:移動MySQL資料目錄到新位置

1 [root@DB-Server~]# mv /var/lib/mysql /u01/mysqldata/

5修改配置檔案my.cnf

並不是所有版本都包含有my.cnf這個配置檔案,在MySQL 5.5版本,我就找不到my.cnf這個配置檔案, 而有些MySQL版本該檔案位於/usr/my.cnf,如果/etc/目錄下沒有my.cnf配置檔案,請到/usr/share/mysql/下找到*.cnf檔案,拷貝其中一個到/etc/並改名為my.cnf中。命令如下:

clip_image003

1 [root@DB-Server mysql]# cp /usr/share/mysql/my-medium.cnf /etc/my.cnf

編輯/etc/my.cnf檔案,修改引數socket

                         MySQL 5.5 版本

clip_image004

123456789101112131415161718192021 # The following options will be passed to all MySQL clients[client]#password       = your_passwordport=3306socket=/u01/mysqldata/mysql/mysql.sock# Here follows entries for some specific programs# The MySQL server[mysqld]port=3306socket=/u01/mysqldata/mysql/mysql.sockskip-external-lockingkey_buffer_size=16Mmax_allowed_packet=1Mtable_open_cache=64sort_buffer_size=512Knet_buffer_length=8Kread_buffer_size=256Kread_rnd_buffer_size=512Kmyisam_sort_buffer_size=8M

clip_image005

6:修改啟動指令碼/etc/init.d/mysql

將引數datadir修改為datadir=/u01/mysqldata/mysql/

clip_image006

7:啟動MySQL服務並驗證MySQL資料庫路徑

12345 [root@DB-Server~]# service mysql startStarting MySQL..[OK][root@DB-Server~]# mysqladmin -u root -p variables | grep datadirEnter password:|datadir|/u01/mysqldata/mysql/

我的疑問:

1: 在修改資料庫的儲存目錄前,/var/lib/mysql/目錄下根本沒有mysql.sock檔案,安裝上面配置後,就會生成mysql.sock檔案。

關於mysql.sock檔案,搜尋了一下資料:mysql.sock是用於socket連線的檔案。也就是隻有你的守護程序啟動起來這個檔案才存在。但是你的mysql程式(這個程式是客戶端,伺服器端是mysqld)可以選擇是否使用mysql.sock檔案來連線(因為這個方法只適合在Unix主機上面連線本地的mysqld),對於非本地的任何型別的主機。那麼這個檔案是否一定需要的呢? 這個需要進一步瞭解清楚。

2:我在網上看有些網友總結的修改MySQL資料路徑,有些需要給新建的目錄的許可權做一些處理,而有些有不用對目錄許可權進行授權,我沒有處理,也沒有什麼問題。到底要不要對新的資料庫目錄授權呢?

3:我在MySQL_5.6.20這個版本測試時,不修改my.cnf,只修改啟動指令碼/etc/init.d/mysql,也完全沒有啥問題。也沒有myssql.sock檔案生成。

4: 注意如果沒有禁用selinux, 修改MySQL的資料路徑後啟動MySQL服務會遇到一些錯誤。關於這個的解釋是後臺服務都需要有對相應目錄的對應許可權,而 mysql 的預設路徑/var/lib/mysql 已經添加了相應的策略,修改路徑後由於沒有相應的策略,導致後臺程序讀取檔案被selinux阻止,從而出現許可權錯誤。 所以要麼關閉Selinux或修改檔案安全上下文。

12345678910111213 [root@DB-Server mysql]# /etc/init.d/mysql startStarting MySQL....The server quit without updating PID file(/u01/mysqldata/mysql//DB-Server.localdomain.pid).[FAILED][root@DB-Server mysql]# [root@DB-Server mysql]# chcon -R -t mysqld_db_t /u01/mysqldata/mysql/[root@DB-Server mysql]# /etc/init.d/mysql startStarting MySQL.[OK][root@DB-Server mysql]#

參考資料: