[轉帖] mysql 使用者 許可權 密碼等操作記錄

來源:互聯網
上載者:User

標籤:並且   管理員   使用   參數說明   upd   5.7   直接   class   auth   

前言

From :78214336

mysql5.7版本中使用者管理與以前版本略有不同,在此記錄,以備忘

登陸
[[email protected] ~]# mysql -h 127.0.0.1 -P 3316 -u root -pEnter password:Welcome to the MySQL monitor.  Commands end with ; or \g.Your MySQL connection id is 9Server version: 5.7.17 Source distributionCopyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.mysql>

 

 

參數說明:
-h: 指定資料庫IP地址;
-P: 指定連接埠,預設的3306時,可以忽略;
-u: 指定登陸使用者名稱;
-p: 指定登陸密碼(小寫,注意與指定連接埠的大寫P區分);

指定操作資料庫
mysql> show databases;  # 查看所有資料庫+--------------------+| Database           |+--------------------+| information_schema || fhgk               || mysql              || performance_schema || sys                |+--------------------+5 rows in set (0.01 sec)mysql> use mysql    # 指定當前操作的資料庫Reading table information for completion of table and column namesYou can turn off this feature to get a quicker startup with -ADatabase changedmysql>

 

建立使用者
# 建立使用者mysql> CREATE USER ‘username‘@‘host‘ IDENTIFIED BY ‘password‘;# 刪除使用者mysql> DROP USER ‘username‘@‘host‘;

 

host參數說明:
% 匹配所有主機
localhost localhost不會被解析成IP地址,直接通過UNIXsocket串連
127.0.0.1 會通過TCP/IP協議串連,並且只能在本機訪問;
::1 ::1就是相容支援ipv6的,表示同ipv4的127.0.0.1

此時還沒有授權,只能登陸,無法做其餘操作

使用者授權
# 使用者授權mysql> grant privileges ON databasename.* TO ‘username‘@‘host‘;# 建立使用者的同時授權mysql> grant all privileges on databasename.* to ‘username‘@‘host‘ identified by ‘1234‘;# 授權重新整理mysql> flush privileges;# 查看使用者擁有許可權mysql> show grants for [email protected]‘%‘;+----------------------------------------------------------------------+| Grants for [email protected]%                                                     |+----------------------------------------------------------------------+| GRANT USAGE ON *.* TO ‘dev‘@‘%‘                                      || GRANT SELECT, INSERT, UPDATE, DELETE, ALTER ON `fhgk`.* TO ‘dev‘@‘%‘ |+----------------------------------------------------------------------+2 rows in set (0.00 sec)# 撤消使用者授權,撤消要求各參數與授權時使用的一致,可以相查看授權再撤消mysql> revoke privileges ON databasename.* FROM ‘username‘@‘host‘;

 

privileges參數說明: all privileges: 所有許可權; select: 查詢; insert: 新增記錄; update: 更新記錄; delete: 刪除記錄; create: 建立表; drop: 刪除表; alter: 修改表結構; index: 索引相關許可權; execute: 執行預存程序與call函數 references: 外鍵相關; create temporary tables:建立暫存資料表; lock tables 鎖表; create view 建立視圖; show view 查看視圖結構; create routine alter routine: event: trigger: 觸發器相關;

 

databasename.*參數說明:
此處可以針對具體的某個庫,如:【zjims.*】;
也可以針對具體庫中的某個對象,如:【zjims.t_user】;
還可以針對所有資料庫,如:【.】;

修改密碼
# 修改自己的密碼mysql> set password=password(‘newpassword‘);# 修改別人密碼——方法1mysql> set password for ‘username‘@‘host‘ = password(‘newpassword‘);# 修改別人密碼——方法2: 適用mysql5.7以前的版本,5.7以後的版本中mysql.user表沒有了password欄位mysql> update mysq.user set password=password(‘newpassword‘) where user=‘user‘ and host=‘host‘;# 修改別人密碼——方法3:適用mysql5.7mysql> update mysql.user set authentication_string=password(‘newpassword‘) where user=‘root‘;# 修改別人密碼——方法4mysql> alter user ‘test‘@‘%‘ identified by ‘newpassword‘;

 

 

重設管理員密碼
  1. 停止mysql服務:service mysqld stop 或 ./mysql.server stop;
  2. 以不檢查許可權方式啟動mysql:./mysqld –skip-grant-tables –user=mysql &;
  3. 以空密碼方式登陸:mysql -h 127.0.0.1 -P 3306 -u root;
  4. mysql5.7以前版本——修改root密碼:update mysq.user set password=password(‘newpassword’) where user=’root’;
  5. mysql5.7以後版本——修改root密碼:update mysql.user set authentication_string=password(‘newpassword’) where user=’root’;(只能用此種update方法修改)
  6. 重新整理許可權:flush privileges;
  7. 關閉mysql:shutdown;
  8. 以正常方式啟動mysql: service mysqld start 或 ./mysql.server start;
參考資料
  1. http://www.cnblogs.com/fslnet/p/3143344.html
  2. http://www.cnblogs.com/xujishou/p/6306765.html
  3. http://www.cnblogs.com/4php/p/4113593.html

[轉帖] mysql 使用者 許可權 密碼等操作記錄

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.