【Mysql】Mysql root用户误删了或只剩下没有任何操作权限的用户怎么办
一、操作步骤
1、停止mysql服务;在mysql安装目录下找到mysqld.cnf;在mysqld.cnf中找到以下片段[mysqld];另起一行加入代码:skip-grant-tables 并保存
比如我的在:/etc/mysql/mysql.conf.d目录下的mysqld.cnf文件,修改后为:
root@c1c5dbe81b37:/etc/mysql/mysql.conf.d# cat mysqld.cnf # Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved. # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; version 2 of the License. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA # # The MySQL Server configuration file. # # For explanations see # http://dev.mysql.com/doc/mysql/en/server-system-variables.html [mysqld] skip-grant-tables pid-file = /var/run/mysqld/mysqld.pid socket = /var/run/mysqld/mysqld.sock datadir = /var/lib/mysql #log-error = /var/log/mysql/error.log # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0
2、启动mysql服务,并登录
2.1 有用户的情况
用用户名密码登录
2.2无用户的情况
直接可登录(无用户名和密码),然后加入root用户
INSERT INTO user (Host,User,Password) VALUES( 'localhost ', 'root ',password( '123456 '));
3、root用户设置权限
update user set Host='%',select_priv='y', insert_priv='y',update_priv='y',Alter_priv='y',delete_priv='y',create_priv='y',drop_priv='y',reload_priv='y',shutdown_priv='y',Process_priv='y',file_priv='y',grant_priv='y',References_priv='y',index_priv='y',create_user_priv='y',show_db_priv='y',super_priv='y',create_tmp_table_priv='y',Lock_tables_priv='y',execute_priv='y',repl_slave_priv='y',repl_client_priv='y',create_view_priv='y',show_view_priv='y',create_routine_priv='y',alter_routine_priv='y',create_user_priv='y' where user='root';commit;
4、把mysql.conf.d刚才加入的那行删除并重启服务
5、最后用root用户登录就可以了
二、其他有关命令
给用户授权
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456';
授权后刷新权限,使其生效 flush privileges; 查看mysql用户有哪些 select user,host from mysql.user;
更新用户密码 UPDATE mysql.user SET Password=PASSWORD('123') where USER='root';
删除用户 Delete FROM mysql.user Where User='root' and Host='%';
插入用户 insert into mysql.user(Host,User,Password) values("%","root",password("123456"));
相关文章
- 【MySQL数据库中删除所有外键的操作】(删除所有外键mysql)
- MySQL字符串切割技巧探索(mysql的字符串切割)
- MySQL的启动命令:从入门到精通(mysql的启动命令)
- MySQL数据库操作:攻克技术之路(操作mysql数据库)
- MySQL:查看从库连接状态(mysql查看从库状态)
- C语言操作MySQL数据库入门实践(c引用mysql)
- 使用Shell脚本操作MySQL数据库(shell连接mysql)
- MySQL分片:支持服务质量的无缝扩容(mysql分片)
- 快速高效:使用SQL脚本轻松导入MySQL数据库(sql脚本导入mysql)
- 用Shell轻松实现MySQL操作(shell执行mysql)
- 使用PHP连接MySQL数据库实现数据操作(php连接mysql类)
- MySQL操作指南:如何删除数据记录(mysql删除数据记录)
- MySQL:为新用户授予权限的指南(mysql给新用户授权)
- 25 Ways to Optimize MySQL for Multiple Queries(mysql多查询优化方法)
- 使用MySQL实现高效数据量统计技巧(mysql统计数量)
- 破解MySQL数据库之:Hint 优化技巧(mysql hint)
- 彻底解读MySQL修改密码命令,一步步来操作(mysql中修改密码命令)
- 提高数据库操作效率MySQL实现两表索引(mysql 两表索引)
- 技术分享MySQL如何查询两表相等的数据(mysql两表相等的数据)
- 用64位 Visual Basic 连接 MySQL 数据库(64位 vb mysql)
- 服务CMD下检查MySQL服务状态的方法(cmd中查看mysql)
- 防重复操作,MySQL中加唯一,数据更稳定(mysql中加唯一)
- MySQL非交互式操作指南(mysql 不交互)
- 如何使用MySQL上传表并管理数据(mysql上传表)
- MySQL中in子查询出现错误的问题解析(mysql不能in子查询)
- MySQL查询操作使用不等于0语句筛选数据(mysql不等于0语句)
- MySQL不支持JOIN操作解决方案了解一下(mysql不支持join)
- Redis驱动MySQL数据强劲协同(redis调度mysql)