在macOS上安装mysql

我试图在mac os 10.6上使用brew install mysql 5.1.52安装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以及,
但是有没有密码都不行。< / p >

这是全新机器上的全新安装,据我所知,新安装必须在没有根密码的情况下访问。我还试过:

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

但我也得到了

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

尝试授予权限命令mysql

好吧,我也遇到过同样的问题,但我已经解决了。由于某种原因,mysql_secure_installation脚本在使用Homebrew安装mysql时不能开箱即用,所以我手动安装了。在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

我刚才也遇到了同样的问题。如果你brew info mysql并遵循步骤,看起来根密码应该是new-password,如果我没记错的话。我看到的和你看到的一样。这篇文章对我的帮助最大。

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

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

我认为如果已经安装了旧版本的mysql,就会出现这种情况。我也遇到了同样的问题,上面的解决方案对我都不起作用。我是这样安排的:

Used brew's remove &cleanup命令,卸载launchctl脚本,然后删除mysql目录在/usr/local/var,删除我现有的/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安装mysql
  2. 运行命令brew suggested:(见注:下面)

    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 start command, 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 launchctl section 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
    

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

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

我有同样的问题后,我试图重新启动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加载,它报告了一个“nothing found to 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'

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

下面是Sedorner上面写的关于如何从你的Mac上删除所有MySQL,然后用Brew Way安装它的详细说明:

根据科技实验室完全删除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*
  • sudo rm -rf /tmp/mysql*
  • 尝试运行mysql,它不应该工作

从这个StackOverflow答案来编译每用户安装MySQL

  • brew doctor并修复任何错误

  • < p > brew remove mysql

  • < p > brew cleanup

  • < p > brew update

  • < p > brew install mysql

  • < p > unset TMPDIR

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

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

mysql现在应该工作,并一直按照预期运行

祝成功。

Mysql的“Base-Path”存储在/etc/my.cnf中,当你酿造升级时,它不会更新。只需打开它并更改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

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

通过(例如)指定旧版本

brew install mysql56

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

如果brew安装了MySQL 5.7,这个过程与以前的版本有点不同。

. sh

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

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

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

如果mysql已经安装

完全停止mysql。

  1. mysql.server stop <——可能需要根据你的版本进行编辑
  2. ps -ef | grep mysql <——列出名称中包含mysql的进程
  3. kill [PID] <——通过PID终止进程

删除文件。上面的说明很好。我将添加:

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

安装

希望你有自制的。如果没有,请下载。

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

  1. sudo brew update
  2. sudo brew install cmake <——mysql的依赖,有用
  3. sudo brew install openssl <——mysql的依赖,有用
  4. ——略读一下这个…它让你知道接下来会发生什么
  5. sudo brew install mysql --with-embedded; say done <——用嵌入式服务器安装mysql。告诉你什么时候完成(我的安装花了10分钟)

后来

  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用户添加密码。

博士TL;

MySQL服务器在安装Brew后可能无法运行。如果你不希望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,效果非常好。

下面是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 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 < / p > 所以,我的问题的关键是卸载后删除/usr/local/etc/my.cnf文件。 在最后一步之后,MySql终于开始工作了

尝试解决方案,我提供的MariaDB,高变化,它与MySQL的工作也:

MacOSX homebrew mysql根密码

简而言之,请尝试使用您的用户名登录!没有根。

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

要以root用户登录,请执行以下命令:

mysql -u johnsmit

家酿

  1. 首先,确保安装了自制程序
  2. 运行brew doctor并解决任何homebrew想要你解决的问题
  3. 运行brew install mysql
  4. 运行brew services restart mysql
  5. 运行mysql.server start
  6. 运行mysql_secure_installation

删除/opt/homebrew/var/mysql就可以了。显然有mysql数据(包括密码),所以上述所有操作都是徒劳的。