mysqlemacssql-mode

How can I get emacs sql-mode to use the mysql config file (.my.cnf)?


When I type mysql dbname at the bash prompt, I automatically connect to the database dbname with the username, password, and host information included in my .my.cnf file.

When I use M-x sql-mysql in emacs, I am asked for all of this information again.

Is there a way that I can get emacs sql mode to use the information in my .my.cnf file?


Solution

  • I don't think that this is possible but you can set such stuff in the mode configuration itself:

    (setq sql-connection-alist
    '((pool-a
    (sql-product 'mysql)
    (sql-server "1.2.3.4")
    (sql-user "me")
    (sql-password "mypassword")
    (sql-database "thedb")
    (sql-port 3306))
    (pool-b
    (sql-product 'mysql)
    (sql-server "1.2.3.4")
    (sql-user "me")
    (sql-password "mypassword")
    (sql-database "thedb")
    (sql-port 3307))))
    
    (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 sql-pool-a ()
      (interactive)
      (sql-connect-preset 'pool-a))
    

    Have a look at this article for more info.