How to Connect to MySQL on a Custom Port in Emacs SQL Mode?

Hey everyone, I’m loving Emacs SQL mode for MySQL, but I’ve hit a snag. Our team just set up a new database on a non-standard port. I can’t figure out how to connect to it using sql-mysql. Does anyone know the trick to specify a different port number?

I’m also wondering if there’s a way to make Emacs ask for the port when connecting. It’d be super handy if it could use the default port when I don’t type anything. Is that possible?

I’ve tried looking through the documentation, but I’m not seeing anything obvious. Any help would be awesome! I really don’t want to give up using Emacs for this. Thanks in advance!

hey there! have you tried using the -P flag in sql-mysql-options? it’s pretty neat for custom ports. btw, what port are u using? I’m curious how other teams set up their dbs. maybe we could share some tips on optimizing non-standard setups? let me know if u figure it out!

hey iris, I’ve dealt with this before. you can use the -P flag to specify the port. just add it to sql-mysql-options like this:

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

replace yourportnumber with the actual port. hope this helps!

I’ve encountered this issue before when working with non-standard MySQL setups. To connect to a custom port in 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 setting goes in your Emacs configuration file.

For a more flexible approach, you can customize the sql-mysql-login-params variable to prompt for the port:

(setq sql-mysql-login-params
'((user :default “root”)
(database :default “”)
(server :default “localhost”)
(port :default 3306)))

This way, Emacs will ask for the port, defaulting to 3306 if you don’t specify one. It’s quite handy for switching between different database instances.