天天看點

Linux下安裝mysql資料庫

1、下載下傳安裝檔案

安裝MySQL需要下面兩個檔案:

MySQL-client-5.5.10-1.rhel5

MySQL-server-5.5.10-1.rhel5

可從官方網站直接下載下傳以上檔案。由于我選用的作業系統為紅帽子企業版5,是以下載下傳的為rpm檔案,以便于安裝。

2、安裝MySQL

rpm檔案是Red Hat公司開發的軟體安裝包。該指令在安裝時常用的參數是 –ivh ,其中i表示将安裝指定的rmp軟體包,V表示安裝時的詳細資訊,h表示在安裝期間出現“#”符号來顯示目前的安裝過程。這個符号将持續到安裝完成後才停止。

1)安裝伺服器端

[root@test1 local]# rpm -ivh MySQL-server-5.5.10-1.rhel5

2)安裝用戶端

如果期間沒有産生錯誤,則表示安裝成功。

3、mysql的安裝目錄介紹

MySQL安裝完成後的資料庫檔案、配置檔案和指令檔案分别在不同的目錄。

1)資料庫目錄

/var/lib/mysql/

2)配置檔案

/usr/share/mysql(mysql.server指令及配置檔案)

3)相關指令

/usr/bin(mysqladmin mysqldump等指令)

4)啟動腳本

/etc/rc.d/init.d/(啟動腳本檔案mysql的目錄)

4、啟動與停止

1)啟動

MySQL安裝完成後啟動檔案mysql在/etc/init.d目錄下,在需要啟動時運作下面指令即可。

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

2)停止

/usr/bin/mysqladmin -u root -p shutdown

3)自動啟動

1、察看mysql是否在自動啟動清單中

[root@test1 local]# /sbin/chkconfig –list

2、把MySQL添加到你系統的啟動服務組裡面去

[root@test1 local]# /sbin/chkconfig – add mysql

3、把MySQL從啟動服務組裡面删除。

[root@test1 local]# /sbin/chkconfig – del mysql

5、測試服務是否啟動

[root@test1 local]# netstat -nat

Active Internet connections (servers and established)

Proto Recv-Q Send-Q Local Address Foreign Address State

tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN

上面顯示可以看出MySQL服務已經啟動。

6、登入MySQL

登入MySQL的指令是mysql, mysql 的使用文法如下:

mysql [-u username] [-h host] [-p[password]] [dbname]

username 與 password 分别是 MySQL 的使用者名與密碼,mysql的初始管理帳号是root。MySQL預設使用者是root,由于初始沒有密碼,第一次進時隻需鍵入mysql即可。

[root@test1 local]# mysql

mysql>

出現了“mysql>”提示符,恭喜你,安裝成功!

增加了密碼後的登入格式如下:

mysql -u root -p

Enter password: (輸入密碼)

其中-u後跟的是使用者名,-p要求輸入密碼,回車後在輸入密碼處輸入密碼。

注意:這個mysql檔案在/usr/bin目錄下,與後面講的啟動檔案/etc/init.d/mysql不是一個檔案。

7、修改登入密碼

1)修改密碼的指令

usr/bin/mysqladmin -u root password \'new-password\'

格式:mysqladmin -u使用者名 -p舊密碼 password 新密碼

例1:給root加個密碼123456。

鍵入以下指令 :

[root@test1 local]# /usr/bin/mysqladmin -u root password 123456

注:因為開始時root沒有密碼,是以-p舊密碼一項就可以省略了。

         2)測試是否修改成功

1、不用密碼登入

ERROR 1045: Access denied for user: \'root@localhost\' (Using password: NO)

顯示錯誤,說明密碼已經修改。

2、用修改後的密碼登入

[root@test1 local]# mysql -u root -p

Enter password: (輸入修改後的密碼123456)

Welcome to the MySQL monitor. Commands end with ; or \\g.

Your MySQL connection id is 4 to server version: 4.0.16-standard

Type \'help;\' or \'\\h\' for help. Type \'\\c\' to clear the buffer.

成功!

8、增加MySQL使用者

格式:grant select on 資料庫.* to 使用者名@登入主機 identified by "密碼"

例1:增加一個使用者user_1密碼為123,讓他可以在任何主機上登入,并對所有資料庫有查詢、插入、修改、删除的權限。首先用以root使用者連入MySQL,然後鍵入以下指令:

mysql> grant select,insert,update,delete on *.* to user_1@"%" Identified by "123";

例1:增加的使用者是十分危險的,如果知道了user_1的密碼,那麼他就可以在網上的任何一台電腦上登入你的MySQL資料庫并對你的資料為所欲為了,解決辦法見例2。

例2:增加一個使用者user_2密碼為123,讓此使用者隻可以在localhost上登入,并可以對資料庫aaa進行查詢、插入、修改、删除的操作(localhost指本地主機,即MySQL資料庫所在的那台主機),這樣使用者即使用知道user_2的密碼,他也無法從網上直接通路資料庫,隻能通過 MYSQL主機來操作aaa庫。

   mysql>grant select,insert,update,delete on aaa.* to user_2@localhost identified by "123";

用新增的使用者如果登入不了MySQL,在登入時用如下指令:

mysql -u user_1 -p -h 192.168.113.50 (-h後跟的是要登入主機的ip位址)

9、備份與恢複

1)備份

例如:将上例建立的aaa庫備份到檔案back_aaa中

[root@test1 mysql]# mysqldump -u root -p --opt aaa > back_aaa

2)恢複

[root@test mysql]# mysql -u root -p ccc < back_aaa

10、常見問題

問題1:The server quit without updating PID file (/var/lib/mysql/localhost.pid

第一種:mysql5.1.30   初次啟動會出現Manager of pid-file quit without updating fi[FAILED]的報錯,需要注釋/etc/my.cnf裡的skip-federated注釋掉即#skip-federated就OK了!

第二種:殺掉已經啟動的程序

[root@localhost mysql]# ps -A|grep mysql

8016 pts/2 00:00:00 mysqld_safe

8037 pts/2 00:00:00 mysqld

[root@localhost mysql]# kill -9 8037(殺掉這個8016那個就自動被殺了!)

[root@localhost mysql]# kill -9 8016

[root@localhost mysql]# /etc/init.d/mysql restart

Starting MySQL [ OK ]

問題2:ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’ (2)

步驟1:檢視 /etc/rc.d/init.d/mysqld status 看看mysql是否已經啟動.

步驟2:确定你的mysql.sock的位置是否正确

mysql -u 你的mysql使用者名 -p -S /var/lib/mysql/mysql.sock

步驟3:重新啟動mysql

service mysqld start

步驟4:如果是權限問題,則先改變權限

mysql>#chown -R mysql:mysql /var/lib/mysql

[root@localhost ~]# /etc/init.d/mysqld start

[root@localhost ~]# mysql -uroot –p

11、解除安裝mysql

1)檢視安裝mysql的服務

rpm -qa|grep -i mysql

rpm -ev MySQL-server-4.0.14-0 MySQL-client-4.0.14-0

2)解除安裝後/var/lib/mysql中的資料及/etc/my.cnf不會删除,如果确定沒用後就手工删除

rm -f /etc/my.cnf

rm -rf /var/lib/mysql