Home  >  Q&A  >  body text

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 days ago643

reply all(10)I'll reply

  • 天蓬老师

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

    Through constant Baidu, Google and attempts, I finally solved it perfectly. I will record it now for the convenience of others later.
    My system is Mac OS X 10.11, and I am going to install mysql 5.7.9.
    Before you begin, it is assumed that your brew is already installed and ready to use.

    Install mysql, enter the following command in the terminal and press Enter:

    brew install mysql

    Wait for about 5 minutes (depending on the network speed). After the execution is completed, you will see this prompt:

    /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

    You can see that brew has initialized mysql during installation (no need to execute the mysql_install_db command as mentioned on the Internet), and there is currently no password
    So I followed its prompts and executed the command:

    mysql_secure_installation

    When a password is required, press Enter directly, and the following error occurs:

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

    I did not see the mysql.sock file in the tmp directory.
    I guess it will be created when the mysql service is started, so I executed the following command in advance:

    mysql.server start

    Prompt .SUCCESS!
    At this time, there is the file mysql.sock in the tmp directory, so I continue to execute 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! Done, you can use mysql -u root -p to connect with password
    PS: I also tried randomly, and I don’t know if it is right, but at least it achieved what I wanted! ! !

    reply
    0
  • 伊谢尔伦

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

    mysql.server start

    reply
    0
  • 天蓬老师

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

    There are several possible questions:

    • First make sure the mysql service is started. You can use ps au | grep mysql to see if there is a mysql process. If not, you can use sudo mysqld_safe to start it.

    • The mysql you just installed does not have a password. Just enter mysql directly on the command line to enter the mysql interactive mode.

    • Set password: mysqladmin -u root password "newpassword"

    • Or try another method, use a mysql GUI management tool to log in. Of course, you can also set a password through the GUI. This software is recommended for osx, or use the official mysql Sequel Promysqlbench

    • reply
      0
  • 阿神

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

    brew info mysql

    After typing the command, you still need to read the prompts

    reply
    0
  • PHPz

    PHPz2017-04-17 13:32:27

    There will be a prompt after the installation command is executed
    After the installation, you need to start MySQL mysql.server start
    By default, there is no password after the installation, you need to set it yourself, refer to the answer upstairs

    reply
    0
  • 怪我咯

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

    I didn’t have a password. I worked on it for a long time, but mariadb was still easy to use

    reply
    0
  • ringa_lee

    ringa_lee2017-04-17 13:32:27

    Default no password:

    mysql -u root
    

    reply
    0
  • 怪我咯

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

    This problem is driving me crazy
    My system is ox el capitan
    Brew reinstalled mysql
    mysql.server start
    Prompt

    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) 
    

    When executing mysql_secure_installation, an error message appears after entering the password and the process cannot be completed

    reply
    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

    reply
    0
  • 阿神

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

    Execute

    > brew info mysql
    

    There will be a prompt like this

    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
    

    Just start mysql according to your requirements

    reply
    0
  • Cancelreply