登入和退出
> mysql
-h 引數後面接hostname或者hostIP
-P 引數後面接Mysql服務的埠號,通過指定的埠號來進行連線
-u 引數後面接username使用者名稱
-p 引數會提示密碼
-e 引數可以可以直接加SQL語句,登入MYSQL伺服器以後即可執行這個SQL語句,然後退出Mysql伺服器
DatabaseName 引數指明登入到哪一個資料庫中,如果沒有該引數,會直接登入到Mysql資料庫中.然後可以使用USE命令來選擇資料庫> quit;
建立使用者
1. create user語句
> create user 'User-name'@'Host-name' (對應User表中的User和Host欄位) IDENTIFIED BY 'password' (對應User表中的Password欄位) || IDENTIFIED BY 'PASSWORD(password)'
2. insert into mysql.user(直接插入mysql.user表)
> insert into mysql.user(Host,User,Password,ssl_cipher,x509_issuer,x509_subject) values('Host-name','User-name',PASSWPRD('password'),'','','');
> flush privileges;
刪除使用者
Methods-1
> drop user 'username'@'hostname';
Methods-2
> delete from mysql.user where User='username' and Host='hostname';
> flush privileges;
查詢所有使用者資訊
> select User,group_concat(Host) from mysql.user group by User having User!=''; +---------+-----------------------------+ | User | group_concat(Host) | +---------+-----------------------------+ | root | localhost,centos2,127.0.0.1 | | test1 | localhost | | test2 | localhost | | test2_1 | localhost | | zabbix | localhost | +---------+-----------------------------+
> SELECT DISTINCT CONCAT('User: ''',user,'''@''',host,''';') AS query FROM mysql.user; +------------------------------+ | query | +------------------------------+ | User: 'root'@'127.0.0.1'; | | User: ''@'centos2'; | | User: 'root'@'centos2'; | | User: ''@'localhost'; | | User: 'root'@'localhost'; | | User: 'test1'@'localhost'; | | User: 'test2'@'localhost'; | | User: 'test2_1'@'localhost'; | | User: 'zabbix'@'localhost'; | +------------------------------+ 9 rows in set (0.02 sec)
查詢某個使用者的所有許可權
Methods-1
> show grants for 'hostname'@'hostname' \G
Methods-2
> select * from mysql.user where User='username' and Host='hostname' \G
> show grants for 'root'@'localhost'\G *************************** 1. row *************************** Grants for root@localhost: GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY PASSWORD '*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9' WITH GRANT OPTION 1 row in set (0.00 sec) > show grants for 'test1'@'localhost'\G *************************** 1. row *************************** Grants for test1@localhost: GRANT USAGE ON *.* TO 'test1'@'localhost' IDENTIFIED BY PASSWORD '*AD6EE75212922D967AF18D408481254CC164B39D' 1 row in set (0.00 sec) > select * from mysql.user where User='root' and Host='localhost'\G
修改/設定密碼
1. 修改root使用者密碼
Methods-1
# mysqladmin -u root -p password 'new_password'
Enter password:--->在這裡要輸入正確的舊密碼
Methods-2
> update mysql.user set Password=PASSWORD('new_password') where User='root' and Host='localhost';
> flush privileges;
Methods-3
> set password=PASSWORD('new_password');
2. 修改普通使用者密碼
Methods-1
> set password for 'username'@'hostname'=PASSWORD('new_password');
Methods-2 建立一個無初始密碼的普通使用者後可以直接用其登入mysql,登入後可以通過這個方法設定密碼:
> set password=PASSWORD('new_password');
Methods-3
> update mysql.user set Password=PASSWORD('new_password') where User='username' and Host='hostname';
> flush privileges;
Methods-4
> grant select on *.* to 'username'@'hostname' identified by 'new_password';
3. 忘記root密碼的解決
# /etc/init.d/mysqld start --skip-grant-tables
# mysql -u root -p --->這樣可以無密碼登陸進去了-> update mysql.user set Password=PASSWORD('new_password') where User='root' and Host='localhost';
> flush privileges;