【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"));

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM