찾다

 >  Q&A  >  본문

macos - mac 上用homebrew安装完mysql后,怎样使用密码连接数据库

我用如下命令安装了mysql:

brew install mysql

命令安装完mysql后,使用

mysql -u root -p

然后密码直接使用回车(空格)或者root都不行。

我百度了下,网上说用./mysqld_safe --skip-grant-tables &命令来跨过验证。

难道就没有办法能在某个配置文件中修改密码吗?或者有没有办法能自己设置密码呢?就像在window上安装mysql时可以自行设置密码一样。

题外话:为什么在终端中输入命令 mysql 会出现:

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

麻烦大家解答下,不甚感激!!!

PHP中文网PHP中文网2786일 전680

모든 응답(10)나는 대답할 것이다

  • 天蓬老师

    天蓬老师2017-04-17 13:32:27

    我通过不断的百度、谷歌和尝试,终于完美解决了,现记录下,方便后面的人。
    我的系统是 Mac OS X 10.11,准备安装的是 mysql 5.7.9。
    开始之前,假设你的brew已经安装好并可以使用。

    安装mysql,在终端输入如下命令并回车:

    brew install mysql

    等待大概5分钟(视网速而定)执行完毕后,会看到这一段提示:

    /usr/local/Cellar/mysql/5.7.9/bin/mysqld --initialize-insecure --user=comet
    ==> Caveats
    We've installed your MySQL database without a root password. To secure it run:
        mysql_secure_installation

    可以看到brew在安装的时候已经初始过mysql了(不用再像网上说的再执行mysql_install_db命令),且当前没有密码
    于是我按照它的提示,执行命令:

    mysql_secure_installation

    需要密码时,直接回车,出现了如下的错误:

    Error: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

    我在tmp目录下并没有看到mysql.sock这个文件。
    我猜想它是在mysql服务启动时才会创建,于是我提前执行了如下的命令:

    mysql.server start

    提示 .SUCCESS!
    这时tmp目录下有mysql.sock这个文件了,于是我继续执行 mysql_secure_installation

    cometdeMacBook-Pro:~ comet$ mysql_secure_installation
    
    Securing the MySQL server deployment.
    
    Connecting to MySQL using a blank password.
    
    VALIDATE PASSWORD PLUGIN can be used to test passwords
    and improve security. It checks the strength of password
    and allows the users to set only those passwords which are
    secure enough. Would you like to setup VALIDATE PASSWORD plugin?
    
    Press y|Y for Yes, any other key for No: N   // 这个选yes的话密码长度就必须要设置为8位以上,但我只想要6位的
    Please set the password for root here.
    
    New password:            // 设置密码
    
    Re-enter new password:     // 再一次确认密码
    By default, a MySQL installation has an anonymous user,
    allowing anyone to log into MySQL without having to have
    a user account created for them. This is intended only for
    testing, and to make the installation go a bit smoother.
    You should remove them before moving into a production
    environment.
    
    Remove anonymous users? (Press y|Y for Yes, any other key for No) : Y    // 移除不用密码的那个账户
    Success.
    
    
    Normally, root should only be allowed to connect from
    'localhost'. This ensures that someone cannot guess at
    the root password from the network.
    
    Disallow root login remotely? (Press y|Y for Yes, any other key for No) : n
    
     ... skipping.
    By default, MySQL comes with a database named 'test' that
    anyone can access. This is also intended only for testing,
    and should be removed before moving into a production
    environment.
    
    
    Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
     - Dropping test database...
    Success.
    
     - Removing privileges on test database...
    Success.
    
    Reloading the privilege tables will ensure that all changes
    made so far will take effect immediately.
    
    Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
    Success.
    
    All done!

    OK!搞定,可以使用mysql -u root -p 进行密码连接了
    PS: 我也是胡乱尝试的,也不知道这么做对不对,但至少达到我想要的了!!!

    회신하다
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-17 13:32:27

    mysql.server start

    회신하다
    0
  • 天蓬老师

    天蓬老师2017-04-17 13:32:27

    可能的问题有好几个:

    • 先确定mysql服务有没有启动,可以用 ps au | grep mysql 看看有无mysql的进程,如果没有,可以用 sudo mysqld_safe 启动。

    • 刚装完的mysql的是没有密码的, 直接在命令行输入 mysql 就可以进入到mysql交互模式。

    • 设置密码:mysqladmin -u root password "newpassword"

    • 或者换个方法,使用某个mysql的GUI管理工具登录试试,当然,通过GUI也是可以设置密码的,osx下推荐Sequel Pro这个软件,或者使用mysql官方的 mysqlbench

    회신하다
    0
  • 阿神

    阿神2017-04-17 13:32:27

    brew info mysql

    命令敲完,还要看提示啊

    회신하다
    0
  • PHPz

    PHPz2017-04-17 13:32:27

    安装命令执行完有提示
    安装完需启动MySQL mysql.server start
    默认安装完是没有密码,需要自己设置,参考楼上的答案

    회신하다
    0
  • 怪我咯

    怪我咯2017-04-17 13:32:27

    没有密码,当时搞半天,还是mariadb好用

    회신하다
    0
  • ringa_lee

    ringa_lee2017-04-17 13:32:27

    缺省没密码:

    mysql -u root
    

    회신하다
    0
  • 怪我咯

    怪我咯2017-04-17 13:32:27

    这个问题快把我搞疯了
    我的是ox el capitan系统
    brew 重装了mysql
    mysql.server start
    提示

    Starting MySQL
    . ERROR! The server quit without updating PID file (/usr/local/var/mysql/haotaodeMacBook-Pro.local.pid).
    
    mysql_secure_installation
    Enter password for user root: 
    Error: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) 
    

    执行mysql_secure_installation 输完密码就提示错误 没法进行完

    회신하다
    0
  • PHP中文网

    PHP中文网2017-04-17 13:32:27

    为毛我的是
    ➜ ~ mysql_secure_installation

    Securing the MySQL server deployment.

    Connecting to MySQL using a blank password.
    [1] 17759 segmentation fault mysql_secure_installation

    회신하다
    0
  • 阿神

    阿神2017-04-17 13:32:27

    执行

    > brew info mysql
    

    之后提示会有这么一段

    To have launchd start mysql now and restart at login:

    brew services start mysql
    

    Or, if you don't want/need a background service you can just run:

    mysql.server start
    

    按照自己的要求去启动mysql 就行了

    회신하다
    0
  • 취소회신하다