brew在macOS上安装mysql

时间:2010-12-05 13:57:49

标签: mysql macos homebrew mysql-error-1045

我正在尝试使用Homebrew brew install mysql 5.1.52在mac os 10.6上设置MySQL。

一切顺利,我也mysql_install_db成功 但是,当我尝试使用以下命令连接到服务器时:

/usr/local/Cellar/mysql/5.1.52/bin/mysqladmin -u root password 'mypass'

我明白了:

/usr/local/Cellar/mysql/5.1.52/bin/mysqladmin: connect to server at 'localhost' 
failed error: 'Access denied for user 'root'@'localhost' (using password: NO)'

我也试图访问mysqladmin or mysql using -u root -proot
但无论是否有密码都无效。

这是全新机器上的全新安装,据我所知,新安装必须无需root密码即可访问。我也尝试过:

/usr/local/Cellar/mysql/5.1.52/bin/mysql_secure_installation

但我也得到了

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

17 个答案:

答案 0 :(得分:643)

我认为已经安装了旧版本的mysql,最终可能会处于这个位置。我遇到了同样的问题,上述解决方案都没有对我有用。我这样修好了:

二手brew remove& cleanup命令,卸载了launchctl脚本,然后删除了/usr/local/var中的mysql目录,删除了我现有的/etc/my.cnf(如果适用的话,请将其留给您)和launchctl plist中

更新了plist的字符串。另请注意,您的备用安全脚本目录将基于您要安装的MySQL版本。

步骤一步:

brew remove mysql

brew cleanup

launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist

rm ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist

sudo rm -rf /usr/local/var/mysql

然后我从头开始:

  1. 使用brew install mysql
  2. 安装了mysql
  3. 运行brew建议的命令:(参见下面的注释)

    unset TMPDIR
    
    mysql_install_db --verbose --user=`whoami` --basedir="$(brew --prefix mysql)" --datadir=/usr/local/var/mysql --tmpdir=/tmp
    
  4. 使用mysql.server start命令启动mysql,以便能够登录

  5. 使用备用安全脚本:

    /usr/local/Cellar/mysql/5.5.10/bin/mysql_secure_installation
    
  6. 跟随brew包脚本输出中的launchctl部分,例如

    #start
    launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
    
    #stop
    launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
    
  7. 动臂。

  8. 希望能帮助别人!

    注意: --force上的brew cleanup位也会清理过时的小桶,认为这是一个新的自制功能。

    注意第二个:评论者说不需要第2步。我不想测试它,所以YMMV!

答案 1 :(得分:46)

以下是结合从Mac上删除所​​有MySQL然后安装它的详细说明以上所述的Brew Way as Sedorner:

The Tech Lab

