进入mysql后,输入:
select user(); +--------+ | user() | +--------+ | root@ | +--------+发现mysql一开始没有root用户
先说这个问题产生的影响,这个跟解题有关,有必要说明,我的user表没有任何一个用户,包括root,所以一开始我要给权限,方便后续操作:
vim /etc/my.cnf skip-grant-tables #在[mysqld]下面添加这一行,忽略权限表然后重启mysql:
/etc/init.d/mysqld restart开始解题: 先说解决步骤: 1.创建root用户 2.给予root所有权限 过程: 1.创建root用户:
create user 'root'@'localhost' identified by '123457';localhost表示本地,mysql登入的时候,不用指定ip登入
此步骤可能会报以下错误,没报错的跳过(直接到权限那一步),用一下方法解决:
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';再次重新创建用户:
create user 'root'@'localhost' identified by '123457';结果没有再报错,root用户创建成功。 下面赋予root权限:
GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION; #赋予所有库所有表操作权限 flush privileges; exit到这一步没有报错,表明已经成功了,不过要把最开始的配置文件恢复: vim /etc/my.cnf 删除配置文件中的:
skip-grant-tables退出,重启mysql:
/etc/init.d/mysqld restart最后,测试:
mysql -uroot -p123457 #可以登入的话,表示前面的操作没问题然后:
show databases; #出来下面四张表,表示:恭喜你成功了!hh +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | +--------------------+