Mariadb在CentOS下修改數據位置(更改數據存儲目錄到/home)

         隨着業務的變更,需要在新的一臺CentOS下部署MariaDB數據庫,並把之前的數據庫文件遷移到新數據庫中。之前想來應該不會太複雜,結果沒想到也折騰了一天多。記錄下希望今後能避免類似問題,再次遇到此類問題時也不用到處搜索。

        在新的CentOS服務器上安裝部署MariaDB,安裝過很多次了,所以這塊就不多累述。新版本的CentOS都有自帶的MariaDB,所以得先把自帶的卸載了再裝自己想安裝的版本。接下來就是修改數據目錄的操作,這塊就得詳細記錄下。

        第一步:停止MariaDB服務,並修改數據目錄

        1、執行命令,停止MariaDB服務

    systemctl stop mariadb

       2、在 /home 下創建新的數據文件夾

     mkdir mysql

       3、修改my.cnf (新增紅色部分)

     [mysqld]
     datadir=/home/mysql
     socket=/home/mysql/mysql.sock
     skip-name-resolve

        4、修改 server.cnf (紅色部分)

      [mysqld]

      datadir=/home/mysql

         5、重新執行安裝命令:mysql_install_db --defaults-file=/etc/my.cnf --datadir=/home/mysql/ --user=mysql

[root@localhost home]# mysql_install_db --defaults-file=/etc/my.cnf --datadir=/home/mysql/ --user=mysql
Installing MariaDB/MySQL system tables in '/home/mysql/' ...
OK

To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system


PLEASE REMEMBER TO SET A PASSWORD FOR THE MariaDB root USER !
To do so, start the server, then issue the following commands:

'/usr/bin/mysqladmin' -u root password 'new-password'
'/usr/bin/mysqladmin' -u root -h localhost.localdomain password 'new-password'

Alternatively you can run:
'/usr/bin/mysql_secure_installation'

which will also give you the option of removing the test
databases and anonymous user created by default.  This is
strongly recommended for production servers.

See the MariaDB Knowledgebase at http://mariadb.com/kb or the
MySQL manual for more instructions.

You can start the MariaDB daemon with:
cd '/usr' ; /usr/bin/mysqld_safe --datadir='/home/mysql/'

You can test the MariaDB daemon with mysql-test-run.pl
cd '/usr/mysql-test' ; perl mysql-test-run.pl

Please report any problems at http://mariadb.org/jira

The latest information about MariaDB is available at http://mariadb.org/.
You can find additional information about the MySQL part at:
http://dev.mysql.com
Consider joining MariaDB's strong and vibrant community:
https://mariadb.org/get-involved/

        6、重啓數據庫服務

[root@localhost mysql]# systemctl start mariadb
Job for mariadb.service failed because the control process exited with error code. See "systemctl status mariadb.service" and "journalctl -xe" for details.

       經覈查日誌文件,提示沒有權限問題導致的:

        第二步:解決權限不足的問題

         1、根據提示信息,顯示數據庫服務不能操作新建目錄

         1.1、覈查之前目錄所用的權限

 

 

 

 

 

 

         1.2、經覈實是用的mysql用戶組及用戶,所以新建的文件夾也應該要加此用戶組

[root@localhost home]# chown -R mysql:mysql /home/mysql
[root@localhost home]# chomd 775 /home/mysql
bash: chomd: 未找到命令...
相似命令是: 'chmod'
[root@localhost home]# chmod 775 /home/mysql
[root@localhost home]# ll
總用量 4
drwxrwxr-x. 16 mysql mysql 4096 7月  10 17:53 admin
drwxrwxr-x.  9 mysql mysql  277 7月  14 10:31 data
drwxrwxr-x   2 mysql mysql  130 7月  14 10:41 mysql

         2、再次重啓數據庫服務,結果還是失敗,還是提示沒有權限

