在Centos7上更改mysql root密码 [英] Change mysql root password on Centos7

查看:650
本文介绍了在Centos7上更改mysql root密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经安装mySQL在Centos7 vm,但我有问题使用root登录。我尝试登录没有密码或尝试任何默认的(如mysql,admin等)我看了my.cnf文件,没有密码。我试图更改密码通过停止服务,并重新启动它与 mysqld_safe --skip-grant-tables& 但我得到 mysqld_safe:command not found
我不知道该怎么做。

解决方案

您使用的是什么版本的mySQL?我使用的是5.7.10,并且以root的身份登录也有同样的问题。



有两个问题 - 为什么我不能以root身份登录? ,为什么我不能使用mysqld_safe来启动mySQL来重置root密码。



我在安装过程中没有设置root密码的答案,您可以通过运行


$来重置根密码



编辑 b $ b

  grep'temporary password'/var/log/mysqld.log 

http://dev.mysql.com /doc/refman/5.7/en/linux-installation-yum-repo.html







  1. systemd 现在用于查看mySQL而不是 mysqld_safe 获取 -bash:mysqld_safe:command not found 错误 - 尚未安装)


  2. c $ c> user 表结构已更改。


,您仍然使用 - skip-grant-tables 选项启动mySQL并更新用户表,它已改变。

  1。停止mysql:
systemctl stop mysqld

2.设置mySQL环境选项
systemctl set-environment MYSQLD_OPTS = - skip-grant-tables

3.启动mysql usig你刚刚设置的选项
systemctl start mysqld

4.以root登录
mysql -u root

5。使用这些mysql命令更新root用户密码
mysql> UPDATE mysql.user SET authentication_string = PASSWORD('MyNewPassword')
- > WHERE User ='root'AND Host ='localhost';
mysql> FLUSH特权;
mysql> quit

6.停止mysql
systemctl stop mysqld

7.取消设置mySQL envitroment选项,使其下次正常启动
systemctl unset-environment MYSQLD_OPTS

8.正常启动mysql:
systemctl start mysqld

尝试使用新密码登录:
7. mysql -u root -p






参考 >

正如 http:// dev.mysql.com/doc/refman/5.7/en/mysqld-safe.html


注意



从MySQL 5.7.6开始,对于使用RPM
分配的MySQL安装,服务器启动和关闭由systemd管理
几个Linux平台。在这些平台上,mysqld_safe不再安装
,因为它是不必要的。有关更多信息,请参阅
2.5.10节使用systemd管理MySQL服务器。


a href =http://dev.mysql.com/doc/refman/5.7/en/server-management-using-systemd.html> http://dev.mysql.com/doc/refman/5.7/en /server-management-using-systemd.html ,其中向页面底部提及 systemctl set-environment MYSQLD_OPTS =



密码重置命令位于 http: //dev.mysql.com/doc/refman/5.7/en/resetting-permissions.html


I have installed mySQL on a Centos7 vm but I have problems logging in with root. I tried logging in without password or tried any default ones (like mysql, admin etc) I looked in the my.cnf file and there's no password. I tried changing the password by stopping the service and restarting it with mysqld_safe --skip-grant-tables & but I get that mysqld_safe:command not found I have no idea what else to do. Any tips/ideas would be greatly appreciated!

解决方案

What version of mySQL are you using? I''m using 5.7.10 and had the same problem with logging on as root

There is 2 issues - why can't I log in as root to start with, and why can I not use 'mysqld_safe` to start mySQL to reset the root password.

I have no answer to setting up the root password during installation, but here's what you do to reset the root password

Edit the initial root password on install can be found by running

grep 'temporary password' /var/log/mysqld.log

http://dev.mysql.com/doc/refman/5.7/en/linux-installation-yum-repo.html


  1. systemd is now used to look after mySQL instead of mysqld_safe (which is why you get the -bash: mysqld_safe: command not found error - it's not installed)

  2. The user table structure has changed.

So to reset the root password, you still start mySQL with --skip-grant-tables options and update the user table, but how you do it has changed.

1. Stop mysql:
systemctl stop mysqld

2. Set the mySQL environment option 
systemctl set-environment MYSQLD_OPTS="--skip-grant-tables"

3. Start mysql usig the options you just set
systemctl start mysqld

4. Login as root
mysql -u root

5. Update the root user password with these mysql commands
mysql> UPDATE mysql.user SET authentication_string = PASSWORD('MyNewPassword')
    -> WHERE User = 'root' AND Host = 'localhost';
mysql> FLUSH PRIVILEGES;
mysql> quit

6. Stop mysql
systemctl stop mysqld

7. Unset the mySQL envitroment option so it starts normally next time
systemctl unset-environment MYSQLD_OPTS

8. Start mysql normally:
systemctl start mysqld

Try to login using your new password:
7. mysql -u root -p


Reference

As it says at http://dev.mysql.com/doc/refman/5.7/en/mysqld-safe.html,

Note

As of MySQL 5.7.6, for MySQL installation using an RPM distribution, server startup and shutdown is managed by systemd on several Linux platforms. On these platforms, mysqld_safe is no longer installed because it is unnecessary. For more information, see Section 2.5.10, "Managing MySQL Server with systemd".

Which takes you to http://dev.mysql.com/doc/refman/5.7/en/server-management-using-systemd.html where it mentions the systemctl set-environment MYSQLD_OPTS= towards the bottom of the page.

The password reset commands are at the bottom of http://dev.mysql.com/doc/refman/5.7/en/resetting-permissions.html

这篇关于在Centos7上更改mysql root密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