34

By default I am being connected to port 3309.I need to connect to port 3307.How do I do that?

0

6 Answers 6

92

Use -P parameter, like this:

mysql -h 127.0.0.1 -P 3307 -u user_name -p database_name

Important: if you connecting to localhost - use -h 127.0.0.1, NOT localhost, because MySQL will connect by file socket, not by TCP

6
  • mysql> mysql -h 127.0.0.1 -P 3307 -u root -p sch_rel_122718; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'mysql -h 127.0.0.1 -P 3307 -u root -p sch_rel_122718' at line 1. I even ommited the mysql in the start but the to no avail. Feb 13, 2019 at 5:41
  • This message means that you are successfully connected to database. Suppose you are trying to load some SQL from file, like this: mysql -h 127.0.0.1 -P 3307 -u root -p sch_rel_122718 < file.sql The error is in the file.sql. Show the file, I will help. PS: is sch_rel_122718 the database name? Feb 13, 2019 at 6:25
  • Yes I am connected to the database but to port 3309 because when I execute "show databases" it displays the databases of that port.and yes sch_rel_122718 is the name of the database which exists in port 3307. Feb 13, 2019 at 7:36
  • Are you specify -h 127.0.0.1, NOT -h localhost? Feb 13, 2019 at 9:42
  • 3
    Yup -h 127.0.0.1 no localhost Feb 14, 2019 at 11:29
2

From command line, assuming you are on the same host, have you tried :

mysql --user root --password (mypassword) --host=localhost --port=33061 

In server name specify custom port when not using default one (you can imply it only when is the standard mysql port 3306)

$servername = "localhost:33061";

2

you can use -P (uppercase) or --port=portnumber sample

mysql -u root -P 13306 -p databasename

or

mysql -u root --port=13306 -p databasename
1

for *nix client use --host=127.0.0.1 instead of localhost

mysql --host=127.0.0.1 --port=3307 -u root -pdb_passwd db_name

Only connection options that are relevant to the selected transport protocol are used or checked. Other connection options are ignored. For example, with --host=localhost on Unix, the client attempts to connect to the local server using a Unix socket file, even if a --port or -P option is given to specify a TCP/IP port number.

To ensure that the client makes a TCP/IP connection to the local server, use --host or -h to specify a host name value of 127.0.0.1 (instead of localhost), or the IP address or name of the local server.

2
  • FYI: For this type of answer, you should provide an example... because sometimes user's won't fully understand, and voters will pick other answers...
    – benc
    Mar 28, 2023 at 7:07
  • example has been provided. please review Apr 19, 2023 at 21:42
0

Enter this command changing your details. after that MySQL requests the password for the connection, then enter the password.

mysql --user=user1  --host=127.16.38.1 --port=25060 -p

especially consider about -p and double Hyphen --

0

I am giving simple way, one liner which summarizes.

mysql -u root -p --port=3316 // I have MySQL port 3316, instead of default 3306

If the --port=3316 is not provided, then MySQL Cli protocol will try with the default port, without asking.

For any other user

mysql -u anotheruser -p --port=3316

Not the answer you're looking for? Browse other questions tagged or ask your own question.