MySQL更改資料庫資料存儲目錄
2015-03-27 15:24
潇湘隐者
閱讀(48175)
評論(1)
編輯
收藏
舉報
MySQL資料庫預設的資料庫檔案位于/var/lib/mysql下,有時候由于存儲規劃等原因,需要更改MySQL資料庫的資料存儲目錄。下文總結整理了實踐過程的操作步驟。
1:确認MySQL資料庫存儲目錄[root@DB-Server tmp]# mysqladmin -u root -p variables | grep datadir
Enter password:
| datadir | /var/lib/mysql/
2:關閉MySQL服務 在更改MySQL的資料目錄前,必須關閉MySQL服務。
方式 1 :[root@DB-Server ~]# service mysql status
MySQL running (9411)[ OK ]
[root@DB-Server ~]# service mysql stop
Shutting down MySQL..[ OK ]
[root@DB-Server ~]#

[root@DB-Server ~]# /etc/rc.d/init.d/mysql status
MySQL running (8900)[ OK ]
[root@DB-Server ~]# /etc/rc.d/init.d/mysql stop
Shutting down MySQL..[ OK ]
[root@DB-Server ~]#
3:建立新的資料庫存儲目錄 [root@DB-Server ~]# cd /u01
[root@DB-Server u01]# mkdir mysqldata
4:移動MySQL資料目錄到新位置[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中。指令如下:
[root@DB-Server mysql]# cp /usr/share/mysql/my-medium.cnf /etc/my.cnf
編輯/etc/my.cnf檔案,修改參數socket
MySQL 5.5 版本# The following options will be passed to all MySQL clients
[client]
#password = your_password
port = 3306
socket = /u01/mysqldata/mysql/mysql.sock
# Here follows entries for some specific programs
# The MySQL server
[mysqld]
port = 3306
socket = /u01/mysqldata/mysql/mysql.sock
skip-external-locking
key_buffer_size = 16M
max_allowed_packet = 1M
table_open_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
将參數datadir修改為datadir=/u01/mysqldata/mysql/
[root@DB-Server ~]# service mysql start
Starting MySQL..[ OK ]
[root@DB-Server ~]# mysqladmin -u root -p variables | grep datadir
Enter 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或修改檔案安全上下文。
[root@DB-Server mysql]# /etc/init.d/mysql start
Starting 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 start
Starting MySQL.[ OK ]
[root@DB-Server mysql]#
參考資料:
http://database.ctocio.com.cn/tips/449/7566949.shtml
www.linuxidc.com/Linux/2012-12/75647.htm
http://blog.csdn.net/hellyhe/article/details/8309470
- 分類 資料庫技術(My SQL)
- 标簽 mysql修改資料庫資料目錄