I am trying to query a database on my website’s hosting service (HostWinds). However, when I run the following code nothing happens. No error, no console logs, nothing.
I have MySQL installed, and I’m using the same username and password I use to login to HostWinds. I am also pretty sure I have the right database name. Is my host misnamed? I am not really sure what to use since ‘localhost’ can’t be correct, right? WebMaxLabs.com is listed as my ‘Main Domain’ in my cPanel. So I figured it might be my host, but I am really not sure.
I found this code from W3 schools guide on MySQL and Node.js.
var mysql = require('mysql');
var con = mysql.createConnection({
host: "webmaxlabs.com",
user: "abc",
password: "123",
database: "USER_wp4"
});
con.connect(function(err) {
if (err) throw err;
con.query("SELECT * FROM wp_posts", function (err, result, fields) {
if (err) throw err;
console.log(result);
});
});
Any feedback or information would be very much appreciated. Thank you!
2
Answers
localhost should work fine.
the username and database name typically have your username_ prepended to it. A database should be create first, then create a user and give it permissions to the database by adding the user.
Looks like you’re trying to access a WordPress Database?
Try accessing the file
wp-config.php
via the CPanel file manager tool, at the web-root. You should see something like this near the top:Since PHP is running on the same server as MySQL it is localhost for PHP, but since you’re connecting remotely you’ll need an address that resolves to that server.
Check on your hosting provider cPanel for the actual IP address of the server. It might be different depending on proxies, CDN’s & redirects from the actual web address. On my cPanel the IP address is listed in an info block top left of the collection of tools.
Its possible that the MySQL instance is only listening on localhost, and is set up not to listen to external addresses, or alternatively that the users that are on it are set to only have access from localhost.
From there if you use the command line client
mysql
you could try connecting:Obviously replace the 123.456.789.10 with your actual IP address.
If the mysql command line tool gives you errors like that try using the "Remote MySQL" tool on cPanel to add your workstation as a valid host.