在CentOS linux中设置mysql root密码

时间:2014-11-25 20:31:05

标签: mysql linux centos

我在CentOS linux上安装了MySQL,并尝试设置root密码。为此,我采取了以下步骤:

1.) I opened the terminal and typed in `su - ` to run as root.  
2.) I then ran `mysql - u root`, which resulted in a lot of output and another prompt.  
3.) I then typed in `UPDATE mysql.user SET Password=PASSWORD('NewPassHere')  
    WHERE User='root';`  

但是第3步产生了以下错误:

-bash: syntax error near unexpected token `('

当我将第3步更改为UPDATE mysql.user SET Password='NewPassHere' WHERE User='root';时,出现以下错误:

bash: UPDATE: command not found...

如何解决此错误,以便我可以成功在MySQL中设置root密码?


根据Chuck的建议,我尝试了以下内容,但得到了以下结果:

[root@localhost ~]# /usr/bin/mysqladmin -u root password 'newpwd'
/usr/bin/mysqladmin: connect to server at 'localhost' failed
error: 'Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)'
Check that mysqld is running and that the socket: '/var/lib/mysql/mysql.sock' exists!

以下两个命令也有以下两个结果:

[root@localhost ~]# sudo service mysqld status
Redirecting to /bin/systemctl status  mysqld.service
mysqld.service
   Loaded: not-found (Reason: No such file or directory)
   Active: inactive (dead)

[root@localhost ~]# sudo service mysqld start
Redirecting to /bin/systemctl start  mysqld.service
Failed to issue method call: Unit mysqld.service failed to load: No such file or directory.

答案:


此问题的解决方案是使用yum remove删除mysql,然后明确地关注the steps in this tutorial。但是,我在下面将Chuck的答案标记为已被接受,因为他花了很多时间研究这个问题。

3 个答案:

答案 0 :(得分:2)

我通常使用mysqladmin来设置root密码。在CENTOS 6上,尝试:

/usr/bin/mysqladmin -u root password 'new-password'

这假设您为全新安装设置root密码,并且目前根密码不存在。如果您已经拥有root密码,则需要将-p附加到该命令的末尾并输入当前的mysql root密码。

请注意,这只会为root用户@ localhost设置密码。一旦您可以登录到mysql,您应该运行查询以查看实际存在多少root用户。您可能应该看到至少两个(root @ localhost,root @127.0.0.1),具体取决于版本和平台。您需要单独为每个主机设置root密码。从mysql命令行输入:

SELECT user, host FROM mysql.user WHERE user = 'root';

然后您可以再次注销mysql并使用mysqladmin命令设置所有密码,这次使用-h标志来指定主机:

/usr/bin/mysqladmin -u root password 'new-password' -h127.0.0.1

答案 1 :(得分:1)

我看到OP已经重新安装了mysql,但是我想在CentOS上提一下你可以使用一个很好的命令来保护你的安装生产并在这个过程中设置mysql root密码。命令是:mysql_secure_installation 这是它的工作原理

首先使用yum

安装mysql
[root@mail ~]# yum install mysql-server

然后将mysql设置为在启动时运行并启动mysql

[root@mail ~]# chkconfig mysqld on
[root@mail ~]# service mysqld start

然后运行mysql安装安装这里是示例输出: (首次运行时,mysql root密码为空,所以只需输入第一个问题)

[root@mail ~]# mysql_secure_installation
Enter current password for root (enter for none):
OK, successfully used password, moving on...
Set root password? [Y/n] y
New password:
Re-enter new password:
Password updated successfully!
Remove anonymous users? [Y/n] y
... Success!
Disallow root login remotely? [Y/n] n
... skipping.
Remove test database and access to it? [Y/n] y
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!
Reload privilege tables now? [Y/n] y
... Success!
Cleaning up...
All done!  If you've completed all of the above steps, your MySQL
installation should now be secure.
Thanks for using MySQL!

答案 2 :(得分:0)

使用mysql,你可以:

1)将你连接到mysql

 mysql -u root -p

2)运行mysql命令:

use mysql

3)运行mysql命令:

update user set authentication_string=password('NEWPASSWORD') where user='root';

4)运行mysql命令:

flush privileges;

5)运行mysql命令:

quit

6)现在您可以无需密码连接

mysql -u root