1、mysql没有root用户
vim /etc/my.cnf
skip-grant-tables #在[mysqld]下面添加这一行,忽略权限表
2、重启mysql
/etc/init.d/mysqld restart
或
service mysql stop
service mysql start
3、 创建用户
create user 'root'@'localhost' identified by '123457';
可能会出现错误
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
执行
flush privileges;
再次创建用户
create user 'root'@'localhost' identified by '123457';
可能再次报错,执行:
drop user 'root'@'localhost';
再次重新创建用户,root创建成功
4、下面赋予root权限:
GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION; #赋予所有库所有表操作
grant all privileges on *.* to root@"%" identified by "123456" ;#赋予远程访问权限
权限
flush privileges;
exit
5、到这一步没有报错,表明已经成功了,不过要把最开始的配置文件恢复:
vim /etc/my.cnf
删除配置文件中的:
skip-grant-tables
退出,重启mysql,
最后,测试:
mysql -uroot -p123457 #可以登入的话,表示前面的操作没问题
show databases; #出来下面四张表,表示:恭喜你成功了!
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
6、修改用户密码
set password for 用户名@localhost = password('新密码');
7、select host,user from mysql.user; 查看用户的权限
select host,user from mysql.user;
8、撤销用户权限
REVOKE privilege ON databasename.tablename FROM 'username'@'host';