天天看點

MySQL 初步安裝後更改datadir目錄幾種方式

注釋: 

     若mysql 安裝初步使用rpm預設将db安裝到/var/lib/mysql/路徑後,想更改db路徑,怎麼處理呢? 

測試步驟:

    1、 簡單叙述安裝步驟

    2、更改db目錄的2中方式..

         ①. 通過db初始化指令更改db存放位置   /usr/bin/mysql_install_db --user=mysql --basedir=/usr/ --datadir=/data/public3306/db/

         ②. 通過将原來db目錄内内容拷貝到新位址,再更改my.cnf 檔案的datadir (類似oracle 冷備,更改控制檔案 datafile指針..)  

    3、解除安裝搭建的測試環境

<b>一、簡單叙述安裝步驟</b>

<b>--&gt; 準備安裝包</b>

[root@lottery ~]# ll  

-rw-rw-rw-  1 root root  18360416 5月  19 10:54 mysql-client-5.6.19-1.el6.x86_64.rpm

-rw-rw-rw-  1 root root  54592892 5月  19 10:54 mysql-server-5.6.19-1.el6.x86_64.rpm

<b>--&gt;rpm -vhi 安裝包</b>

[root@lottery ~]# rpm -hvi mysql-client-5.6.19-1.el6.x86_64.rpm

preparing...                ########################################### [100%]

   1:mysql-client      ########################################### [100%] 

[root@lottery ~]# rpm -hvi mysql-server-5.6.19-1.el6.x86_64.rpm

   1:mysql-server     ########################################### [100%]

<b>--&gt;啟動mysql服務</b>

[root@lottery ~]#  /etc/init.d/mysql start

starting mysql... success!

<b>--&gt;初始化密碼</b>

# mysqld_safe --user=mysql --skip-grant-tables --skip-networking &amp;

# mysql -uroot

mysql&gt; update user set password=password('newpassword') where user='root';

mysql&gt; flush privileges;

mysql&gt; quit 

# /etc/init.d/mysqld restart

# mysql -uroot -p

enter password:

mysql&gt;   show variables like '%datadir%';

+---------------+-----------------------+

| variable_name | value                           |

| datadir             | /var/lib/mysql/           |

mysql&gt;

<b>二、更改db目錄的2中方式..</b>

<b> ①. 通過db初始化指令更改db存放位置   /usr/bin/</b>mysql_install_db <b>--user=mysql --basedir=/usr/ --datadir=/data/public3306/db/ </b>

   --  将之前資料庫停掉--用mysql_install_db 腳本 初始化(建立庫)到/data/public3306/db/ 目錄的資料庫.. 

   --  若原資料庫有建立的database ,需要将database對應的目錄cp -rp 到更改後的路徑/data/public3306/db/ ;

   --  否則原新的database不會在新路徑展現....  

[root@lottery public3306]# /usr/bin/mysql_install_db --user=mysql --basedir=/usr/ --datadir=/data/public3306/db/

installing mysql system tables...ok

filling help tables...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 mysql 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 lottery 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 manual for more instructions.

you can start the mysql daemon with:

  cd /usr ; /usr//bin/mysqld_safe &amp;

you can test the mysql daemon with mysql-test-run.pl

  cd mysql-test ; perl mysql-test-run.pl

please report any problems at http://bugs.mysql.com/

the latest information about mysql is available on the web at

  http://www.mysql.com

support mysql by buying support/licenses at http://shop.mysql.com

warning: found existing config file /usr//my.cnf on the system.

because this file might be in use, it was not replaced,

but was used in bootstrap (unless you used --defaults-file)

and when you later start the server.

the new default config file was created as /usr//my-new.cnf,

please compare it with your file and take the changes you need.

warning: default config file /etc/my.cnf exists on the system

this file will be read by default by the mysql server

if you do not want to use this, either remove it, or use the

--defaults-file argument to mysqld_safe when starting the server

[root@lottery public3306]#  /etc/init.d/mysql start

starting mysql....<b> success! </b>

[root@lottery public3306]# cd db

[root@lottery db]# ll

總用量 2097172

-rw-rw---- 1 mysql mysql         56 5月  19 15:47 auto.cnf 

drwx------ 2 mysql mysql       4096 5月  19 15:46 mysql

drwx------ 2 mysql mysql       4096 5月  19 15:46 performance_schema

