在CentOS linux中设置mysql根密码
我在CentOS linux上安装了MySQL,正在尝试设置根密码。为此,我采取了以下步骤:在CentOS linux中设置mysql根密码,mysql,linux,centos,Mysql,Linux,Centos,我在CentOS linux上安装了MySQL,正在尝试设置根密码。为此,我采取了以下步骤: 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 Passwo
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 `('
bash: UPDATE: command not found...
当我将步骤3更改为updatemysql.user SET Password='NewPassHere'WHERE user='root'时代码>,我得到以下错误:
-bash: syntax error near unexpected token `('
bash: UPDATE: command not found...
如何解决此错误,以便在MySQL中成功设置根密码?
根据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.
以下两个命令还具有以下两个结果:
[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,然后显式地执行。然而,我在下面将Chuck的答案标记为已接受,因为他花了太多时间研究这个问题。我通常使用mysqladmin设置root密码。在CENTOS 6上,尝试:
/usr/bin/mysqladmin -u root password 'new-password'
这假设您正在为全新安装设置根密码,而根密码当前不存在。如果您已经有了根密码,那么需要在该命令的末尾追加-p
,并输入当前的mysql根密码
请注意,这将仅为用户设置密码root@localhost. 登录mysql后,应该运行一个查询,查看实际存在多少根用户。你可能会看到至少两个(root@localhost, root@127.0.0.1)取决于版本和平台。您需要分别为每个主机设置根密码。在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
我看到OP已经重新安装了mysql,但我想说的是,在CentOS上,您可以使用一个非常好的命令来保护您的生产安装安全,并在此过程中设置mysql根密码。命令是:mysql\u secure\u installation
下面是它的工作原理
首先使用yum或者up2date安装mysql
[root@mail ~]# yum install mysql-server
然后将mysql设置为在启动时运行,然后启动mysql
[root@mail ~]# chkconfig mysqld on
[root@mail ~]# service mysqld start
然后运行mysql安全安装,以下是示例输出:
(第一次运行mysql时,根密码为空,所以第一个问题只需按enter键)
使用mysql,您可以:
1) 将您连接到mysql
mysql -u root -p
2) 运行mysql命令:
use mysql
update user set authentication_string=password('NEWPASSWORD') where user='root';
flush privileges;
quit
3) 运行mysql命令:
use mysql
update user set authentication_string=password('NEWPASSWORD') where user='root';
flush privileges;
quit
4) 运行mysql命令:
use mysql
update user set authentication_string=password('NEWPASSWORD') where user='root';
flush privileges;
quit
5) 运行mysql命令:
use mysql
update user set authentication_string=password('NEWPASSWORD') where user='root';
flush privileges;
quit
6) 现在,您可以使用无密码连接
mysql -u root
您确定mysql服务器已启动并正在运行吗sudo服务mysqld status
将显示它是否正在运行,如果它不在运行,sudo服务mysqld start
应该启动它。。。你是如何安装mysql的?你确定它已经安装了吗?尝试从终端安装哪个mysql
,以确保它已安装。如果通过yum安装,则应自动设置服务调用。如果通过其他方法安装,则可能需要手动设置shell>cp-mysql.server/etc/init.d/mysql;chmod+x/etc/init.d/mysql
应该可以让上述命令正常工作。您可能还想在这里检查一下:对不起,我从mysql的网站复制了创建init.d脚本的代码,其中包括shell>提示符。它只是标准终端提示符的占位符,没有任何意义。只需开始从cp
part.Hmm复制命令即可。。。我的想法都快用完了。这是一个没有数据的新mysql安装吗?我猜您安装了mysql客户端,但没有安装mysql服务器,尽管我不确定sudo-yum-install-mysql-server
将安装mysql-server并设置所有内容,但您可能希望确保尚未安装该服务器。不知道最初是如何安装的。在CentOS 7上,本指南对于设置所有内容非常有用:感谢您和+1花时间回答。我还有一个帖子。你也愿意帮我吗?这里是链接:我将尝试查看我的答案,如果你不能得到它,请告诉我,因为vi可能很棘手