首頁  >  問答  >  主體

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中文网2742 天前636

全部回覆(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 啟動

    回覆
    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

    確保 MySQL 伺服器部署的安全性。

    使用空白密碼連接到 MySQL。
    [1] 17759 分段錯誤 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
  • 取消回覆