由于之前是做后端开发的,想着“虽然现在做前端了,但不能忘记以前的老本行,能否应用以往做后端时学到的知识”。就打算从搭建本地数据库开始,下面主要介绍2种安装方法以及后续的配置,希望对大家有帮助。(推荐通过安装包的形式安装)
1.使用安装包安装mysql(网上下载实在是太慢了,需要安装包的可以在文章底部留言,我发给您)
- 双击打开安装文件

- 双击pkg文件安装

- 一路向下,记得保存最后弹出框中的密码(它是你mysql root账号的密码)

-
正常情况下,安装成功。
-
此时只是安装成功,但还需要额外的配置:
(1) 进入系统偏好设置

(2) 点击mysql

(3) 开启mysql服务

- 此时我们在命令行输入mysql -uroot -p命令会提示没有commod not found,我们还需要将mysql加入系统环境变量。
(1).进入/usr/local/mysql/bin,查看此目录下是否有mysql,见pic6。
(2).执行vim ~/.bash_profile
在该文件中添加mysql/bin的目录,见pic7:
PATH=$PATH:/usr/local/mysql/bin
添加完成后,按esc,然后输入wq保存。
最后在命令行输入source ~/.bash_profile


- 现在你就可以通过mysql -uroot -p登录mysql了,会让你输入密码,就是pic3上的>fj...
登录成功后,你可以通过下面的命令修改密码
SET PASSWORD FOR 'root'@'localhost' = PASSWORD('newpass');
2.使用homebrew安装mysql及遇到的问题
- 安装命令
brew install mysql
- 安装完成之后,本地命令行输入mysql命令,发现无此命令
commond not found
- 首先,检查是否是安装了
重新执行一遍
brew install mysql
命令行提示:
Warning: mysql-5.7.10 already installed, it's just not linked
- 然后网上查找解决方法,最后解决方法是执行:
brew link --overwrite mysql
- 但是执行,却报错
Linking /usr/local/Cellar/mysql/5.7.10...
Error: Could not symlink share/man/man8/mysqld.8
/usr/local/share/man/man8 is not writable.
- 又在网上各种查找解决方法,最后本地实验以下语句执行成功
sudo chown -R 'yin' /usr/local
注意: yin是你电脑的用户名
- 解决了问题后,重新执行:
brew link --overwrite mysql
提示:
Linking /usr/local/Cellar/mysql/5.7.10... 92 symlinks created
心想着,这下算是成功了吧。重新执行:
mysql -u root -p
但是又报错:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
依次执行:
unset TMPDIR
bash mysql_install_db --verbose --user=root
--basedir="$(brew --prefix mysql)"--datadir=/usr/local/var/mysql --tmpdir=/tmp
接下来启动mysql
bash mysql.server start
网友评论
这是命令: set password for 'root'@'localhost' = password('123456')
owned by: han dated: Thu Jul 26 12:19:07 2018
file name: ~han/.bash_profile
modified: YES
user name: han host name: handeMacBook-Air.local
process ID: 3022
While opening file "/Users/han/.bash_profile"
(1) Another program may be editing the same file. If this is the case,
be careful not to end up with two different instances of the same
file when making changes. Quit, or continue with caution.
(2) An edit session for this file crashed.
If this is the case, use ":recover" or "vim -r /Users/han/.bash_profile"
to recover the changes (see ":help recovery").
If you did this already, delete the swap file "/Users/han/.bash_profile.swp"
to avoid this message.
Swap file "~/.bash_profile.swp" already exists!
Swap file "~/.bash_profile.swp" already exists!elete it, (Q)uit, (A)bort:
[O]pen Read-Only, (E)dit anyway, (R)ecover, (D)elete it, (Q)uit, (A)bort:
-- More --"~/.bash_profile.swp" already exists!elete it, (Q)uit, (A)bort:
-- More --
Swap file "~/.bash_profile.swp" already exists!
[O]pen Read-Only, (E)dit anyway, (R)ecover, (D)elete it, (Q)uit, (A)bort:
-- More --
请问这是啥意思
到后面编辑配置的时候,我的配置文件里面是有内容的,并且修改完配置文件,在终端输入mysql -u root -p,会提示 command not found
你遇到这个问题了吗
export JAVA_8_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_162.jdk/Contents/Home
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_162.jdk/Contents/HomeE
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
"~/.bash_profile" 4L, 169C
不知道怎么进入下一步
我在执行下面两行命令后 报错:
/usr/local/bin/mysql_install_db: /usr/local/bin/mysql_install_db: cannot execute binary file
这个如何解决?
PATH=$PATH:/usr/local/mysql/bin
source ~/.profile
~
~
~
~
~
~
~
~
~
~
~
~
~
~
按esc 怎么退出不了的
谢谢楼主了
(2).执行vim ~/.bash_profile
在该文件中添加mysql/bin的目录,见pic7:
PATH=$PATH:/usr/local/mysql/bin
添加完成后,按esc,然后输入wq保存。
最后在命令行输入source ~/.bash_profile
sudo mysql -uroot 就可以了
mysql Ver 14.14 Distrib 5.7.20, for macos10.12 (x86_64)
按楼主的方法重设密码的时候提示:
“You must reset your password using ALTER USER statement before executing this statement.”
后来实验直接:SET PASSWORD FOR 'root@localhost' = PASSWORD('NEW PASSWORD'); 成功
FYI
--basedir="$(brew --prefix mysql)"--datadir=/usr/local/var/mysql --tmpdir=/t 这个命令报错 解决不了
brew install mysql出现的问题。
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
解决:mysql.server start
推荐下,分库分表中间件 Sharding-JDBC 源码解析 17 篇:http://www.yunai.me/categories/Sharding-JDBC/?jianshu&401
栋
谢谢楼主
麻烦作者了