linux下MySQL初始環境設定

hzczichao發表於2010-07-16
[1]為MySQL的root使用者設定密碼

MySQL在剛剛被安裝的時候,它的root使用者是沒有被設定密碼的。首先來設定MySQL的root密碼。[@more@]


[root@sample ~]# mysql -u root  ← 用root使用者登入MySQL伺服器

Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 2 to server version: 4.1.20

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

mysql> select user,host,password from mysql.user;  ← 檢視使用者資訊
+------+------------------------------+---------------+
| user | host          | password |
+------+------------------------------+---------------+
| root | localhost        |       | 
← root密碼為空
| root | sample.centospub.com  |       | 
← root密碼為空
|   | sample.centospub.com |       |
|   | localhost       |       |
+------+------------------------------+---------------+

4 rows in set (0.00 sec)

mysql> set password for root@localhost=password('在這裡填入root密碼');  ← 設定root密碼
Query OK, 0 rows affected (0.01 sec)

mysql> set password for root@'sample.centospub.com'=password('在這裡填入root密碼');  ← 設定root密碼
Query OK, 0 rows affected (0.01 sec)

mysql> select user,host,password from mysql.user;  ← 檢視使用者資訊
+------+--------------------------------+--------------------------+
| user | host          | password     |
+------+--------------------------------+--------------------------+
| root | localhost        |
19b68057189b027f |  ← root密碼被設定
| root | sample.centospub.com   |
19b68057189b027f |  ← root密碼被設定
|    | sample.centospub.com   |          |
|    | localhost        |          |
+------+--------------------------------+--------------------------+
4 rows in set (0.01 sec)

mysql> exit  ← 退出MySQL伺服器
Bye


+++++++
然後,測試一下root密碼有沒有生效。
[root@sample ~]# mysql -u root  ← 透過空密碼用root登入

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO) 
← 出現此錯誤資訊說明密碼設定成功

[root@localhost ~]#
mysql -u root -h sample.centospub.com  ← 透過空密碼用root登入

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO) 
← 出現此錯誤資訊說明密碼設定成功

[root@sample ~]#
mysql -u root -p  ← 透過密碼用root登入
Enter password: 
← 在這裡輸入密碼

Welcome to the MySQL monitor. Commands end with ; or g. 
← 確認用密碼能夠成功登入
Your MySQL connection id is 5 to server version: 4.1.20

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

mysql> exit
Bye

[root@sample ~]# mysql -u root -h sample.centospub.com -p  ← 透過密碼用root登入
Enter password:  ← 在這裡輸入密碼

Welcome to the MySQL monitor. Commands end with ; or g.  ← 確認用密碼能夠成功登入
Your MySQL connection id is 6 to server version: 4.1.20

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

mysql> exit  ← 退出MySQL伺服器
Bye
++++++++++++++
[2] 刪除匿名使用者
在MySQL剛剛被安裝後,存在使用者名稱、密碼為空的使用者。這使得資料庫伺服器有無需密碼被登入的可能性。為消除隱患,將匿名使用者刪除。
[root@sample ~]# mysql -u root -p  ← 透過密碼用root登入
Enter password: 
← 在這裡輸入密碼

Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 7 to server version: 4.1.20

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

mysql> select user,host from mysql.user;  ← 檢視使用者資訊
+------+----------------------------+
| user | host         |
+------+----------------------------+
|   | localhost       |
| root | localhost       |
|   | sample.centospub.com |
| root | sample.centospub.com  |
+------+----------------------------+
4 rows in set (0.02 sec)

mysql> delete from mysql.user where user='';  ← 刪除匿名使用者
Query OK, 2 rows affected (0.17 sec)

mysql> select user,host from mysql.user;  ← 檢視使用者資訊
+------+----------------------------+
| user | host         |
+------+----------------------------+
| root | localhost      |
| root | sample.centospub.com |
+------+----------------------------+
2 rows in set (0.00 sec)

mysql> exit  ← 退出MySQL伺服器
Bye
+++++++++++++++++++
[3] 刪除測試用資料庫

在MySQL被安裝後,存在名為test的空資料庫,將它刪除。這裡要注意的是,系統預設的還有一個名為mysql的資料庫,它用於系統管理,所以請不要刪除。
[root@sample ~]# mysql -u root -p  ← 透過密碼用root登入
Enter password:  ← 在這裡輸入密碼
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 8 to server version: 4.1.20

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

mysql> show databases;  ← 檢視系統已存在的資料庫
+-------------+
| Database |
+-------------+
| mysql   |
| test   |
+------------+
2 rows in set (0.02 sec)

mysql> drop database test;  ← 刪除名為test的空資料庫
Query OK, 0 rows affected (0.07 sec)

mysql> show databases;  ← 檢視系統已存在的資料庫
+-------------+
| Database |
+-------------+
| mysql   |  ← 確認名為test的資料庫被刪除,已不存在
+-------------+
1 row in set (0.00 sec)

mysql> exit  ← 退出MySQL伺服器
Bye

++++++++++++
測試MySQL
下面對MySQL進行測試。包括建立新使用者,以及用對關係性資料庫進行資料庫操作的指令來試著建立資料庫及資料表。這裡,新建使用者以centospub為例。


[root@sample ~]# mysql -u root -p  ← 透過密碼用root登入
Enter password: 
← 在這裡輸入密碼

Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 9 to server version: 4.1.20

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

mysql> grant all privileges on test.* to centospub@localhost identified by '在這裡定義密碼';  ← 建立對test資料庫有完全操作許可權的名為centospub的使用者
Query OK, 0 rows affected (0.03 sec)

mysql> select user from mysql.user where user='centospub';  ← 確認centospub使用者的存在與否
+---------+
| user  |
+---------+
| centospub |  ← 確認centospub已經被建立
+---------+
1 row in set (0.01 sec)

mysql> exit  ← 退出MySQL伺服器
Bye

[root@sample ~]# mysql -u centospub -p  ← 用新建立的centospub使用者登入MySQL伺服器
Enter password:  ← 在這裡輸入密碼

Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 10 to server version: 4.1.20

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

mysql> create database test;  ← 建立名為test的資料庫
Query OK, 1 row affected (0.00 sec)

mysql> show databases;  ← 檢視系統已存在的資料庫
+-------------+
| Database |
+-------------+
| test    |
+-------------+
1 row in set (0.00 sec)

mysql> use test  ← 連線到資料庫
Database changed

mysql> create table test(num int, name varchar(50));  ← 在資料庫中建立表
Query OK, 0 rows affected (0.03 sec)

mysql> show tables;  ← 檢視資料庫中已存在的表
+-------------------+
| Tables_in_test |
+-------------------+
| test     |
+-------------------+
1 row in set (0.01 sec)

mysql> insert into test values(1,'Hello World!');  ← 插入一個值到表中
Query OK, 1 row affected (0.02 sec)

mysql> select * from test;  ← 檢視資料庫中的表的資訊
+------+-------------------+
| num | name      |
+------+-------------------+
| 1   | Hello World!  |
+------+-------------------+
1 row in set (0.00 sec)

mysql> update test set name='Hello Everyone!';  ← 更新表的資訊,賦予新的值
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql> select * from test;  ← 檢視資料庫中的表的資訊
+------+----------------------+
| num | name      |
+------+----------------------+
| 1   | Hello Everyone! |  ← 確認被更新到新的值
+------+----------------------+
1 row in set (0.01 sec)

mysql> delete from test where num=1;  ← 刪除表內的值
Query OK, 1 row affected (0.00 sec)

mysql> select * from test;  ← 確認刪除結果
Empty set (0.01 sec)

mysql> drop table test;  ← 刪除表
Query OK, 0 rows affected (0.01 sec)

mysql> show tables;  ← 檢視錶資訊
Empty set (0.00 sec)  ← 確認表已被刪除

mysql> drop database test;  ← 刪除名為test的資料庫
Query OK, 0 rows affected (0.01 sec)

mysql> show databases;  ← 檢視已存在的資料庫
Empty set (0.01 sec)  ← 確認test資料庫已被刪除(這裡非root使用者的關係,看不到名為mysql的資料庫)

mysql> exit  ← 退出MySQL伺服器
Bye

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/23595918/viewspace-1035338/,如需轉載,請註明出處,否則將追究法律責任。

相關文章