最近開始將開發工具都轉移到 Mac 上了,其中也會莫名其妙的遇到一些坑,不如干脆將整個流程都記錄下來,方便之後查找。mysql

下載與安裝

首先進入 MySQL 官網,選擇免費的Community版:MySQL Community Server。MySQL 官網提供了tar.gzdmg兩種格式的安裝包,接下來主要圍繞使用dmg安裝來講。sql

download

下載後雙擊打開安裝包,根聽說明一步步確認便可完成安裝。安裝成功後打開系統偏好設置,在最下面能夠找到 MySQL,經過它能夠查看 MySQL 狀態並啓動和關閉 MySQL。數據庫

start

指定配置

使用該方式啓動 MySQL 時是沒有指定配置文件的,所以 MySQL 會按照如下順序載入配置文件:vim

  1. /etc/my.cnf
  2. /etc/mysql/my.cnf
  3. /usr/local/mysql/etc/my.cnf
  4. ~/.my.cnftomcat

    只須要建立其中一個文件並設定好配置便可。bash

mysql: command not found

安裝後直接在命令行輸入mysql會提示mysql: command not found,須要先將其添加到環境變量:oracle

vim ~/.bash_profile

添加如下指令:app

export PATH=${PATH}:/usr/local/mysql/bin

保存後當即使其生效:工具

source ~/.bash_profile

即可以使用 MySQL 的相關命令了。post

配置用戶

剛安裝好的 MySQL 只有一個密碼爲空的root用戶,首先須要給該用戶設置一個密碼:

mysqladmin -u root password 你的密碼

而後使用設置好的密碼登陸 MySQl:

mysql -u root -p
輸入剛纔設置的密碼

因爲root用戶只能用於本地登陸,沒法遠程登陸,因此還須要建立一個用於遠程登陸的用戶:

GRANT ALL ON *.* TO 你的用戶名 @'%' IDENTIFIED BY '你的密碼' WITH GRANT OPTION;

這條語句的意思是建立一個用戶,賦予它以任何方式訪問,在任何數據庫的全部操做權限,可是實際這裏的任何訪問方式其實不包括localhost,因此還須要單獨設定:

GRANT ALL ON *.* TO 你的用戶名 @localhost IDENTIFIED BY '你的密碼' WITH GRANT OPTION;

與上一條語句同樣,只是將任何訪問方式改成了以localhost訪問。

ERROR 2003 (HY000): Can’t connect to MySQL server on ‘127.0.0.1’ (61)

配置完遠程登陸的帳戶後,嘗試使用該帳戶登陸一下 MySQL:

mysql -h localhost -u 你的用戶名 -p
輸入密碼

確實是能夠登陸成功的,但是若是使用 ip 登陸:

mysql -h 127.0.0.1 -u 你的用戶名 -p
輸入密碼

卻會出現ERROR 2003 (HY000): Can't connect to MySQL server on '127.0.0.1' (61)的錯誤。緣由是使用dmg安裝的 MySQL 啓動時的默認端口號不是3306而是3307,能夠在這裏修改:

sudo nano com.oracle.oss.mysql.mysqld.plist

--port的值改成3306便可:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key> <string>com.oracle.oss.mysql.mysqld</string>
<key>ProcessType</key> <string>Interactive</string>
<key>Disabled</key> <false/>
<key>RunAtLoad</key> <true/>
<key>KeepAlive</key> <true/>
<key>SessionCreate</key> <true/>
<key>LaunchOnlyOnce</key> <false/>
<key>UserName</key> <string>_mysql</string>
<key>GroupName</key> <string>_mysql</string>
<key>ExitTimeOut</key> <integer>600</integer>
<key>Program</key> <string>/usr/local/mysql/bin/mysqld</string>
<key>ProgramArguments</key>
<array>
<string>/usr/local/mysql/bin/mysqld</string>
<string>--user=_mysql</string>
<string>--basedir=/usr/local/mysql</string>
<string>--datadir=/usr/local/mysql/data</string>
<string>--plugin-dir=/usr/local/mysql/lib/plugin</string>
<string>--log-error=/usr/local/mysql/data/mysqld.local.err</string>
<string>--pid-file=/usr/local/mysql/data/mysqld.local.pid</string>
<string>--port=3307</string>
</array>
<key>WorkingDirectory</key> <string>/usr/local/mysql</string>
</dict>
</plist>

或是直接指定端口號鏈接:

mysql -h 127.0.0.1 -P 3307 -u 你的用戶名 -p
輸入密碼

Communications link failure

Out of resources when opening file ‘…’

這兩個都是 JDBC 鏈接 MySQL 時報的錯,之因此放在一塊兒說是由於改着改着莫名其妙兩個都好了。

配好 MySQL 項目後運行了一個多數據源並使用數據庫鏈接池的項目,一共有 32 個數據源,tomcat-jdbc 鏈接池每一個數據源初始化 10 個鏈接,在建立一半數據源以後開始出現第一個錯誤:

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.

不少種狀況都會致使這個錯誤,不過這裏應該是數據庫鏈接數不夠,想到配置完後幾乎沒有編輯my.cnf文件,因而添加了這些屬性:

max_connect_errors = 10000
max_connections=10000
max_user_connections=10000

修改完後使用show variables like 'max%';查看數據庫屬性,發現對應數值確實改變了,可是運行程序仍是會出現同樣的問題。

後來也 google 了好久,推測應該是系統配置致使的限制,可是也沒找到問題具體出在哪裏,只好先把初始化的線程數改小看看能不能正常運行,結果程序運行了一段時間後又出現另外一個錯誤:

Out of resources when opening file './xxx.MYD' (Errcode: 24)

這個錯誤的緣由就很顯而易見了,確定是 MySQL 打開的文件過多了,可是我明明記得在my.cnf中設置了open_files_limit,不可能出現這個錯誤啊。

結果使用show variables like 'open%';一看,配置居然沒生效,文件數只有可憐的 256,再三確認配置沒有問題後認定緣由確定出在系統配置上。

首先使用ulimit -n查看系統的文件數限制,果真是 256,看來 MySQL 會在my.cnfulimit中選擇較小的一方做爲真正的值。

而後 google 一下 Mac 如何修改這個數值,一種方式是使用launchctl limit maxfiles 65535將文件數修改爲 65535,嘗試後發現確實系統的ulimit -n和 MySQL 的open_files_limit都變成了 65535。當時覺得這樣就解決了,結果又發現 JDBC 沒法鏈接 MySQL 了,而 MySQL 客戶端就能連上,真是匪夷所思。還好這個配置只要重啓電腦就會失效,重啓事後又能正常的連上 MySQL 了。

還有一種方法只能用在 Mac OS X Yosemite 上的,首先建立/Library/LaunchDaemons/limit.maxfiles.plist文件,添加如下內容:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>limit.maxfiles</string>
<key>ProgramArguments</key>
<array>
<string>launchctl</string>
<string>limit</string>
<string>maxfiles</string>
<string>65536</string>
<string>65536</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>ServiceIPC</key>
<false/>
</dict>
</plist>

建立/Library/LaunchDaemons/limit.maxproc.plist文件,添加如下內容:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple/DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>limit.maxproc</string>
<key>ProgramArguments</key>
<array>
<string>launchctl</string>
<string>limit</string>
<string>maxproc</string>
<string>2048</string>
<string>2048</string>
</array>
<key>RunAtLoad</key>
<true />
<key>ServiceIPC</key>
<false />
</dict>
</plist>

而後重啓電腦就會發現數值已經改變了,全部方式也都能正常鏈接,我把初始化線程數調回來之後發現第一個錯誤也不會出現了。