我正在尝试在mac os 10.6上使用Homebrew by brew安装MySQL 5.1.52。

一切都很顺利,我也成功地使用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或mysql使用-u root - root以及, 但是有没有密码都不行。

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

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

但我也得到了

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

当前回答

尝试授予权限命令mysql

其他回答

下面是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,就会出现这种情况。我也遇到了同样的问题,上面的解决方案对我都不起作用。我是这样安排的:

使用brew的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

然后我从头开始:

installed mysql with brew install mysql 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 Start mysql with mysql.server start command, to be able to log on it Used the alternate security script: /usr/local/Cellar/mysql/5.5.10/bin/mysql_secure_installation 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

注意:啤酒清理的——force位也会清理过时的酒桶,这是一个新的自制啤酒功能。

注意第二点:一个评论者说第二步不是必需的。我不想测试它,所以是YMMV!

如果mysql已经安装

完全停止mysql。

mysql。服务器停止<——可能需要根据您的版本进行编辑 Ps -ef | grep mysql <——列出名称中包含mysql的进程 kill [PID] <——按PID终止进程

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

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

安装

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

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

Sudo brew更新 Sudo brew install cmake <——mysql的依赖关系,有用 Sudo brew install openssl <——依赖于mysql,有用 Sudo brew info mysql <—撇过这个…它让你知道接下来会发生什么 Sudo brew install mysql -with-embedded用嵌入式服务器安装mysql。告诉你什么时候完成(我的安装花了10分钟)

后来

sudo chown -R mysql /usr/local/var/mysql/ <——mysql不为我工作,直到我运行这个命令 sudo mysql。Server start <——同样,确切的语法可能有所不同 在mysql (http://dev.mysql.com/doc/refman/5.7/en/create-user.html)中创建用户。请记住为root用户添加密码。

尝试授予权限命令mysql

家酿

首先,确保安装了自制程序 运行brew doctor,解决任何homebrew想要你解决的问题 运行brew install mysql 运行brew services重启mysql 运行mysql。服务器启动 运行mysql_secure_installation