完全删除MySQL
  • ps -ax | grep mysql
  • 停止并kill任何MySQL进程
  • sudo rm /usr/local/mysql
  • sudo rm -rf /usr/local/var/mysql
  • sudo rm -rf /usr/local/mysql*
  • sudo rm ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
  • sudo rm -rf /Library/StartupItems/MySQLCOM
  • sudo rm -rf /Library/PreferencePanes/My*
  • launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
  • 修改/etc/hostconfig并删除第MYSQLCOM=-YES-
  • rm -rf ~/Library/PreferencePanes/My*
  • sudo rm -rf /Library/Receipts/mysql*
  • sudo rm -rf /Library/Receipts/MySQL*
  • sudo rm -rf /private/var/db/receipts/*mysql*
  • 尝试运行mysql,它不应该运行

从这个StackOverflow答案

中为每个用户Sedorner安装MySQL
  • brew doctor并修复任何错误
  • brew remove mysql
  • brew cleanup
  • brew update
  • brew install mysql
  • unset TMPDIR
  • mysql_install_db --verbose --user= {WHOAMI {1}}
  • --basedir="$(brew --prefix mysql)" --datadir=/usr/local/var/mysql --tmpdir=/tmp
  • 运行Brew建议的命令,将MySQL添加到mysql.server start,以便它在启动时自动启动

launchctl现在应该可以正常运行并按预期运行

一帆风顺。

答案 2 :(得分:42)

有同样的问题。似乎设置说明或正在创建的初始表有问题。这就是我在我的机器上运行mysqld的方法。

如果mysqld服务器已在Mac上运行,请先使用以下命令停止:

launchctl unload -w ~/Library/LaunchAgents/com.mysql.mysqld.plist

使用以下命令启动mysqld服务器,该命令允许任何人以完全权限登录。

mysqld_safe --skip-grant-tables

然后运行mysql -u root,现在可以让您在没有密码的情况下成功登录。以下命令应重置所有root密码。

UPDATE mysql.user SET Password=PASSWORD('NewPassword') WHERE User='root'; FLUSH PRIVILEGES;

现在,如果您终止mysqld_safe的运行副本并在没有skip-grant-tables选项的情况下再次启动它,您应该可以使用mysql -u root -p和刚刚设置的新密码登录。

答案 3 :(得分:13)

如果brew安装了MySQL 5.7,则该过程与以前的版本略有不同。 要重置root密码,请按以下步骤操作:

sudo rm -rf /usr/local/var/mysql
mysqld --initialize

临时密码将打印到控制台,它只能用于更新root密码:

mysql.server start
echo "ALTER USER 'root'@'localhost' IDENTIFIED BY 'my-new-password';" | mysql -uroot --password=TEMPORARY_PASSWORD

答案 4 :(得分:8)

好的,我有同样的问题并解决了它。出于某种原因,当使用Homebrew安装mysql时,mysql_secure_installation脚本不能正常工作,所以我手动完成了。 在CLI上输入

mysql -u root

那应该让你进入mysql。现在执行以下操作(取自mysql_secure_installation):

UPDATE mysql.user SET Password=PASSWORD('your_new_pass') WHERE User='root';
DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1');
DELETE FROM mysql.user WHERE User='';
DELETE FROM mysql.db WHERE Db='test' OR Db='test\\_%'
DROP DATABASE test;
FLUSH PRIVILEGES;

现在退出并使用:mysql -u root -p

返回mysql

答案 5 :(得分:8)

我刚才遇到了同样的问题。如果您brew info mysql并按照步骤操作,如果我没记错的话,根密码应该是new-password。我看到你看到的同样的事情。 This article给了我最多的帮助。

事实证明我没有为我创建任何帐户。当我在运行mysqld_safe后登录并且select * from user;没有返回任何行时。我打开了运行mysqld_safe的MySQLWorkbench,并添加了一个root帐户,其中包含我预期的所有权限。这对我来说效果很好。

答案 6 :(得分:6)

如果已经安装了mysql

完全停止mysql。

  1. mysql.server stop< - 可能需要根据您的版本进行修改
  2. ps -ef | grep mysql< - 列出名称为mysql的进程
  3. kill [PID]< - 按PID终止进程
  4. 删除文件。上面的说明很好。我将添加:

    1. sudo find /. -name "*mysql*"
    2. 使用您的判断,rm -rf这些文件。请注意,许多程序都有mysql驱动程序,您不想删除它们。例如,不要删除PHP安装目录中的内容。请删除自己的mysql目录中的内容。
    3. 安装

      希望你有自制软件。如果没有,请下载它。

      我喜欢以root身份运行brew,但我不认为你必须这样做。编辑2018:您不能再以root身份运行brew

      1. sudo brew update
      2. sudo brew install cmake< - 依赖于mysql,很有用
      3. sudo brew install openssl< - 依赖于mysql,很有用
      4. sudo brew info mysql< - 浏览一遍......它让您了解接下来会发生什么
      5. sudo brew install mysql --with-embedded; say done< - 使用嵌入式服务器安装mysql。告诉你什么时候完成(我的安装需要10分钟)
      6. 之后

        1. sudo chown -R mysql /usr/local/var/mysql/< - mysql在我运行此命令之前不会为我工作
        2. sudo mysql.server start< - 再一次,确切的语法可能会有所不同
        3. 在mysql(http://dev.mysql.com/doc/refman/5.7/en/create-user.html)中创建用户。请记住为root用户添加密码。

答案 7 :(得分:3)

brew info mysql

mysql: stable 5.6.12 (bottled)
http://dev.mysql.com/doc/refman/5.6/en/
Conflicts with: mariadb, mysql-cluster, percona-server
/usr/local/Cellar/mysql/5.6.12 (9363 files, 353M) *
  Poured from bottle
From: https://github.com/mxcl/homebrew/commits/master/Library/Formula/mysql.rb
==> Dependencies
Build: cmake
==> Options
--enable-debug
    Build with debug support
--enable-local-infile
    Build with local infile loading support
--enable-memcached
    Enable innodb-memcached support
--universal
    Build a universal binary
--with-archive-storage-engine
    Compile with the ARCHIVE storage engine enabled
--with-blackhole-storage-engine
    Compile with the BLACKHOLE storage engine enabled
--with-embedded
    Build the embedded server
--with-libedit
    Compile with editline wrapper instead of readline
--with-tests
    Build with unit tests
==> Caveats
A "/etc/my.cnf" from another install may interfere with a Homebrew-built
server starting up correctly.

To connect:
    mysql -uroot

To reload mysql after an upgrade:
    launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
    launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist

mysql.service start

. ERROR! The server quit without updating PID file (/var/run/mysqld/mysqld.pid).

或mysql -u root

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

我正在寻找一段时间的解决方案,但我无法解决我的问题。我在stackoverflow.com上尝试了几个解决方案,但没有这个帮助我。

答案 8 :(得分:2)

<强> TL; DR

使用Brew安装后,MySQL服务器可能无法运行。如果您不希望MySQL作为后台服务运行,请尝试brew services start mysqlmysql.server start

全文:

我刚刚在运行Sierra的新MacBook Pro上安装了MySQL(稳定版)5.7.17,并在运行mysql_secure_installation时遇到错误:

Securing the MySQL server deployment.

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

说什么?

根据Brew的安装信息,mysql_secure_installation应该提示我......确保安装。我认为MySQL服务器可能没有正常运行。运行brew services start mysql然后mysql_secure_installation就像魅力一样。

答案 9 :(得分:1)

自酿

  1. 首先,请确保您已安装自制软件
  2. 运行brew doctor并解决自制软件要修复的所有问题
  3. 运行brew install mysql
  4. 运行brew services restart mysql
  5. 运行mysql.server start
  6. 运行mysql_secure_installation

答案 10 :(得分:1)

尝试给予Grant权限mysql命令

答案 11 :(得分:1)

这是MySQL 5.7的更新

bash --version
GNU bash, version 4.4.12(1)-release (x86_64-apple-darwin17.0.0)
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later 

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

#========================================
brew --version
Homebrew 1.7.6
Homebrew/homebrew-core (git revision eeb08; last commit 2018-09-27)
Homebrew/homebrew-cask (git revision c9f62; last commit 2018-09-27)

#========================================
mysql --version
mysql  Ver 14.14 Distrib 5.7.23, for osx10.13 (x86_64) using  EditLine wrapper

#========================================
system_profiler SPSoftwareDataType
Software:

    System Software Overview:

      System Version: macOS 10.13.3 (17D47)
      Kernel Version: Darwin 17.4.0
      Boot Volume: Macintosh HD
      Boot Mode: Normal
      Computer Name: EdisonMacHomeBj
      User Name: Edison (edison)
      Secure Virtual Memory: Enabled
      System Integrity Protection: Disabled
      Time since boot: 6 days 23:13
brew remove mysql@5.7
brew cleanup
mv /usr/local/var/mysql /usr/local/var/mysql.bak
brew install mysql@5.7
rm -rf /usr/local/var/mysql

#========================================
mysqld --initialize
2018-09-28T04:54:06.526061Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-09-28T04:54:06.542625Z 0 [Warning] Setting lower_case_table_names=2 because file system for /usr/local/var/mysql/ is case insensitive
2018-09-28T04:54:07.096637Z 0 [Warning] InnoDB: New log files created, LSN=45790
2018-09-28T04:54:07.132950Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2018-09-28T04:54:07.196824Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 87cf2f10-c2da-11e8-ac2d-ba163df10130.
2018-09-28T04:54:07.224871Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2018-09-28T04:54:07.366688Z 0 [Warning] CA certificate ca.pem is self signed.
2018-09-28T04:54:07.457954Z 1 [Note] A temporary password is generated for root@localhost: kq3K=JR8;GqZ

#========================================
mysql_secure_installation -uroot -p"kq3K=JR8;GqZ"
mysql_secure_installation: [Warning] Using a password on the command line interface can be insecure.

Securing the MySQL server deployment.


The existing password for the user account root has expired. Please set a new password.

New password:

Re-enter new 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
Using existing password for root.
Change the password for root ? ((Press y|Y for Yes, any other key for No) : y

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) : n

 ... skipping.
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!

答案 12 :(得分:1)

当使用brew与最新版本的mysql和yosemite时,上述答案(或我在其他地方看到的任何答案中的任何一个)都没有为我工作。我最终通过brew安装了一个不同的mysql版本。

通过说(例如)

来指定旧版本
brew install mysql56

为我工作。希望这有助于某人。这是一个令人沮丧的问题,我觉得我被永远困住了。

答案 13 :(得分:0)

&#34;基本路径&#34; for Mysql存储在/etc/my.cnf中,当你进行brew升级时,它不会更新。只需打开它并更改基础值

例如,更改此内容:

[mysqld]
basedir=/Users/3st/homebrew/Cellar/mysql/5.6.13

指向新版本:

[mysqld]
basedir=/Users/3st/homebrew/Cellar/mysql/5.6.19

使用以下命令重启mysql:

mysql.server start

答案 14 :(得分:0)

只需在以前的答案中添加一些内容-从MySql 5.6升级到MySql 8.0时,我按照此处提供的步骤进行了干净的卸载,但出现了以下错误

2019-11-05T07:57:31.359304Z 0 [ERROR] [MY-000077] [Server] /usr/local/Cellar/mysql/8.0.18/bin/mysqld: Error while setting value 'ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' to 'sql_mode'.
2019-11-05T07:57:31.359330Z 0 [ERROR] [MY-013236] [Server] The designated data directory /usr/local/var/mysql is unusable. You can remove all files that the server added to it.
2019-11-05T07:57:31.359413Z 0 [ERROR] [MY-010119] [Server] Aborting
2019-11-05T07:57:31.359514Z 0 [Note] [MY-010120] [Server] Binlog end

花一些时间弄清楚它。在这里找到了线索: https://discourse.brew.sh/t/clean-removal-of-mysql/2251

因此,我的问题的关键是在卸载后删除/usr/local/etc/my.cnf文件。 经过最后一步,MySql终于开始工作。

答案 15 :(得分:0)

尝试我为MariaDB提供的解决方案,它也可以与MySQL一起使用,这是一个很大的变化。

MacOSX homebrew mysql root password

简而言之,请尝试使用您的用户名登录!不是root。

尝试使用与MacOS帐户用户名相同的名称,例如约翰斯密特。

要以root用户身份登录,请发出:

mysql -u johnsmit

答案 16 :(得分:0)

我尝试重新启动mysql后遇到了同样的问题。

为方便起见,我在.profile中使用以下两个别名

alias mysql-stop='launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist'
alias mysql-start='launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist' 

停止mysql然后尝试重启后,我遇到了你遇到的问题。我查看了launchctl加载,它报告了“没有发现加载”错误。

快速搜索后我发现了这个..

http://www.daveoncode.com/2013/02/01/solve-mac-osx-launchctl-nothing-found-to-load-error/

所以我更新了mysql-start别名,如下所示

alias mysql-start='launchctl load -w -F ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist'

这解决了我可能对你有用的问题。