# Connect To Remote Database



## DejaVu (Dec 6, 2010)

Hello All,

I'm new to FreeBSD and started to use it before 2 months.

I'm running FreeBSD using Virtual Machine software... and I faced a problem and couldn't solve it and also didn't find any accurate solution for it.

The problem is that I'm unable to connect remotely to MySQL database, while the same code connects fine in the same network.

I was able to connect remotely to my server via FTP, SSH and some online games, but couldn't connect with my PHP code.

Should I use my IP or the Dynamic DNS?

I tried the following commands with no errors but didn't work too:

```
mysql> GRANT ALL ON *.* TO root@"%" IDENTIFIED BY 'MyPassword' WITH GRANT OPTION;
mysql> GRANT ALL ON *.* TO root@'sub.mydomain.com' IDENTIFIED BY 'MyPassword';
```

Some articles asked to modify the file "my.cnf" but I didn't find it anywhere in my FreeBSD, so do I need to use this file too?

I'm using FreeBSD 7.2-Release, MySQL-5.0.90 and also connecting to my server using DynamicDNS.

So is it possible to connect from my website to MySQL database on my server? 

Thanks in advance


----------



## SirDice (Dec 6, 2010)

What is the error message you're getting?


----------



## DejaVu (Dec 6, 2010)

Thank you for your reply.

I'm getting this Error:

```
Lost connection to MySQL server during query
```

My PHP code is in the attachments.

Also I tried to check the ports with "fsockopen" but didn't work too.

I have no problem to give the real info about DB and server if it's needed, because this a test server and I use it for academic reasons and I will make new one when everything is ok.

Regards,


----------



## SirDice (Dec 6, 2010)

Use the mysql command from the server that needs to connect to the database server.


----------



## DejaVu (Dec 6, 2010)

thanks man
but really didn't understand what command to use?


----------



## SirDice (Dec 6, 2010)

mysql(1)

And another note, don't use the MySQL root account. Create an account specifically for the website. Even if it's just for test.


----------



## mix_room (Dec 6, 2010)

```
mysql -u root -p remotehost
```
or something of that nature, as SirDice so nicely said.


----------



## DejaVu (Dec 7, 2010)

Hello,

I'm still unable to fix that and cant connect to MySQL located in my server.
I got this error:


----------



## qsecofr (Dec 7, 2010)

```
man mysql
```
 suggests the long form options.  Try

```
mysql -h fqdn_host.domain.com --user=username --password=password
```
Or optionally the dotted-decimal IP address (or simply hostname if it resolves) as an argument to the -h switch.  I had had trouble in the past with both the -u and -p switches for still as yet unknown reasons.  The above command, though, works for me - connecting from my workstation to a remote mysql all within my 192.168 private subnet.

If there's a firewall between you & mysql verify it's letting you through.  If client & server sit on the same physical box you may not need the host switch.  Not sure about running in a VM.  But if it's bound to a particular interface, you might be able to find which IP by 

```
sockstat -4 | grep mysql
```

The ~/.my.cnf can probably go in your home directory..


----------



## DejaVu (Dec 10, 2010)

Thank you guys for your support.. its working now
but i'm still unable to find "my.cnf" i made search in my FreeBSD with no result.
I just found these: my-small.cnf, my-medium.cnf and my-large.cnf.
one thing more please, what does this mean: "~/." ?
thanks in advance,


----------



## DutchDaemon (Dec 10, 2010)

my.cnf or .my.cnf will not exist if you don't choose to create them.

~ means the current user's home directory, and what you're seeing above is *~* plus a file named *.my.cnf*, in other words: a file named .my.cnf in the current user's home directory. Unix 101.


----------

