用Emacs訪問MySQL好久了,最近因為換了單位,重新設定環境的時候居然花了一個多小時,特此記錄。
1.Ubuntu下用sudo apt-get install 安裝
libmysqlclient-dev 和 mysql-client-core-5.5
2.下載sql.el和mysql.el,並在~/.emacs檔案中載入
http://www.emacswiki.org/emacs/sql.el
http://www.emacswiki.org/emacs/mysql.el
(load-file (expand-file-name "/opt/emacs_plugins/sql.el"))(load-file (expand-file-name "/opt/emacs_plugins/mysql.el"))
3.如果你的mysql連接埠不是標準連接埠的話,需要用下面的命令在.emacs檔案中設定
(setq sql-mysql-options (list "-P 6871"))
4.通過下面的命令設定兩個串連mysql的常用帳號:
(setq sql-connection-alist '((pool-a (sql-product 'mysql) (sql-server "ip1") (sql-user "user1") (sql-password "pwd1") (sql-database "db1") (sql-port 6871)) (pool-b (sql-product 'mysql) (sql-server "ip2") (sql-user "user2") (sql-password "pwd2") (sql-database "db2") (sql-port 3306))))(defun sql-connect-preset (name) "Connect to a predefined SQL connection listed in `sql-connection-alist'" (eval `(let ,(cdr (assoc name sql-connection-alist)) (flet ((sql-get-login (&rest what))) (sql-product-interactive sql-product)))))(defun mysql-a () (interactive) (sql-connect-preset 'pool-a))(defun mysql-b () (interactive) (sql-connect-preset 'pool-b))