drwx------ 2 mysql mysql       4096 5月  19 15:46 test

[root@lottery db]# 

<b> ②. 通過将原來db目錄内内容拷貝到新位址,再更改my.cnf 檔案的datadir </b>

    --  測試之前将原database,執行create database lottery ,利于遷移後是否正确.

<b>--&gt; 原路徑</b>

[root@lottery mysql]# pwd

/var/lib/mysql/ 

<b>--&gt; 目錄開頭的為database</b>

[root@lottery mysql]# ll 

drwx------ 2 mysql mysql       4096 5月  19 15:52 <b>lottery</b>

drwx------ 2 mysql mysql       4096 5月  19 15:46 test 

<b>--&gt; 檢視lottery 資料庫的表</b>

[root@lottery lottery]# ll  lottery/*

-rw-rw---- 1 mysql mysql    54 5月  19 15:47 db.opt

-rw-rw---- 1 mysql mysql  8560 5月  19 15:52 t1.frm    <b>---t1表 一張</b>

-rw-rw---- 1 mysql mysql 98304 5月  19 15:52 t1.ibd

<b>--&gt; 建立新datadir目錄</b>

[root@lottery lottery ]# mkdir -p /data/public3306/db 

     --  參數-p: 級聯建立

<b>--&gt; 整個mysql datadir進行拷貝</b>

[root@lottery lottery ]# cp -pr /var/lib/mysql/ /data/public3306/db 

     --  參數-p:連帶屬性拷貝,-r是目錄拷貝

<b>--&gt; 檢查cp 的檔案内容</b>

[root@lottery lottery  ]# cd /data/public3306/db 

<b>--&gt; 更改my.cnf 的datadir參數,将原來的/var/lib/mysql 改成 /data/public3306/db </b>

[root@lottery db]# vi /etc/my.cnf

<b>--&gt; 啟動mysql服務</b>

[root@lottery db]# /etc/init.d/mysql start

starting mysql..... <b>success</b>!

<b>--&gt; 檢視更改後datadir是否有效的2種方式</b>

<b>--&gt; os層檢視</b>

[root@lottery db]# ps -ef | grep mysql 

root     11346     1  0 16:07 pts/6    00:00:00 /bin/sh /usr/bin/mysqld_safe <b>--datadir=/data/public3306/db</b> --pid-file=/data/public3306/mysqld.pid

mysql    12007 11346 17 16:07 pts/6    00:00:03 /usr/sbin/mysqld --basedir=/usr<b> --datadir=/data/public3306/db</b> --plugin-dir=/usr/lib64/mysql/plugin --user=mysql --log-error=/data/public3306/err.log --pid-file=/data/public3306/mysqld.pid --socket=/data/public3306/mysql.sock --port=3306 

<b>--&gt; db層檢視</b>  

mysql&gt; show variables like '%datadir%';

| variable_name | value                 |

| datadir            | <b>/data/public3306/db/ |</b>

+---------------+-----------------------+ 

<b>--&gt; 檢查下原來database lottery 中的t1表 再新的環境中是否存在</b>

mysql&gt; use lottery;

database changed 

mysql&gt; show tables;

+-------------------+

| tables_in_lottery |

| <b>t1                </b>|

+-------------------+ 

<b>三、解除安裝搭建的測試環境 </b>

<b>--&gt; 檢視系統中是否以rpm包安裝的mysql</b>

[root@lottery db]# rpm -qa | grep -i mysql

mysql-client-5.6.19-1.el6.x86_64

mysql-server-5.6.19-1.el6.x86_64

<b>--&gt; 通過rpm -e 解除安裝軟體包</b>

[root@lottery db]# rpm -e mysql-client-5.6.19-1.el6.x86_64

[root@lottery db]# rpm -e mysql-server-5.6.19-1.el6.x86_64

<b>--&gt; 檢視是否有mysql服務,删除 mysql服務指令:chkconfig --del mysql</b>

[root@lottery db]# chkconfig --list | grep -i mysql

<b>--&gt; mysql程式指令進行删除 </b>

[root@lottery db]# whereis mysql 

mysql: /usr/lib64/mysql

[root@lottery db]# rm  -rf /usr/lib64/mysql

  .... 解除安裝完成..

  【源于本人筆記】 若有書寫錯誤,表達錯誤,請指正...