ChatGPT解决这个技术问题 Extra ChatGPT

在 macOS 上冲泡安装 mysql

我正在尝试使用 brew install mysql 5.1.52 的 Homebrew 在 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)
另请参阅:apple.stackexchange.com/a/199747/152271 ...它不是 brew install,但是当这里没有任何答案时它对我有用
FWIW 我使用了 mysql -u root,它在这里工作:|

T
Trilarion

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

用过 brew 的 remove & cleanup 命令,卸载 launchctl 脚本,然后删除 /usr/local/var 中的 mysql 目录,删除我现有的 /etc/my.cnf(如果适用,请自行决定)并启动ctl 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

然后我从头开始:

使用 brew install mysql 安装 mysql 运行 brew 建议的命令:(见注:下面) unset TMPDIR mysql_install_db --verbose --user=`whoami` --basedir="$(brew --prefix mysql)" --datadir=/ usr/local/var/mysql --tmpdir=/tmp 使用 mysql.server start 命令启动 mysql,以便能够登录使用备用安全脚本:/usr/local/Cellar/mysql/5.5.10/bin/ mysql_secure_installation 遵循 brew 包脚本输出中的 launchctl 部分,例如 #start launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist #stop launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.mysql .plist

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

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


很好的答案,谢谢!要补充的一件事:尽管答案中提到了它,但我错过了它,因此在尝试在 Lion OSX 上进行设置时浪费了大约 6 个小时。如果您在重新安装之前有以前版本的 MySql,请绝对确保删除 /usr/local/var/mysql 中的旧 mysql 目录。否则,您最初将无法以 root 身份登录以设置密码,并且您将花费大量时间对您的计算机大喊大叫。
进入 ~/Library/LaunchAgents 以查看实际调用 mysql 的 .plist 文件 - 在我的情况下,它是由自制软件安装的,因此您需要修改上述卸载过程。
因为这个线程很旧,所以上面的“launchctl unload”行现在是错误的。 homebrew 安装的文件不再称为“com.mysql.mysqld.plist”,而是称为“homebrew.mxcl.mysql.plist”。该行现在应为“launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist”
我有` [~/Library/LaunchAgents]$ ls com.adobe.ARM.202f4087f2bbde52e3ac2df389f53a4f123223c9cc56a8fd83a6f7ae.plist com.mysql.mysqld.plist com.facebook.videochat.ava.plist homebrew.mxcl.postgresql.plist` 在` ~/Library /LaunchAgents` 但卸载它们会得到 launchctl: Couldn't stat("com.mysql.mysqld.plist"): No such file or directory
mysql_install_db 现在可以省略,brew 在正确的目录中自动创建服务器。
C
Community

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

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

ps -ax | grep mysql

停止并杀死任何 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 /图书馆/收据/mysql*

sudo rm -rf /图书馆/收据/MySQL*

