误删除了全部用户解决办法mysql
第一种方法(企业经常使用)sql
1.将数据库down掉shell
[root@db03 mysql]# /etc/init.d/mysqld stop Shutting down MySQL.. SUCCESS! [root@db03 mysql]# mysql ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/opt/mysql-5.6.44/tmp/mysql.sock' (2)
2.给/opt/目录下全部受mysql用户权限数据库
[root@db03 mysql]# chown -R mysql.mysql /opt/*
3.启动数据库socket
[root@db03 ~]# mysqld_safe --skip-grant-tables --skip-networking &
4.进入数据库ide
[root@db03 ~]# mysql
5.初始化ui
mysql> flush privileges; Query OK, 0 rows affected (0.01 sec)
6.设置数据库用户权限和密码this
mysql> grant all on *.* to root@'localhost' identified by '1' with grant option; Query OK, 0 rows affected (0.00 sec)
7.退出重启数据库rest
mysql> \q [root@db03 mysql]# /etc/init.d/mysqld restar
8.进入数据库查看code
mysql> select user,host from mysql.user; +------+-----------+ | user | host | +------+-----------+ | root | localhost | +------+-----------+ 1 row in set (0.01 sec)
第二种方法
1.down掉数据库
[root@db04 scripts]# /etc/init.d/mysqld stop Shutting down MySQL.. SUCCESS!
2.启动数据库
[root@db04 scripts]# mysqld_safe --skip-grant-tables --skip-networking & [1] 25934
3.进入数据库
[root@db04 scripts]# mysql
4.使用数据库
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
5.建立root用户
mysql> insert into mysql.user values ('localhost','root',PASSWORD('123'), -> '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');
6.查看用户
mysql> select user,host from mysql.user; +------+-----------+ | user | host | +------+-----------+ | root | localhost | +------+-----------+ 1 row in set (0.00 sec)
第三种方法(企业不可用,数据会丢失)
1.down掉数据库
[root@db04 ~]# /etc/init.d/mysqld stop Shutting down MySQL.. SUCCESS!
2.删除或更名/opt/mysql/data目录
[root@db04 opt]# cd mysql [root@db04 mysql]# mv data date [root@db04 mysql]# rm -fr data
3.初始化
[root@db04 scripts]# ./mysql_install_db --datadir=/opt/mysql/data --basedir=/opt/mysql -- user=mysql [root@db04 scripts]# echo $? 0
4.受权
[root@db04 scripts]# chown -R mysql.mysql /opt/*
5.启动数据库
[root@db04 scripts]# /etc/init.d/mysqld start Starting MySQL.Logging to '/opt/mysql/data/db04.err'. SUCCESS!
6.进入数据库查看
[root@db04 scripts]# mysql mysql> select user,host from mysql.user; +------+-----------+ | user | host | +------+-----------+ | root | % | | root | 127.0.0.1 | | root | ::1 | | | db02 | | root | db02 | | | localhost | | root | localhost | +------+-----------+ 7 rows in set (0.00 sec)