How to connect to MySQL on a custom port using Emacs sql-mode?

Hey everyone, I need some help with Emacs sql-mode. I’ve been using it to talk to MySQL and it’s been great. But now I’m stuck.

A coworker set up a new database on a different port. I can’t figure out how to connect to it using sql-mysql. Does anyone know how to specify a custom port number when connecting?

It would be awesome if Emacs could ask for the port and use the default if I don’t type anything. Is that possible?

I’ve tried looking through the docs but couldn’t find anything clear. Any tips or tricks would be really helpful. Thanks in advance!

To connect to MySQL on a custom port using Emacs sql-mode, you can modify the sql-mysql-options variable. Add the port option like this:

(setq sql-mysql-options '(“-P 3307”))

Replace 3307 with your actual port number. This method sets a specific port for all MySQL connections in Emacs.

Alternatively, for more flexibility, you can create a custom connection in your init file:

(add-to-list 'sql-connection-alist
'(“custom-mysql”
(sql-product 'mysql)
(sql-server “localhost”)
(sql-user “username”)
(sql-password “password”)
(sql-database “dbname”)
(sql-port 3307)))

Then, use M-x sql-connect RET custom-mysql to connect using these settings.

hey climbin monkey, i’ve had that same issue before. try using sql-set-sqli-mysql-program and set it to “mysql -P PORT”. replace PORT with ur actual port number. then run sql-mysql as usual. hope that helps! lemme know if u need more details

ooh, interesting question! have u tried messin with the sql-connection-alist variable? i think u can add a new entry there with a custom port. curious tho, why did ur coworker set up the db on a diff port? sounds like theres a cool story there! :thinking: