Jay Patwa
Jay Patwa

Reputation: 11

How to connect other MySQL in terminal PC?

There are some problems in my mac. One of the problems is that there are two MySQL instances installed. First mysql is executed at localhost and second one is at MAMP. Because of this, when i tried to connect mysql using php:

... it is connected to to the MAMP instance.

And when i tried to connect to MySQL using Terminal, it is connected to the localhost instance. So I can't do what I want... When I create a database named 'dbname' and try to connect to it from my html, it generateses an error: mysqli_connect(): (42000/1049): Unknown database 'dbname'

What should I do to make it work?

Upvotes: 1

Views: 157

Answers (1)

Alma Do
Alma Do

Reputation: 37365

When you are connecting to local host, by default it tries to use not TCP/IP for serving a connection, but UNIX-socket instead - if your connection does not tell strictly, that TCP/IP must be used. That means, for instance, that if you have two mysqld instances on different port for localhost, then it's not enough just to specify port, since then mysql will try to connect for a wring socket.

Example: you have once mysql instance at localhos:3306 and second on localhost:3307. Then, command:

mysql -uUSER -pPASSWORD -P3307

will connect you to wrong 3306 instance, since it will use the default socket instead of using port. It may look like an oversight from mysql client, but socket has a priority in this case (i.e. localhost). To solve this problem, you have two options:

First option is to specify socket directly. That may be done directly with -S parameter, like:

mysql -uUSER -pPASSWORD -S/path/to/correct/socket

where path to socket for needed instance you may see in your configuration file. Since you have Mac, may be you'll have even to create your file, see this question for more details on that.

Second option would be - force mysql client to use TCP/IP connection. To achieve that, you may just specify host IP address explicitly in your connection options:

mysql -uUSER -pPASSWORD -h127.0.0.1 -P3307

That would be for 3307 port. This, however, may lead to error like:

"Lost connection to MySQL server at 'reading initial communication packet, system error: 0"

Then, most probably, that would mean wrongly configured instance. You may then just add bind-address=127.0.0.1 to your my.cnf file, if it was not there. Again, more details on that problem may be found in this question.

Upvotes: 1

Related Questions