Jul 14 10:32:00 localhost systemd: mariadb.service failed.
Jul 14 10:35:13 localhost systemd: Starting MariaDB 10.2.32 database server...
Jul 14 10:35:13 localhost mysqld: 2020-07-14 10:35:13 140669217863872 [Note] /usr/sbin/mysqld (mysqld 10.2.32-MariaDB) starting as process 16051 ...
Jul 14 10:35:13 localhost mysqld: 2020-07-14 10:35:13 140669217863872 [Warning] Could not increase number of max_open_files to more than 16364 (request: 32222)
Jul 14 10:35:13 localhost mysqld: 2020-07-14 10:35:13 140669217863872 [Warning] Can't create test file /home/data/localhost.lower-test
Jul 14 10:35:13 localhost mysqld: #007/usr/sbin/mysqld: Can't change dir to '/home/data/' (Errcode: 13 "Permission denied")
Jul 14 10:35:13 localhost mysqld: 2020-07-14 10:35:13 140669217863872 [ERROR] Aborting
Jul 14 10:35:14 localhost systemd: mariadb.service: main process exited, code=exited, status=1/FAILURE
Jul 14 10:35:14 localhost systemd: Failed to start MariaDB 10.2.32 database server.
Jul 14 10:35:14 localhost systemd: Unit mariadb.service entered failed state.
Jul 14 10:35:14 localhost systemd: mariadb.service failed.
Jul 14 10:40:01 localhost systemd: Started Session 124 of user root.
Jul 14 10:45:21 localhost systemd: Starting MariaDB 10.2.32 database server...
Jul 14 10:45:21 localhost mysqld: 2020-07-14 10:45:21 140469303916736 [Note] /usr/sbin/mysqld (mysqld 10.2.32-MariaDB) starting as process 16815 ...
Jul 14 10:45:21 localhost mysqld: 2020-07-14 10:45:21 140469303916736 [Warning] Could not increase number of max_open_files to more than 16364 (request: 32222)
Jul 14 10:45:21 localhost mysqld: 2020-07-14 10:45:21 140469303916736 [Warning] Can't create test file /home/mysql/localhost.lower-test
Jul 14 10:45:21 localhost mysqld: #007/usr/sbin/mysqld: Can't change dir to '/home/mysql/' (Errcode: 13 "Permission denied")
Jul 14 10:45:21 localhost mysqld: 2020-07-14 10:45:21 140469303916736 [ERROR] Aborting
Jul 14 10:45:21 localhost systemd: mariadb.service: main process exited, code=exited, status=1/FAILURE
Jul 14 10:45:21 localhost systemd: Failed to start MariaDB 10.2.32 database server.
Jul 14 10:45:21 localhost systemd: Unit mariadb.service entered failed state.
Jul 14 10:45:21 localhost systemd: mariadb.service failed.

         3、各種度娘後發現要修改此文件配置 (migrated-from-my.cnf-settings.conf)

[root@localhost mysql]# find / -name migrated-from-my.cnf-settings.conf
/etc/systemd/system/mariadb.service.d/migrated-from-my.cnf-settings.conf
[root@localhost mysql]# vi /etc/systemd/system/mariadb.service.d/migrated-from-my.cnf-settings.conf

   [Service]
   ProtectHome=false


[root@localhost mysql]# systemctl daemon-reload

       4、再次重啓服務,直接成功了。

 

        第三步:拷貝當前正式服務器數據庫文件到新服務器上

        1、拷貝到/home/mysql下後,用數據庫查看確實也能看到數據庫了,但是發現卻不能正常操作表,提示:

            ERROR 1932 (42S02): Table 'users' doesn't exist in engine

         度娘後發現要同步 ibdata1 此文件纔行。

        2、停止服務後,同步時需要完全覆蓋當前的文件,重啓數據庫服務報錯。

Jul 14 11:11:13 localhost mysqld: 2020-07-14 11:11:13 140069742344384 [Note] /usr/sbin/mysqld (mysqld 10.2.32-MariaDB) starting as process 18711 ...
Jul 14 11:11:13 localhost mysqld: 2020-07-14 11:11:13 140069742344384 [Warning] Could not increase number of max_open_files to more than 16364 (request: 32222)
Jul 14 11:11:13 localhost mysqld: 2020-07-14 11:11:13 140069742344384 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
Jul 14 11:11:13 localhost mysqld: 2020-07-14 11:11:13 140069742344384 [Note] InnoDB: Uses event mutexes
Jul 14 11:11:13 localhost mysqld: 2020-07-14 11:11:13 140069742344384 [Note] InnoDB: Compressed tables use zlib 1.2.7
Jul 14 11:11:13 localhost mysqld: 2020-07-14 11:11:13 140069742344384 [Note] InnoDB: Using Linux native AIO
Jul 14 11:11:13 localhost mysqld: 2020-07-14 11:11:13 140069742344384 [Note] InnoDB: Number of pools: 1
Jul 14 11:11:13 localhost mysqld: 2020-07-14 11:11:13 140069742344384 [Note] InnoDB: Using SSE2 crc32 instructions
Jul 14 11:11:13 localhost mysqld: 2020-07-14 11:11:13 140069742344384 [Note] InnoDB: Initializing buffer pool, total size = 128M, instances = 1, chunk size = 128M
Jul 14 11:11:13 localhost mysqld: 2020-07-14 11:11:13 140069742344384 [Note] InnoDB: Completed initialization of buffer pool
Jul 14 11:11:13 localhost mysqld: 2020-07-14 11:11:13 140069184190208 [Note] InnoDB: If the mysqld execution user is authorized, page cleaner thread priority can be changed. See the man page of setpriority().
Jul 14 11:11:13 localhost mysqld: 2020-07-14 11:11:13 140069742344384 [ERROR] InnoDB: The innodb_system data file 'ibdata1' must be writable
Jul 14 11:11:13 localhost mysqld: 2020-07-14 11:11:13 140069742344384 [ERROR] InnoDB: The innodb_system data file 'ibdata1' must be writable

          根據此提示,重新加下權限即可

chown -R mysql:mysql /home/mysql

        再次重啓就沒有問題了。數據表也能正常操作了。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章