溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

MySQL中怎么更改數(shù)據(jù)庫數(shù)據(jù)存儲目錄

發(fā)布時間:2021-07-26 15:31:20 來源:億速云 閱讀:527 作者:Leah 欄目:數(shù)據(jù)庫

MySQL中怎么更改數(shù)據(jù)庫數(shù)據(jù)存儲目錄,針對這個問題,這篇文章詳細介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。

1:確認MySQL數(shù)據(jù)庫存儲目錄

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

2:關(guān)閉MySQL服務(wù)

在更改MySQL的數(shù)據(jù)目錄前,必須關(guān)閉MySQL服務(wù)。

方式1:

[root@DB-Server ~]# service mysql status  MySQL running (9411)[ OK ]  [root@DB-Server ~]# service mysql stop  Shutting down MySQL..[ OK ]  [root@DB-Server ~]#

方式2:

[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:創(chuàng)建新的數(shù)據(jù)庫存儲目錄

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

4:移動MySQL數(shù)據(jù)目錄到新位置

[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中。命令如下:

MySQL中怎么更改數(shù)據(jù)庫數(shù)據(jù)存儲目錄

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

編輯/etc/my.cnf文件,修改參數(shù)socket

MySQL中怎么更改數(shù)據(jù)庫數(shù)據(jù)存儲目錄

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

MySQL中怎么更改數(shù)據(jù)庫數(shù)據(jù)存儲目錄

6:修改啟動腳本/etc/init.d/mysql

將參數(shù)datadir修改為datadir=/u01/mysqldata/mysql/

MySQL中怎么更改數(shù)據(jù)庫數(shù)據(jù)存儲目錄

7:啟動MySQL服務(wù)并驗證MySQL數(shù)據(jù)庫路徑

[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: 在修改數(shù)據(jù)庫的存儲目錄前,/var/lib/mysql/目錄下根本沒有mysql.sock文件,安裝上面配置后,就會生成mysql.sock文件。

關(guān)于mysql.sock文件,搜索了一下資料:mysql.sock是用于socket連接的文件。也就是只有你的守護進程啟動起來這個文件才存在。但是你的mysql程序(這個程序是客戶端,服務(wù)器端是mysqld)可以選擇是否使用mysql.sock文件來連接(因為這個方法只適合在Unix主機上面連接本地的mysqld),對于非本地的任何類型的主機。那么這個文件是否一定需要的呢? 這個需要進一步了解清楚。

2:我在網(wǎng)上看有些網(wǎng)友總結(jié)的修改MySQL數(shù)據(jù)路徑,有些需要給新建的目錄的權(quán)限做一些處理,而有些有不用對目錄權(quán)限進行授權(quán),我沒有處理,也沒有什么問題。到底要不要對新的數(shù)據(jù)庫目錄授權(quán)呢?

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

4:注意如果沒有禁用selinux, 修改MySQL的數(shù)據(jù)路徑后啟動MySQL服務(wù)會遇到一些錯誤。關(guān)于這個的解釋是后臺服務(wù)都需要有對相應(yīng)目錄的對應(yīng)權(quán)限,而 mysql 的默認路徑/var/lib/mysql 已經(jīng)添加了相應(yīng)的策略,修改路徑后由于沒有相應(yīng)的策略,導(dǎo)致后臺進程讀取文件被selinux阻止,從而出現(xiàn)權(quán)限錯誤。 所以要么關(guān)閉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]#

關(guān)于MySQL中怎么更改數(shù)據(jù)庫數(shù)據(jù)存儲目錄問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識。

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI