1. 程式人生 > 實用技巧 >mysql 基本操作

mysql 基本操作

1.設定資料庫密碼

#1.簡單的設定方式
[root@db01 ~]# mysqladmin -uroot password '123'
Warning: Using a password on the command line interface can be insecure.

#2.安全的設定方式
[root@db02 ~]# mysqladmin -uroot password
New password: 123
Confirm new password: 123

2.使用密碼登入資料庫

#1.正確的登陸方式(不標準,不安全)
[root@db01 ~]# mysql -uroot -p123
[root@db01 ~]# mysql -u root -p123

#2.正確的登陸方式(標準,安全)
[root@db01 ~]# mysql -u root -p
Enter password:

#3.錯誤的登陸方式(會把密碼當成資料庫)
[root@db01 ~]# mysql -u root -p 123
Enter password: 
ERROR 1049 (42000): Unknown database '123'

for password options, the password value is optional:  If you use a -p or --password option and specify the password value, there must be no space between -p or --password= and the password following it.
#如果使用-p或者--password引數,他們與密碼之間不能有空格

If you use a -p or --password option but do not specify the password value, the client program prompts you to enter the password. The password is not displayed as you enter it. This is more secure than giving the password on the command line. Other users on your system may be able to see a password specified on the command line by executing a command such as ps auxw. 
#如果只寫-p或--password引數什麼都不接,意思是讓你手動輸入密碼

#4.登入資料庫後直接進入庫中
[root@db01 ~]# mysql -u root -p mysql
Enter password: 

3.查詢資料庫使用者

mysql> select user,host from mysql.user;
+------+-----------+
| user | host      |
+------+-----------+
| root | 127.0.0.1 |
| root | ::1       |
|      | db01      |
| root | db01      |
|      | localhost |
| root | localhost |
+------+-----------+
6 rows in set (0.00 sec)

4.刪除沒有用的使用者(優化)

mysql> drop user root@'::1';
Query OK, 0 rows affected (0.00 sec)

mysql> drop user ''@'localhost';
Query OK, 0 rows affected (0.00 sec)

mysql> drop user ''@'db01';
Query OK, 0 rows affected (0.00 sec)

mysql> delete from mysql.user where host='db01';
Query OK, 1 row affected (0.00 sec)

mysql> select user,host from mysql.user;
+------+-----------+
| user | host      |
+------+-----------+
| root | 127.0.0.1 |
| root | localhost |
+------+-----------+
2 rows in set (0.00 sec)