sudo rm -rf /private/var/db/receipts/*mysql*

sudo rm -rf /tmp/mysql*

尝试运行mysql,它不应该工作

Brew install MySQL per user Sedorner from this StackOverflow answer

酿造医生并修复任何错误

冲泡删除mysql

冲泡清理

酿造更新

冲泡安装mysql

unset TMPDIR mysql_install_db --verbose --user=`whoami` --basedir="$(brew --prefix mysql)" --datadir=/usr/local/var/mysql --tmpdir=/tmp # whoami 内联执行

mysql.server 启动

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

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

神速。


在没有先删除 /tmp/mysql.sock 的情况下执行上述所有操作,我也被咬了。
谢谢,+1。一对评论:您对 sudo rm ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist 说,然后几步之后您尝试 launchctl unload 该文件...但它不会存在,因为您删除了它。此外,brew install mysql 为我运行了 mysql_install_db 步骤,无需重复两次。
@CorySimmons 这是否意味着我应该在您的命令的 whoami 部分使用我自己的名字或 root
已确认 - whoami 是我的名字(我使用了 root)。这很好用。
请注意,优胜美地及其他地区不存在 /etc/hostconfig (superuser.com/questions/850974/…)
A
Alistair McMillan

有同样的问题。似乎设置说明或正在创建的初始表有问题。这就是我在我的机器上运行 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 和刚刚设置的新密码登录。


这对我有用!在我尝试这个之前我开始猜测密码并且密码最终是“密码”。所以试试吧。
在没有授权表的情况下启动 mysqld_safe 对我有用。但是,由于我在 select * from mysql.user 上没有任何记录,因此 UPDATE 不起作用;由于我们已经以 root 身份登录,我们可以简单地通过运行 GRANT ALL PRIVILEGES ON 为 root 插入一个。 TO 'root'@'localhost' 由 'whatever' 识别;同花顺特权;
我正在使用停止服务/脚本,它一直在重新启动。那是因为我从未卸载 LaunchAgent 吗? (多哈)
对我有用的东西类似于你做的东西。在您更新用户密码的部分,我不得不做出:使用mysql;更新用户集 authentication_string=password('1111') where user='root';如:stackoverflow.com/a/31122246/1328261
T
TRicks43

如果 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

我可以看到生成的临时密码,但是当我尝试运行 mysql 时,我得到:ERROR! The server quit without updating PID file (/usr/local/var/mysql/XXXXX.local.pid).,当我执行 mysql -u root 时,密码不被接受。
我使用 dmg installer 成功安装了 mysql 5.7.9,它提示我一个窗口,它为我生成了一个临时密码。现在 mysql 工作正常。太糟糕了,我不得不花费大量时间才能让它在全新的 Macbook 上运行。 :|
@forloop 1. ps -ef | grep mysql 2. kill -9 pid 3. mysql.server 启动
对我来说,上面的密码重置/更改不起作用。我必须使用临时密码登录 mysql,然后运行 alter command 1. mysql -u root -p <temp-password> 2. ALTER USER 'root'@'localhost' IDENTIFIED BY 'New-Password';
V
Vetrivel Chinnasamy

家酿

首先,确保您已安装自制软件运行 brew doctor 并解决自制软件希望您修复的任何内容运行 brew install mysql 运行 brew services restart mysql 运行 mysql.server start 运行 mysql_secure_installation


D
Darren Newton

好的,我遇到了同样的问题并解决了。由于某种原因,在使用 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:mysql -u root -p


这个解决方案对我不起作用,因为 mysql -u root 没有带我进入 mysql 提示符。它说用户'root'@'localhost'的访问被拒绝(使用密码:否)
你之前在这台机器上安装过 MySQL 吗?
M
Mark

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

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


C
Community

如果已经安装了mysql

完全停止mysql。

mysql.server stop <-- 可能需要根据您的版本进行编辑 ps -ef | grep mysql <-- 列出名称中带有 mysql 的进程 kill [PID] <-- 按 PID 杀死进程

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

须藤找到/。 -name "*mysql*" 使用你的判断,rm -rf 这些文件。请注意,许多程序都有您不想删除的 mysql 驱动程序。例如,不要删除 PHP 安装目录中的内容。删除它自己的 mysql 目录中的东西。

安装

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

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

sudo brew update sudo brew install cmake <-- mysql 的依赖项,有用的 sudo brew install openssl <-- mysql 的依赖项,有用的 sudo brew info mysql <-- 略过这个……它让你对接下来会发生什么有所了解 sudo brew install mysql --with-embedded; say done <-- 使用嵌入式服务器安装 mysql。完成后告诉你(我的安装花了 10 分钟)

然后

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


F
Felipe Duardo

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

. 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 中尝试了几种解决方案,但这对我没有帮助。


如果您以 root 身份运行 mysql.server(开始使用 root 用户时,MySQL 服务器降级为 _mysql 用户),请尝试在运行 mysql.server start 之前运行 sudo chown -R mysql /usr/local/var/mysql/
嘿,您是否碰巧找到了解决此问题的方法?
天哪,这个问题的问题数量和 1,000 行答案......我想我只会 brew install redis tbh
N
Nicholas

TL;博士

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

全文:

我刚刚在运行 Sierra 的新 MacBook Pro 上安装了 MySQL (stable) 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 就像一个魅力。


E
Edison

这是 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!

从“mysql.bak”复制所有数据库时,它不起作用:(
G
Gishas

只是为了在以前的答案中添加一些内容-从 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 终于开始工作了。


X
XMen

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


问题是通过brew安装后我根本无法访问mysql服务器。我可以启动它,但无论有没有密码,它都不会让我以 root 身份进入。 (因此我不能创建其他用户或管理权限..或者我不理解你吗?
A
Alexander Millar

我尝试重新启动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 load,它报告了“没有找到加载”错误。

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

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'

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


Brew 现在支持 services 命令,因此可以分别用 brew services stop mysqlbrew services start mysql 替换这些命令。
t
toddmetheny

将 brew 与最新版本的 mysql 和 yosemite 一起使用时,上述答案(或我在其他地方看到的几十个答案中的任何一个)都不适合我。我最终通过 brew 安装了不同的 mysql 版本。

通过说(例如)指定旧版本

brew install mysql56

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


是的,这可行,但如果您想安装 MySQL 5.7(目前由 brew install mysql 完成),您可以按照我的回答 stackoverflow.com/a/33924648/133106 中的说明进行操作
m
mrucci

Mysql 的“基本路径”存储在 /etc/my.cnf 中,当您进行 brew 升级时不会更新。只需打开它并更改 basedir 值

例如,改变这个:

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

指向新版本:

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

使用以下命令重新启动 mysql:

mysql.server start

A
Arunas Bartisius

试试我为 MariaDB 提供的解决方案,它也适用于 MySQL:

MacOSX homebrew mysql root password

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

尝试使用与您的 MacOS 帐户用户名相同的名称,例如 johnsmit。

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

mysql -u johnsmit