Find the Web Address for MySQL Database - php

I have got a cPanel hosting and I can create and log in to the phpmyadmin and change databases after I log in to the cPanel. What I need to do is log in to the same database even if my files are not on the same server, so I need to specify the exact database URL instead of "localhost". How do I get that URL?

The most secure way to access a remote database server is to setup SSH access on your database server and a SSH Tunnel on the web server that is hosting your files.
See: Connect to a MySQL server over SSH in PHP
This way you don't need to expose your database server to the internet, and instead can access it from the localhost of your web server.
If the database is exposed to the internet, you can assign a domain name to your database server and use that domain name, or subdomain, as the host and create a wildcard user like root#'%' but that is far less secure.

Related

Remote mysql connection

I work on a dictionary PHP project. Project owner has a dedicated server. They control server files via terminal. And they have a big database which include 370,000 words. In my program I want to connect to database. But the problem is that they don't want to upload my PHP project files to their server. Because they fear that the database maybe stolen by others. They say you can access our mysql server via some server info and they have send some information like this:
server ip : xx.xxx.xxx.xx
Port=3307;
UID=xxx;
pwd=xxxx
The thing that I know is if I want to connect a remote mysql server, The server owner must allow my IP address or upload project files to their server.
They say we don't have a ftp interface and can't allow remote IPs. You can successfully connect via mysql connector which written in php.
Is it possible to connect to the server only with this server information if the server owner not allow to my IP address for mysql connection?
No, it's not possible (with MySQL server default configuration) to connect to a remote MySQL server instance if your user id does not have permission to connect from the remote IP to MySQL server.
If you try to connect to local MySQL server these information would suffice but if you try to connect to a remote host with these information you might get an error like this:
"Connect failed: Access denied for user 'root'#'aa.bb.cc.dd' (using
password: YES)"
This indicates that your user id does not have permission to connect to the MySQL instance from this remote IP address.
But again you can try to connect to the remote MySQL server and get a connection back. It all depends on the remote MySQL server configuration.
The above information is enough to connect to MySQL server through php.
But if the server owner, has disallowed your IP for MySQL port, then you will not be able to connect.
But maybe that isn't the case, the IP disallowing can be application specific.
eg. they can disallow outside IPs to access there FTP and SSH but their MySQL port can be open for outside IP addresses hence can be accessed by you.

Can not connect to my mysql database

I have been recently using "localhost" as my mysql database, and now i want to update my website to web.
I took free hosting at "serversfree.com" and uploaded all my web files there.Now i have problem.I made mysql database on that website, and uploaded my mysql database from localhost.
I can go to that server and list all files i have in that mysql database, but when i want to connect my .php documents i don't know how.What to put under "hostname" (i was recently using only localhost).Do i need to put a link where my web mysql database is?
Or i need to put a link where my website is?
I tried both but didn't worked for me
Most web hosts I've used have MySQL running on the same box so I usually can keep localhost as the server.
Your host should have some documentation on where to point the server to.
Check if your database credentials, are from a valid user in that database..
What hostname did you use to upload your mysql database from localhost? It would be the same hostname. Probably mysql.serversfree.com. If their mysql server is on the same subdomain then you can use localhost as well. To connect your php scripts to a mysql server you need to use the hostname of the mysql server, not the hostname of your http server.
If your mysql is running in the same server then you can use localhost or 127.0.0.1 but if mysql is in another server probably you need remote connections

How to connect to WordPress database remotely?

I have a WordPress website, which is not done by me, all I need to do is connect to its database remotely and query some results from its database. The point is that I don't know anything about WordPress, and I saw that the database information are written inside the wp-config.php file, when I opened it, there was the username and password, but the host name was this:
define('DB_HOST', 'localhost');
Which seems a bit curious to me. The website uses cPanel X from GoDaddy, and when I logged in there and opened phpMyAdmin, it had these information under database server part:
Server: Localhost via UNIX socket
So, I really don't know how to remotely connect to the database and query things, because host name says localhost. I'm used to ASP.NET MVC, and where I specify the host name to be the actual server ip or hostname of the database server. Any idea how should I proceed?
This is probably a better question for superuser as it's technically a hosting related question. That being said, you would need to contact GoDaddy and find out if they even allow remote MySQL connections on their servers. Most hosting companies disable external connections to the MySQL server on port 3306, but if you portscan your domain name/IP and see 3306 open then you can probably remote connect to it. Just use the domain name/IP of your site as the host, and pull the user/pass/db name from the config file.
You can do this by using command line.
GRANT ALL ON wpdb.* TO wpuser#`192.168.2.25` IDENTIFIED BY `Pa55w0rd!`;
Check here for a detailed tutorial.

Host my wordpress site locally using the server database

I have set up my wordpress site on my local machine and I would like it to talk to the live mysql database on the server. I accessed the wp-config.php file on my machine and changed the hostname to use the ip address instead of localhost, but it will not work.
What do I need to do?
It could be that your remote database is configured to accept connections only from localhost for security reasons. Most web providers set it up that way. In that case, you have no chance of making this work.
Anyway, even if you would get it to work, you will encounter two problems: It will be awfully slow, and the HTML served by the remote database will contain references to server URLs (as opposed to local ones).
If you need more detailed information, you will need to post any error messages you get from mySQL.
Alternatively, you can try running the mysql command-line utility to connect.
mysql -u username -h server -p wordpress_database
Make sure that works first before attempting to get Wordpress to connect.
My guess is you won't be able to connect due to firewall issues. MySql uses port 3306 by default, so if the server's firewall doesn't allow connections through that port, you'll either have to change the port mysql is using (probably a bad idea if other apps expect to see MySql on that port) or get that port opened on the server (make sure you only open it for local IPs, so someone else can't get to your MySql instance!)

How can I connect to MySQL database on a dedicated Windows 2003 server from EC2 based app server with PHP

I have developed a web application that uses PHP and MySQL and has all been running fine from a single development server. I now want to separate things a bit and place an app server within Amazons cloud that will receive and process uploaded files. The database is going to remain on the dedicated server but will need to be accessed by the EC2 instance.
Heres what I have tried so far...
In phpMyAdmin I added a new user to the privileges table. I gave it the elastic IP address from AWS as the host name, a new password and granted privileges to INSERT, SELECT and UPDATE.
In the PHP scripts on my EC2 based app server I included this username, password and the domain name of the location of the mysql server (example.domain.net) when connecting with mysql_connect
I have a simple test page that tries to SELECT and echo some results from the database but get the following error -
Could not connect to MySQL: Can't connect to MySQL server on 'example.domain.net' (4)
What could this mean, what have I missed, are there any other issues such as trying to do this from within EC2 that will cause other problems?
cheers all
Turns out the only other thing I needed to do was to open port 3306 on the database server to allow access. Did this by going to Control Panel -> Windows Firewall -> Exceptions -> Add Port ["SQL", 3306]
Now it works great!

Categories