前言:
因?yàn)橛胢ac開(kāi)發(fā),很多工具不是很熟練久信,這里做個(gè)筆記窖杀,以防以后再次入坑!H故俊入客!
安裝方式一:用dmg鏡像安裝
1、安裝
下載好mysql MAC版安裝包,常規(guī)步驟安裝桌硫,安裝過(guò)程中會(huì)出現(xiàn)如下提示:
2018-04-22T08:07:19.044013Z 1 [Note] A temporary password is generated for root@localhost: TfrNnt9!k1xi
其中TfrNnt9!k1xi 是初始密碼夭咬,最好先記住铆隘!
2卓舵、登陸
但是在終端命令行 登陸mysql
mysql -u root -p
提示:-bash: mysql: command not found
遇上-bash: mysql: command not found的情況別著急,這個(gè)是因?yàn)?usr/local/bin目錄下缺失mysql導(dǎo)致膀钠,只需建立軟鏈接掏湾,即可以解決:
把mysql安裝目錄,比如MYSQLPATH/bin/mysql肿嘲,映射到/usr/local/bin目錄下:
cd /usr/local/bin
ln -fs /usr/local/mysql-8.0.11-macos10.13-x86_64/bin/mysql mysql
接下來(lái)登陸就OK啦(完事H诨鳌)
3、修改密碼
在MySQL8.0.4以前雳窟,執(zhí)行
SET PASSWORD=PASSWORD('修改的密碼');
即可修改密碼尊浪。
如果mysql是8.0版本以上,這樣默認(rèn)是不行的封救。因?yàn)橹澳吹樱琈ySQL的密碼認(rèn)證插件是“mysql_native_password”,而現(xiàn)在使用的是“caching_sha2_password”誉结。
因?yàn)楫?dāng)前有很多數(shù)據(jù)庫(kù)工具和鏈接包都不支持“caching_sha2_password”工育,為了方便,我暫時(shí)還是改回了“mysql_native_password”認(rèn)證插件搓彻。
在MySQL中執(zhí)行命令:
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '新密碼';
總結(jié):
其實(shí)鏡像安裝非常簡(jiǎn)單如绸,只是在映射mysql指令的時(shí)候需要燒點(diǎn)腦子。
安裝方式二:用Homebrew進(jìn)行安裝
最近發(fā)現(xiàn)mac上一個(gè)神奇工具 Homebrew(包缺失管理)旭贬,就像maven管理jar包的依賴(lài)一樣怔接,Homebrew幫忙管理各種工具,真是太方便稀轨。Homebrew的常用指令扼脐。
安裝mysql:brew install mysql
feideMacBook-Air:~ fei$ brew install mysql@5.7
Updating Homebrew...
==> Downloading https://homebrew.bintray.com/bottles/mysql@5.7-5.7.23.high_sierra.
Already downloaded: /Users/fei/Library/Caches/Homebrew/mysql@5.7-5.7.23.high_sierra.bottle.tar.gz
==> Pouring mysql@5.7-5.7.23.high_sierra.bottle.tar.gz
==> /usr/local/Cellar/mysql@5.7/5.7.23/bin/mysqld --initialize-insecure --user=fei
==> Caveats
We've installed your MySQL database without a root password. To secure it run:
mysql_secure_installation
MySQL is configured to only allow connections from localhost by default
To connect run:
mysql -uroot
This formula is keg-only, which means it was not symlinked into /usr/local,
because this is an alternate version of another formula.
If you need to have this software first in your PATH run:
echo 'export PATH="/usr/local/opt/mysql@5.7/bin:$PATH"' >> ~/.bash_profile
For compilers to find this software you may need to set:
LDFLAGS: -L/usr/local/opt/mysql@5.7/lib
CPPFLAGS: -I/usr/local/opt/mysql@5.7/include
To have launchd start mysql@5.7 now and restart at login:
brew services start mysql@5.7
Or, if you don't want/need a background service you can just run:
/usr/local/opt/mysql@5.7/bin/mysql.server start
==> Summary
?? /usr/local/Cellar/mysql@5.7/5.7.23: 317 files, 234.4MB
看看輸出的提示,剩下的該怎么操作其實(shí)brew提示的很清楚了7芄簟M呶辍!
對(duì)mysql進(jìn)行初始化操作:mysql_secure_installation
$ mysql_secure_installation
Securing the MySQL server deployment.
Connecting to MySQL using a blank 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?
// 這個(gè)選yes的話(huà)密碼長(zhǎng)度就必須要設(shè)置為8位以上佣谐,但我只想要6位的
Press y|Y for Yes, any other key for No: N
Please set the password for root here.
New password: // 設(shè)置密碼
Re-enter new password: // 再一次確認(rèn)密碼
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.
// 移除不用密碼的那個(gè)賬戶(hù)
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.
//是否禁止遠(yuǎn)程登錄
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.
// 是否刪除test庫(kù)
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
- Dropping test database...
Success.
- Removing privileges on test database...
Success.
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!
OK肚吏!這樣brew提示中的第一步就完成!接下來(lái)就可以嘗試登陸了
登陸mysql:mysql -u root -p
啟動(dòng)mysql:brew services start mysql@5.7
停止mysql:brew services stop mysql@5.7