【问题标题】:setting mysql root password in CentOS linux在 CentOS linux 中设置 mysql root 密码
【发布时间】:2015-01-24 00:16:34
【问题描述】:

我在 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 的回答标记为已接受,因为他花了很多时间研究这个问题。

【问题讨论】:

    标签: mysql linux centos


    【解决方案1】:

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

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

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

    请注意,这只会为用户 root@localhost 设置密码。一旦您可以登录 mysql,您应该运行查询以查看实际存在多少根用户。根据版本和平台,您可能应该至少看到两个(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
    

    【讨论】:

    • 你确定mysql服务器已经启动并运行了吗? sudo service mysqld status 会显示它是否正在运行,sudo service mysqld start 应该启动它,如果它没有运行
    • 嗯...你是如何安装mysql的,你确定它已经安装了吗?从终端尝试which mysql 以确保它已安装。如果您通过 yum 安装,服务调用应该会自动设置。如果您通过其他方法安装,则可能需要手动设置。 shell> cp mysql.server /etc/init.d/mysql; chmod +x /etc/init.d/mysql 应该使上述命令起作用。您可能还想在这里查看:dev.mysql.com/doc/refman/5.0/en/automatic-start.html
    • 对不起,我从mysql的网站复制了创建init.d脚本的代码,它包含了shell>提示。它只是标准终端提示符的占位符,没有任何意义。只需开始从cp 部分复制命令即可。
    • 嗯...我的想法已经不多了。这是没有数据的全新 mysql 安装吗?我猜你安装了 mysql 客户端,但没有安装 mysql 服务器,虽然我不确定。 sudo yum install mysql-server 将安装 mysql-server 并设置所有内容,但您可能需要确保尚未安装它。
    • 不知道您最初是如何安装它的。在 CentOS 7 上,本指南在设置一切方面看起来非常有用:digitalocean.com/community/tutorials/…
    【解决方案2】:

    我看到 OP 已经重新安装了 mysql,但我想提一下,在 CentOS 上,您可以使用一个很棒的命令来保护您的生产安装并在此过程中设置 mysql 根密码。命令是: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!
    

    【讨论】:

    • 感谢您和 +1 抽出宝贵时间回答。我有另一个帖子。你也愿意帮助我吗?这是链接:stackoverflow.com/questions/27138568/…
    • 我会尝试检查我的答案,如果你不能得到它,请告诉我,因为 vi 可能很棘手
    【解决方案3】:

    使用 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-20
      • 2015-07-10
      • 2021-04-29
      • 2020-08-09
      • 2018-06-02
      • 1970-01-01
      • 2023-03-16
      相关资源
      最近更新 更多