在 macOS 上 brew install mysql

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4359131/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 17:54:57  来源:igfitidea点击:

brew install mysql on macOS

mysqlmacoshomebrewmysql-error-1045

提问by nikola

I'm trying to setup up MySQL on mac os 10.6 using Homebrew by brew install mysql 5.1.52.

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

Everything goes well and I am also successful with the mysql_install_db.
However when I try to connect to the server using:

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

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

I get:

我得到:

/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)'

I've tried to access mysqladmin or mysql using -u root -prootas well,
but it doesn't work with or without password.

我也尝试过访问mysqladmin or mysql using -u root -proot
但无论是否有密码都无法使用。

This is a brand new installation on a brand new machine and as far as I know the new installation must be accessible without a root password. I also tried:

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

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

but I also get

但我也得到

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

回答by Lorin Rivers

I think one can end up in this position with older versions of mysql already installed. I had the same problem and none of the above solutions worked for me. I fixed it thus:

我认为如果已经安装了旧版本的 mysql,最终可能会处于这个位置。我遇到了同样的问题,上述解决方案都不适合我。我是这样修复的:

Used brew's remove& cleanupcommands, unloaded the launchctlscript, then deleted the mysql directory in /usr/local/var, deleted my existing /etc/my.cnf(leave that one up to you, should it apply) and launchctl plist

使用 brew's remove&cleanup命令,卸载launchctl脚本,然后删除 mysql 目录中的 mysql 目录/usr/local/var,删除我现有的/etc/my.cnf(留下那个由你决定,如果它适用)和 launchctl plist

Updated the string for the plist. Note also your alternate security script directory will be based on which version of MySQL you are installing.

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

Step-by-step:

一步步:

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

I then started from scratch:

然后我从头开始:

  1. installed mysql with brew install mysql
  2. ran the commands brew suggested: (see note: below)

    unset TMPDIR
    
    mysql_install_db --verbose --user=`whoami` --basedir="$(brew --prefix mysql)" --datadir=/usr/local/var/mysql --tmpdir=/tmp
    
  3. Start mysql with mysql.server startcommand, to be able to log on it

  4. Used the alternate security script:

    /usr/local/Cellar/mysql/5.5.10/bin/mysql_secure_installation
    
  5. Followed the launchctlsection from the brew package script output such as,

    #start
    launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
    
    #stop
    launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
    
  1. 安装了mysql brew install mysql
  2. 运行 brew 建议的命令:(见注释:下面)

    unset TMPDIR
    
    mysql_install_db --verbose --user=`whoami` --basedir="$(brew --prefix mysql)" --datadir=/usr/local/var/mysql --tmpdir=/tmp
    
  3. mysql.server start命令启动mysql ,就可以登录了

  4. 使用备用安全脚本:

    /usr/local/Cellar/mysql/5.5.10/bin/mysql_secure_installation
    
  5. 遵循launchctlbrew 包脚本输出中的部分,例如,

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

Note:the --forcebit on brew cleanupwill also cleanup outdated kegs, think it's a new-ish homebrew feature.

注意:--forcebrew cleanup也将清理过时的小桶,认为这是一个新的自制功能。

Note the second:a commenter says step 2 is not required. I don't want to test it, so YMMV!

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

回答by corysimmons

Here are detailed instructions combining getting rid of all MySQL from your Mac then installing it The Brew Way as Sedorner wrote above:

以下是详细说明,结合从您的 Mac 中删除所有 MySQL,然后按照 Sedorner 上面写的 The Brew Way 安装它:

Remove MySQL completely per The Tech Lab

根据技术实验室完全删除 MySQL

  • ps -ax | grep mysql
  • stop and killany MySQL processes
  • 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
  • edit /etc/hostconfigand remove the line 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*
  • sudo rm -rf /tmp/mysql*
  • try to run mysql, it shouldn't work
  • 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*
  • sudo rm -rf /tmp/mysql*
  • 尝试运行mysql,它不应该工作


Brew install MySQL per user Sedorner from this StackOverflowanswer

从这个StackOverflow答案中为每个用户 Sedorner Brew 安装 MySQL

  • brew doctorand fix any errors
  • brew remove mysql
  • brew cleanup
  • brew update
  • brew install mysql
  • unset TMPDIR

    mysql_install_db --verbose --user=`whoami` --basedir="$(brew --prefix mysql)" --datadir=/usr/local/var/mysql --tmpdir=/tmp # whoami is executed inline
    
  • mysql.server start

  • run the commands Brew suggests, add MySQL to launchctlso it automatically launches at startup
  • brew doctor并修复任何错误
  • brew remove mysql
  • brew cleanup
  • brew update
  • brew install mysql
  • unset TMPDIR

    mysql_install_db --verbose --user=`whoami` --basedir="$(brew --prefix mysql)" --datadir=/usr/local/var/mysql --tmpdir=/tmp # whoami is executed inline
    
  • mysql.server start

  • 运行 Brew 建议的命令,将 MySQL 添加到其中launchctl以便它在启动时自动启动

mysqlshould now work and be running all the time as expected

mysql现在应该可以正常工作并按预期一直运行

Godspeed.

神速。

回答by Alistair McMillan

Had the same problem. Seems like there is something wrong with the set up instructions or the initial tables that are being created. This is how I got mysqld running on my machine.

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

