修改mysql資料儲存路徑 踩坑紀實

2021-09-26 05:22:43 字數 3629 閱讀 1471

安裝好mysql資料庫以後,會有乙個指定的資料儲存位置,這個是在安裝過程中通過定義my.cnf 檔案確定的。my.cnf裡面有句話,這個就是定義了資料的位置了。

datadir=/var/lib/mysql
開啟路徑中指定的資料夾,可以看到很多檔案,這裡就是存放資料的位置了

[root@localhost etc]

# cd /var/lib/mysql

[root@localhost db]

# ls

auto.cnf binlog.000003 binlog.index client-cert.pem ibdata1 ibtmp1 mysql mysql.sock.lock public_key.pem

binlog.000001 binlog.000004 ca-key.pem client-key.pem ib_logfile0 #innodb_temp mysql.ibd performance_schema server-cert.pem

binlog.000002 binlog.000005 ca.pem ib_buffer_pool ib_logfile1 localhost-slow.log mysql.sock private_key.pem server-key.pem

[root@localhost db]

#

一定要關閉mysql才能修改配置檔案,所以一定要關。

systemctl stop mysqld.service
開啟配置檔案my.cnf

vi /etc/my.cnf
修改一句話,將當前路徑

datadir=/var/lib/mysql
改為目標位置

datadir=/service/db
mkdir -p /service/db

chown mysql:mysql /service/db

chmod go+x /service/db

初始化的作用就是將資料檔案重新在新的路徑下生成,初始化前一定要清空目標路徑下的檔案,避免檔案衝突引起報錯。

mysqld --initialize --datadir=/service/db --user=mysql
如果mysql啟動成功,整個步驟也就完成了。

systemctl start msyqld.service
我錯誤以為這沒什麼,mysql啟動時總是報錯。。。

直接刪除,順便將當前路徑下的檔案也刪除了

rm -rf /service/db

rm -rf /var/lib/mysql

這個問題困擾了我很久很久。。。

不斷提示這個報錯

[root@localhost etc]

# systemctl status mysqld

● mysqld.service - mysql server

loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)

active: failed (result: exit-code) since 四 2019-08-15 16:18:04 cst; 9s ago

docs: man:mysqld(8)

process: 22032 execstart=/usr/sbin/mysqld $mysqld_opts

(code=exited, status=1/failure)

process: 22015 execstartpre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/success)

main pid: 22032 (code=exited, status=1/failure)

status: "server_booting"

error: 13 (許可權不夠)

8月 15 16:18:04 localhost.localdomain systemd[1]: starting mysql server...

8月 15 16:18:04 localhost.localdomain systemd[1]: mysqld.service: main process exited, code=exited, status=1/failure

8月 15 16:18:04 localhost.localdomain systemd[1]: failed to start mysql server.

8月 15 16:18:04 localhost.localdomain systemd[1]: unit mysqld.service entered failed state.

8月 15 16:18:04 localhost.localdomain systemd[1]: mysqld.service failed.

(1)檢視selinux狀態

getenforce
disabled
(2)臨時關閉,二選一即可

setenforce 0 #設定selinux 成為permissive模式

setenforce 1 #設定selinux 成為enforcing模式

(3)永久關閉

vi /etc/sysconfig/selinux
將enforcing 改為disabled ,並重啟系統reboot。

# this file controls the state of selinux on the system.

# selinux= can take one of these three values:

# enforcing - selinux security policy is enforced.

# permissive - selinux prints warnings instead of enforcing.

# disabled - no selinux policy is loaded.

selinux=enforcing

# selinuxtype= can take one of three values:

# targeted - targeted processes are protected,

# minimum - modification of targeted policy. only selected processes are protected.

# mls - multi level security protection.

selinuxtype=targeted

設定完以後,將mysql資料清空,並初始化mysql。

MySQL修改資料儲存路徑

簡易方法 檢視當前目錄 連線到mysql 輸入查詢目錄命令 show variables like datadir 結果類似 variable name value datadir d mysql data 修改目錄 停止mysql服務 把 d mysql data 中的data目錄內容,複製到想要...

mysql踩坑記錄

好記性不如爛筆頭 之前在電腦上裝過mysql,但最近想在學一下發現忘記了登陸密碼,索性解除安裝了mysql重新安裝了。首先檢視mysql的依賴項 dpkg list grep mysql 然後解除安裝 sudo apt get remove mysql common sudo apt get aut...

mysql踩坑記錄

1 今天在linux上面裝完mysql,卻發現在本地登入可以,但是遠端登入卻報錯host is not allowed to connect to this mysql server,找了半天試了網上的一些方法都沒有解決,最終在一篇文章裡找到了解決方法,特意記錄一下。先說說這個錯誤,其實就是我們的m...