I have a website hosting on www.host1free.com. Earlier I had connected to MySQL database of the host.
Now I am having my own VPS which is accessed by ip address 5.231.36.181. I have also installed phpmyadmin. All works fine within the server.
Now I want to connect my website database to the MySQL server on 5.231.36.181.
The database on the server is accessed by 5.231.36.181/phpmyadmin
and the database on host is accessed by sql12.1freehosting.com/phpmyadmin
How to connect the database to my VPS phpmyadmin ?
whatever the details you are using for logging in to your phpmyadmin use the same usename and password for connecting to database
secondly host can be localhost or your VPS IP Address
The rectangle marked in red contains your IP ,
Use the IP Address of the server and Username and password that you used to login if you are still not getting it post us what you have tried and what error you are getting
connecting to mysql database
// Define database connection constants
define('DB_HOST', 'your IP');
define('DB_USER', 'Your DB USER');
define('DB_PASSWORD', 'db user Password');
define('DB_NAME', 'your dbname');
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
or die('cannot connect to MYSQL');
if you are confused you can create a new database user assign the database to the particular user and can use the details to connect to your DB Cheers
You connect your website to MySql database, not to PhpMyAdmin. Enter Host as localhost, username and password. If localhost is not working, try entering IP of database. There must be statet somewhere what params to use to connect to database
Use Your Mysql Details and connect to your dataBase like this:
mysqli_connect('5.231.36.181',username,password,dbname);
Related
I created a php website and host it online by using Heroku recently. However, I couldn't connect my database because I use localhost phpmyadmin database from my online site. How should I connect my localhost database from my online site? Or how to make my host my database online, and then make the connection? Here is my db.php file to make the connection with mysqli, in case if there's any need to tweak the db connection PHP codes.
//step 1: Establish database connection
DEFINE("DB_HOST", 'localhost');
DEFINE("DB_USER", 'root');
DEFINE("DB_PASS", '');
DEFINE("DB_NAME", 'my_db_name');
// Create connection
$con = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
// Set charset to UFT8
mysqli_set_charset($con, "utf8");
// Check connection
if (!$con) {
die("Connection failed: " . mysqli_connect_error());
}
If you are hosting your database on your local machine, whilst having a webserver that needs to connect to your database, you will have to make sure your database can be accessed by another machine. The host is only ‘localhost’ if the Local IP is identical for the machine and database. Your db.php is correct and will function properly, as long as you ensure that the connection details are correct, since localhost is not correct there will be errors.
You might want to look into how to portforward depending on what database software/installation you are using. Or look into whether your host offers database solutions sch as or similar to PhpMyAdmin (in case of most webhosts).
I am using windows and trying to develop an app in php where I connect a database mySQL created on mySQL workbench. I connect with root and a saved pass. But it seems I cannot connect to the page afterwards.
I use Windows 10.
<?php
define('db_user', 'what is the user?');
define('db_pass', 'mypass');
define('db_host', 'localhost');
define('db_name', 'movies');
&db_conn = mysqli_connect(db_host, db_user, db_pass, db_name);
if(!&db_conn){
die('error connecting to database');
}
echo 'you have connected succesfully';
?>
My database is called movies. I don't know what is the user?
phpMyAdmin is not a database; it is just one of the tools you use to manage what is inside. Your database is MySQL, which should be started and listening on a particular port. The default is usually 3306 or for MariaDB 3307. When MySQL server is located on the same machine as your web server, you can connect via localhost or 127.0.0.1.
If you haven't set up any users in you MySQL server, then the default usually is root with no password. Check your users using this command:
SELECT * FROM mysql.user;
The user must also be authorized for an access via either localhost or IP, whichever you use.
Then to open a connection to MySQL in PHP using the mysqli class, you would usually use this code:
<?php
// your connection code
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = new mysqli('localhost', 'root', '', 'movies');
$mysqli->set_charset('utf8mb4');
Here is my code for PHP for connecting to the database, a Cloud SQL instance for MySQL:
define('DB_SERVER', '192.0.0.1');
define('DB_USERNAME', 'username');
define('DB_PASSWORD', 'password');
define('DB_NAME', 'db');
$db = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME) or die(mysqli_connect_error());
Obviously the DB_SERVER is not the actual IP address, nor are the credentials, but I did put in a dummy one to see if I needed to add anything like cloudsql:/ or jdbc:// like you do in Java.
I did authorize the IP address for the webserver on Cloud SQL, but all it returns is a Connection Timed Out error; What can I do to fix this?
I'm certain that the credentials are correct, and that I can connect from other authorized IP addresses, as I have been able to access from another account & multiple IP addresses through the MySQL workbench.
Solved:
For anyone using Siteground's services, make sure to add 0.0.0.0/0 as the accepted IP addresses if you're using public IP authorization. Then find what IP's are connected when you refresh the php page by running this right after the page is refreshed:
SELECT host FROM information_schema.processlist WHERE ID=connection_id();
It should give you multiple IPs, and check them online using an IP locator to see if they match the location of the Siteground's services. Remove 0.0.0.0/0 and add that IP address to Google Cloud SQL.
Its not connecting to my web hosting server database even though all the files are in server and all the credentials( username, password, database and host-name ) are updated! why its still connecting to my local machine database? even though i changed the username and password, its connecting to my local database itself! how's that possible?
I created another new database and tables in my server database. everything is done! but connecting to my local database!!
<?php
header("Access-Control-Allow-Origin: *");
define('DB_NAME', 'asknow');
define('DB_USER', 'myself');
define('DB_PASSWORD', '*****');
define('DB_HOST', '134.345.**');
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
?>
That is because you set your host as localhost, which means your computer here.
define('DB_HOST', 'localhost');
That is why your data still goes to your local database.
Instead, you should set your database's ip on your shared hosting as your host.
---- Edited ---- (because the question is edited)
You should use your dns like www.yourdns/connect.php to link to your connect.php file in your shared hosting.
I want to connect with external database using PHP. But, it gives this error
Warning: mysql_connect(): Can't connect to MySQL server on 'SERVER_IP_ADDRESS' (110) in /home/<username>/public_html/index.php on line 7
Can't connect to MySQL server on 'SERVER_IP_ADDRESS' (110)
Here is my code.
define("_CONFIG_DB_HOST", "SERVER_IP_ADDRESS"); /* database host name */
define("_CONFIG_DB_NAME", "DATABASE_NAME"); /* database name */
define("_CONFIG_DB_USER", "DATABASE_USERNAME"); /* database username */
define("_CONFIG_DB_PASS", "DATABASE_PASSWORD"); /* database password */
$con = mysql_connect(_CONFIG_DB_HOST,_CONFIG_DB_USER,_CONFIG_DB_PASS) or die(mysql_error());
mysql_select_db(_CONFIG_DB_NAME,$con) or die(mysql_error());
I've also added entry for Remote Database Access Hosts in Host Server
Please guide me if I'm doing anything wrong.
Make sure DATABASE_USERNAME has access from you host (the machine running this script) has access to DATABASE_NAME on SERVER_IP_ADDRESS.
When you grant access on database to use you would use USERNAME#HOST form. Here the HOST means the host where this USERNAME would be connecting from. Usually we keep it localhost. becasue we want to connect by a user who is in localhost according to mysql server. But your local host is not mysql servers localhost. That's why that host part needs to be changed.