<h1>一、系統(tǒng)環(huán)境</h1><p>操作系統(tǒng):Centos 7
已安裝環(huán)境:MySQL 8.0.26</p><h1>二、開始操作</h1><h2>2.1 關(guān)閉SELinux</h2><p>為了提高 Linux 系統(tǒng)的安全性,在 Linux 上通常會使用 SELinux 或 AppArmor 實現(xiàn)強制訪問控制(Mandatory Access Control MAC)。對于 MySQL 數(shù)據(jù)庫的強制訪問控制策略通常是激活的,如果用戶采用默認的配置,并不會感到強制訪問控制策略對 MySQL 數(shù)據(jù)庫的影響,一旦用戶修改了 MySQL 數(shù)據(jù)庫的默認配置,例如默認的數(shù)據(jù)目錄或監(jiān)聽端口,MySQL 數(shù)據(jù)庫的活動就會被 SELinux 或 AppArmor 阻止,導(dǎo)致數(shù)據(jù)庫無法啟動。</p><p>因此,為了此次操作順利執(zhí)行,我們需要把SELinux 關(guān)了。</p><p>setenforce 0
</p><div class="image-package"><img src="https://upload-images.jianshu.io/upload_images/12348461-7a907f36dbf6d322.jpeg" img-data="{"format":"jpeg","size":16117,"height":175,"width":569}" class="uploaded-img" style="min-height:200px;min-width:200px;" width="auto" height="auto"/>
</div><h2>2.2 修改MySQL 配置文件</h2><p>修改配置文件前先進行備份,這是一個好習(xí)慣。</p><p>cp /etc/my.cnf /etc/my.cnf_old
vi /etc/my.cnf
</p><div class="image-package"><img src="https://upload-images.jianshu.io/upload_images/12348461-f1b30ffc27e4d4f1.jpeg" img-data="{"format":"jpeg","size":17793,"height":187,"width":660}" class="uploaded-img" style="min-height:200px;min-width:200px;" width="auto" height="auto"/>
</div><p>修改以下的配置信息:</p><p>datadir=/opt/mysql/mysql
socket=/opt/mysql/mysql/mysql.sock
log-error=/opt/mysql/log/mysqld.log
pid-file=/opt/mysql/run/mysqld/mysqld.pid
</p><h2>2.3 創(chuàng)建新的數(shù)據(jù)目錄</h2><p>根據(jù)上一步的配置文件,創(chuàng)建對應(yīng)的目錄:</p><p>mkdir /opt/mysql
cp -ar /var/lib/mysql/ /opt/mysql/
mkdir /opt/mysql/log/
touch /opt/mysql/log/mysqld.log
mkdir -p /opt/mysql/run/mysqld/
</p><p>并修改其權(quán)限:</p><p>chown -R mysql:mysql /opt/mysql/
chmod 777 /opt/mysql/
</p><h2>2.4 驗證</h2><p>重啟 MySQL 數(shù)據(jù)庫:</p><p>service mysqld stop
service mysqld start
</p><p>往數(shù)據(jù)庫添加數(shù)據(jù),查看是否切換到了新數(shù)據(jù)目錄:</p><div class="image-package"><img src="https://upload-images.jianshu.io/upload_images/12348461-4c0bd6ad448e40e9.jpeg" img-data="{"format":"jpeg","size":43028,"height":370,"width":830}" class="uploaded-img" style="min-height:200px;min-width:200px;" width="auto" height="auto"/>
</div><p>或者在SQL 執(zhí)行界面執(zhí)行以下語句:</p><p>show VARIABLES like 'datadir'
</p><div class="image-package"><img src="https://upload-images.jianshu.io/upload_images/12348461-ab8e6443e7deceab.jpeg" img-data="{"format":"jpeg","size":19973,"height":321,"width":830}" class="uploaded-img" style="min-height:200px;min-width:200px;" width="auto" height="auto"/>
</div><h1>三、小結(jié)</h1><p>MySQL 遷移數(shù)據(jù)目錄其實很簡單,只需要有對應(yīng)的目錄路徑,并修改對應(yīng)的配置信息即可。
這是在Linux 系統(tǒng)里的操作,Windows 系統(tǒng)大體也是一樣的。Windows 系統(tǒng)的簡便性遠超Linux 系統(tǒng),甚至在Windows 系統(tǒng)你還可以直接復(fù)制某一個數(shù)據(jù)庫文件到數(shù)據(jù)目錄中(相當(dāng)于復(fù)制了一個數(shù)據(jù)庫到指定的數(shù)據(jù)庫系統(tǒng)中),這也是能生效的。</p><p>我是陳冰安,在知識宇宙中摸爬滾打,分享個人所得,也期待志同道合。</p>