If the mysqld server is already running on your Mac, stop it first with:

如果 mysqld 服务器已经在您的 Mac 上运行,请先停止它:

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

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

Start the mysqld server with the following command which lets anyone log in with full permissions.

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

mysqld_safe --skip-grant-tables

mysqld_safe --skip-grant-tables

Then run mysql -u rootwhich should now let you log in successfully without a password. The following command should reset all the root passwords.

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

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

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

Now if you kill the running copy of mysqld_safe and start it up again without the skip-grant-tables option, you should be able to log in with mysql -u root -pand the new password you just set.

现在,如果您终止正在运行的 mysqld_safe 副本并重新启动它,而没有使用 skip-grant-tables 选项,您应该能够使用mysql -u root -p刚刚设置的新密码登录。

回答by mrucci

If brew installed MySQL 5.7, the process is a bit different than for previous versions. In order to reset the root password, proceed as follows:

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

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

A temporary password will be printed to the console and it can only be used for updating the root password:

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

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

回答by Darren Newton

Okay I had the same issue and solved it. For some reason the mysql_secure_installation script doesn't work out of the box when using Homebrew to install mysql, so I did it manually. On the CLI enter:

好的,我遇到了同样的问题并解决了它。出于某种原因,在使用 Homebrew 安装 mysql 时,mysql_secure_installation 脚本无法开箱即用,所以我手动完成了。在 CLI 上输入

mysql -u root

That should get you into mysql. Now do the following (taken from mysql_secure_installation):

那应该让你进入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;

Now exit and get back into mysql with: mysql -u root -p

现在退出并返回到mysql: mysql -u root -p

回答by Mark

I had the same problem just now. If you brew info mysqland follow the steps it looks like the root password should be new-passwordif I remember correctly. I was seeing the same thing you are seeing. This articlehelped me the most.

我刚才遇到了同样的问题。如果您brew info mysql按照步骤操作,new-password如果我没记错的话,root 密码应该是这样的。我看到的和你看到的一样。这篇文章对我帮助最大。

It turned out I didn't have anyaccounts created for me. When I logged in after running mysqld_safeand did select * from user;no rows were returned. I opened the MySQLWorkbench with the mysqld_saferunning and added a rootaccount with all the privs I expected. This are working well for me now.

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

回答by sudo rm -rf slash

If mysql is already installed

如果已经安装了mysql

Stop mysql completely.

完全停止mysql。

  1. mysql.server stop<-- may need editing based on your version
  2. ps -ef | grep mysql<-- lists processes with mysql in their name
  3. kill [PID]<-- kill the processes by PID
  1. mysql.server stop<-- 可能需要根据您的版本进行编辑
  2. ps -ef | grep mysql<-- 列出名称中带有 mysql 的进程
  3. kill [PID]<-- 通过 PID 杀死进程

Remove files. Instructions above are good. I'll add:

删除文件。上面的说明很好。我来补充:

  1. sudo find /. -name "*mysql*"
  2. Using your judgement, rm -rfthese files. Note that many programs have drivers for mysql which you do not want to remove. For example, don't delete stuff in a PHP install's directory. Do remove stuff in its own mysql directory.
  1. sudo find /. -name "*mysql*"
  2. 根据您的判断,rm -rf这些文件。请注意,许多程序都有您不想删除的 mysql 驱动程序。例如,不要删除 PHP 安装目录中的内容。删除它自己的 mysql 目录中的东西。

Install

安装

Hopefully you have homebrew. If not, download it.

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

I like to run brew as root, but I don't think you have to. Edit 2018: you can't run brew as root anymore

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

  1. sudo brew update
  2. sudo brew install cmake<-- dependency for mysql, useful
  3. sudo brew install openssl<-- dependency for mysql, useful
  4. sudo brew info mysql<-- skim through this... it gives you some idea of what's coming next
  5. sudo brew install mysql --with-embedded; say done<-- Installs mysql with the embedded server. Tells you when it finishes (my install took 10 minutes)
  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 分钟)

Afterwards

然后

  1. sudo chown -R mysql /usr/local/var/mysql/<-- mysql wouldn't work for me until I ran this command
  2. sudo mysql.server start<-- once again, the exact syntax may vary
  3. Create users in mysql (http://dev.mysql.com/doc/refman/5.7/en/create-user.html). Remember to add a password for the root user.
  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用户添加密码。

回答by Nicholas

TL;DR

TL; 博士

MySQL server might not be running after installation with Brew. Try brew services start mysqlor just mysql.server startif you don't want MySQL to run as a background service.

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

Full Story:

全文:

I just installed MySQL (stable) 5.7.17 on a new MacBook Pro running Sierra and also got an error when running mysql_secure_installation:

我刚刚在运行 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)

Say what?

说什么?

According to the installation info from Brew, mysql_secure_installationshould prompt me to... secure the installation. I figured the MySQL server might not be running and rightly so. Running brew services start mysqland then mysql_secure_installationworked like a charm.

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

回答by Felipe Duardo

brew info mysql

酿造信息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

mysql.service 启动

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

or mysql -u root

或 mysql -u root

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

I'm looking for a solution for some time but I can not solve my problem. I tried several solutions in stackoverflow.com but no this helping me.

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

回答by XMen

Try by giving Grant permission Command of mysql

尝试通过授予 mysql 的授予权限命令