理解SQL語句中各部分的含義,學會使用MySQL語句管理資料庫。 ...
註:
mysql.user表中Host為%的含義
Host列指定了允許用戶登錄所使用的IP,比如user=root Host=192.168.1.1。這裡的意思就是說root用戶只能通過192.168.1.1的客戶端去訪問。
而%是個通配符,如果Host=192.168.1.%,那麼就表示只要是IP地址首碼為“192.168.1.”的客戶端都可以連接。如果Host=%,表示所有IP都有連接許可權。、
這也就是為什麼在開啟遠程連接的時候,大部分人都直接把Host改成%的緣故,為了省事。
1:新增用戶:
註:MySQL資料庫下user表中,Host和User為兩個主鍵列(primary key),已經各版本下非空未設置預設欄位。
登錄後,切換db:
[sql] view plain copy- mysql> use mysql;
- Reading table information for completion of table and column names
- You can turn off this feature to get a quicker startup with -A
- Database changed
新增用戶:
註:限制kaka用戶的登陸ip為10.155.123.55,ip為隨手寫入,如果正確配置為您有效登陸ip,所有ip登陸,則設置Host為 '%'
[sql] view plain copy
- mysql> INSERT INTO mysql.user(Host,User,Password) VALUES("10.155.123.55","kaka",PASSWORD("kaka123"));
在版本 5.6.27:
[sql] view plain copy- mysql> INSERT INTO mysql.user(Host,User,Password,ssl_cipher,x509_issuer,x509_subject) VALUES("10.155.123.55","kaka",PASSWORD("kaka123"),"","","");
- Query OK, 1 row affected (0.03 sec)
新增用戶(全sql):
[sql] view plain copy- INSERT INTO `user`(`Host`,`User`,`Password`,`Select_priv`,`Insert_priv`,`Update_priv`,`Delete_priv`,`Create_priv`,`Drop_priv`,`Reload_priv`,`Shutdown_priv`,`Process_priv`,`File_priv`,`Grant_priv`,`References_priv`,`Index_priv`,`Alter_priv`,`Show_db_priv`,`Super_priv`,`Create_tmp_table_priv`,`Lock_tables_priv`,`Execute_priv`,`Repl_slave_priv`,`Repl_client_priv`,`Create_view_priv`,`Show_view_priv`,`Create_routine_priv`,`Alter_routine_priv`,`Create_user_priv`,`Event_priv`,`Trigger_priv`,`Create_tablespace_priv`,`ssl_type`,`ssl_cipher`,`x509_issuer`,`x509_subject`,`max_questions`,`max_updates`,`max_connections`,`max_user_connections`,`plugin`,`authentication_string`,`password_expired`) VALUES ('%','root','*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','','','','',0,0,0,0,'mysql_native_password','','N');
新增用戶完成,刷新mysql的系統許可權相關表
[sql] view plain copy- mysql> flush privileges;
- Query OK, 0 rows affected (0.00 sec)
設置遇到問題,請查看:MySQL配置和設置問題小結
重啟生效:
[sql] view plain copy
- [root@Tony_ts_tian bin]# service mysqld restart
- Shutting down MySQL.... SUCCESS!
- Starting MySQL. SUCCESS!
[sql] view plain copy
- mysql> SELECT Host,User,Password FROM mysql.user;
- +----------------+------+-------------------------------------------+
- | Host | User | Password |
- +----------------+------+-------------------------------------------+
- | localhost | root | *71ABCA8B06D46066CEF8062A75256E66243D0FC8 |
- | tony\_ts\_tian | root | *71ABCA8B06D46066CEF8062A75256E66243D0FC8 |
- | 127.0.0.1 | root | *71ABCA8B06D46066CEF8062A75256E66243D0FC8 |
- | ::1 | root | *71ABCA8B06D46066CEF8062A75256E66243D0FC8 |
- | 10.155.123.55 | kaka | *90B3D884FB6092549F244125549B77C000A0F9C6 |
- | % | root | *71ABCA8B06D46066CEF8062A75256E66243D0FC8 |
- +----------------+------+-------------------------------------------+
- 6 rows in set (0.00 sec)
2:修改信息,密碼,類似可修改其他欄位。
[sql] view plain copy