今天在学习mysql的时候,一顿蜜汁操作,再次使用mysql的时候发现,不管用啥子命令,都出现了一个报错
mysql> select user,password from mysql.user;
ERROR 1142 (42000): SELECT command denied to user ‘root’@‘localhost’ for table ‘user’
看了一下报错信息,权限不够。。。那就是没有权限了,so,给他权限就好了
step01
退出数据库并且关闭mysql服务
mysql> quitBye[root@jinch ~]# /etc/init.d/mysqld stopShutting down MySQL.. SUCCESS!
step02
安全模式启动mysql,root用户登录
[root@jinch ~]# mysqld_safe --skip-grant-tables &[root@jinch ~]# mysql -uroot -p123 mysql
step03
切换数据库&查看表信息中的root用户的localhost权限
mysql> use mysql;Database changedmysql> show tables;+---------------------------+| Tables_in_mysql |+---------------------------+| columns_priv || db || event || func || general_log|| help_category || help_keyword || help_relation || help_topic|| innodb_index_stats || innodb_table_stats || ndb_binlog_index|| plugin|| proc || procs_priv|| proxies_priv || servers || slave_master_info || slave_relay_log_info|| slave_worker_info || slow_log || tables_priv|| time_zone || time_zone_leap_second|| time_zone_name || time_zone_transition|| time_zone_transition_type || user |+---------------------------+28 rows in set (0.00 sec)mysql> select * from user where user='root' and host='localhost'\G;*************************** 1. row ***************************Host: localhostUser: rootPassword: *23AE809DDACAF96AF0FD78ED04B6A265E05AA257Select_priv: NInsert_priv: NUpdate_priv: NDelete_priv: NCreate_priv: NDrop_priv: NReload_priv: NShutdown_priv: NProcess_priv: NFile_priv: NGrant_priv: NReferences_priv: NIndex_priv: NAlter_priv: NShow_db_priv: NSuper_priv: NCreate_tmp_table_priv: NLock_tables_priv: NExecute_priv: NRepl_slave_priv: NRepl_client_priv: NCreate_view_priv: NShow_view_priv: NCreate_routine_priv: NAlter_routine_priv: NCreate_user_priv: NEvent_priv: NTrigger_priv: NCreate_tablespace_priv: Nssl_type: ssl_cipher: x509_issuer: x509_subject: max_questions: 0max_updates: 0max_connections: 0max_user_connections: 0plugin: mysql_native_passwordauthentication_string: NULLpassword_expired: N1 row in set (0.00 sec)ERROR: No query specified
这里发现全部都是N ,表示root用户本地登陆没有权限
step04
修改root用户的localhost权限(两种写法)
写法1:
mysql> update mysql.user set Grant_priv='Y',Super_priv='Y' where user='root';mysql> flush privileges;mysql>grant all on *.* to 'root'@'localhost';
写法2:
mysql> update user set `Insert_priv` ='Y' where user='root' and host='localhost';mysql> update user set `Update_priv` ='Y' where user='root' and host='localhost';mysql> update user set `Delete_priv` ='Y' where user='root' and host='localhost';mysql> update user set `Create_priv` ='Y' where user='root' and host='localhost';mysql> update user set `Drop_priv` ='Y' where user='root' and host='localhost';mysql> update user set `Reload_priv` ='Y' where user='root' and host='localhost';mysql> update user set `Shutdown_priv` ='Y' where user='root' and host='localhost';mysql> update user set `Process_priv` ='Y' where user='root' and host='localhost';mysql> update user set `File_priv` ='Y' where user='root' and host='localhost';mysql> update user set `Grant_priv` ='Y' where user='root' and host='localhost';mysql> update user set `References_priv` ='Y' where user='root' and host='localhost';mysql> update user set `Index_priv` ='Y' where user='root' and host='localhost';mysql> update user set `Alter_priv` ='Y' where user='root' and host='localhost';mysql> update user set `Show_db_priv` ='Y' where user='root' and host='localhost';mysql> update user set `Super_priv` ='Y',`Create_tmp_table_priv` = 'Y' where user='root'' and host='localhost';mysql> update user set `Lock_tables_priv` ='Y' where user='root' and host='localhost'; mysql> update user set `Execute_priv` ='Y' where user='root' and host='localhost';mysql> update user set `Repl_slave_priv` ='Y' where user='root' and host='localhost';mysql> update user set `Repl_client_priv` ='Y' where user='root' and host='localhost';mysql> update user set `Create_view_priv` ='Y' where user='root' and host='localhost';mysql> update user set `Show_view_priv` ='Y' where user='root' and host='localhost';mysql> update user set `Create_routine_priv` ='Y' where user='root' and host='localhost'';mysql> update user set `Alter_routine_priv` ='Y' where user='root' and host='localhost';;mysql> update user set `Create_user_priv` ='Y' where user='root' and host='localhost'; mysql> update user set `Event_priv` ='Y' where user='root' and host='localhost';mysql> update user set `Trigger_priv` ='Y' where user='root' and host='localhost';mysql> flush privileges;Query OK, 0 rows affected (0.00 sec)
我这里有点傻。。。自己一个一个敲了一遍,可以直接用‘,’ 分割一次写完的,,,
step05
退出&重启&登陆
mysql> quitBye[root@jinch ~]# /etc/init.d/mysqld restartShutting down MySQL.. SUCCESS! Starting MySQL.. SUCCESS! [root@jinch ~]# mysql -uroot -p123
step06
切换库
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 changed
step07
查看表信息
mysql> select * from user\G;*************************** 1. row ***************************Host: localhostUser: rootPassword: *23AE809DDACAF96AF0FD78ED04B6A265E05AA257Select_priv: YInsert_priv: YUpdate_priv: YDelete_priv: YCreate_priv: YDrop_priv: YReload_priv: YShutdown_priv: YProcess_priv: YFile_priv: YGrant_priv: YReferences_priv: YIndex_priv: YAlter_priv: YShow_db_priv: YSuper_priv: YCreate_tmp_table_priv: YLock_tables_priv: YExecute_priv: YRepl_slave_priv: YRepl_client_priv: YCreate_view_priv: YShow_view_priv: YCreate_routine_priv: YAlter_routine_priv: YCreate_user_priv: YEvent_priv: YTrigger_priv: YCreate_tablespace_priv: Nssl_type: ssl_cipher: x509_issuer: x509_subject: max_questions: 0max_updates: 0max_connections: 0max_user_connections: 0plugin: mysql_native_passwordauthentication_string: NULLpassword_expired: N1 row in set (0.01 sec)ERROR: No query specified权限已经基本都有了
测试一下
mysql> create database jinc;Query OK, 1 row affected (0.00 sec)mysql> select user,host from mysql.user;+------+-----------+| user | host|+------+-----------+| root | localhost |+------+-----------+1 row in set (0.00 sec)mysql> drop database jinc;Query OK, 0 rows affected (0.00 sec)