一、操作步骤

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

 

你投入得越多,就能得到越多得价值