I'm trying to connect to a mysql database from a different server to the one the database is hosted on but I'm getting an error.
I'm guessing it might be something to do with remote permissions. Is this something I can change or do I need to get my hosting company to do this?
<?php
$con = mysql_connect("mysql4-remote.hosting.net","myadmin","password123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
?>
Can't connect to MySQL server on 'mysql4-remote.hosting.net' (10060)PHP Warning: mysql_connect() [function.mysql-connect]: Can't connect to MySQL server on 'mysql4-remote.hosting.net' (10060) in D:\websites\abc123\www\test.php on line 2
I would say, that your login credentials or Network settings are wrong:
See this post for further help:
http://wiki.answers.com/Q/What_does_MySQL_error_number_10060_mean
If you don't have access from outside use tunneling, here is an example:
$connection = ssh2_connect('SERVER IP', 22);
ssh2_auth_password($connection, 'username', 'password');
$tunnel = ssh2_tunnel($connection, 'DESTINATION IP', 3307);
$db = new mysqli_connect('127.0.0.1', 'DB_USERNAME', 'DB_PASSWORD',
'dbname', 3307, $tunnel)
or die ('Fail: '.mysql_error());
You have to give the permission to remote user by inserting the particular remote machine IP or wildcards in USER table in mysql.
This link will help you. Click Here
Related
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');
I have tried to connect to Amazon EC2 instance MySql from localhost.
Tried:
Granting privilege for user for remote connect
Made domain ping-able from outside.
Able to login through phpmyadmin with same credentials
Gone through lot of Stackoverflow answers.
$link = mysqli_connect('ec2-12-223-36-172.compute-1.amazonaws.com', 'myuser', 'xxxxxxxxxxx');
// mysqli_connect('domain', 'myuser', 'pasword', 'dbname', 3306);
if (!$link) {
die('Could not connect');
}
echo 'Connected successfully';
exit;
Warning: mysqli_connect(): (HY000/2002): Connection refused
Warning: mysqli_connect(): (42000/1044): Access denied for user '-----'#'localhost' to database '----' in ---.php on line 4
Unable to connect to MySQL
$db = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) or die("Unable to connect to MySQL");
This is how I connect. If I remove DB_NAME from there, I don't get an error and connect database but whenever I add DB_NAME, I get this error. DB_NAME is definitely correct but I don't see why? By the way for DB_HOST, I use localhost.
---Solved---
I finally solved the problem. So, for those of you who have the same problem, I am gonna explain everything. Actually, I decided to use table name before my query and than mysqli gave me another error saying something like "INSERT command denied to user ..." So, I opened up phpMyAdmin and changed my DB_USER to the name that is written in Database Server section in phpMyAdmin. You will see something like this;
Server: Localhost via UNIX socket
Server type: MySQL
Server version: 5.5.42-cll - MySQL Community Server (GPL)
Protocol version: 10
User: -HERE-
Server charset: UTF-8 Unicode (utf8)
So you need to use use the username, which is written in User part. After I changed my DB_USER I was able to add DB_NAME. Basically all the problems I was having (not being able to use DB_NAME and not being able to use sql commands) caused my permission.
The key part is; I was using DB_NAME, which I used to create database in my server and it also let me connect to the database but As I can see it is not the root. So, if you are having "denied" error, make sure that you check the values you typed in mysqli_connect!
You can use
# mysqli_connect(hostname, username, password, database)
or
#mysqli_connect(hostname,username, password)
# mysqli_select_db(true, database);
or
$con=mysqli_connect("localhost","my_user","my_password","my_db");
if (mysqli_connect_errno())
{
echo "Failed : mysqli_connect_error();
}
mysqli_select_db($con,"db");
if you are using localhost then try this
<?php
$con = mysqli_connect("localhost", "root", "", "YOUR_DB_NAME");
if (mysqli_connect_error()) {
die ("Could not Connect");
}
?>
and if you are doing this online then replace localhost with host_name root with username "" with password and at last your DB_Name. I hope this would solve your problem :)
I've mysql database and php/apache on two different servers: let's say hostphp.domain.com and hostmysql.domain.com.
On the mysql server I've set a user "my_user" with permissions to connect to "my_database" db from the specific host "hostphp.domain.com".
When I connect to it using mysql_connect it does right. But when I do it via php PDO I get this error:
SQLSTATE[42000] [1044] Access denied for user 'my_user'#'%' to database 'my_database'
I've done some tests and I found the problem is ...#'%', mysql is refusing that connection because "my_user" does not have permission to connect from any host.
Also I've tried to connect using mysql_connect with a wrong password to see the error and I get this:
Could not connect: Access denied for user 'my_user'#'hostphp.domain.com' (using password: YES).
The difference is in ..#'%' and ...#'hostphp.domain.com'.
So that's my question, why php pdo do not declare hostname when connecting to a remote host? (or is doing that wrong).
Thanks and sorry for my english.
Edit.
Some code example, this does not work:
try {
$pdo = new PDO(
'mysql:host=hostmysql.domain.com;port=3306;dbname=my_database',
'my_user',
'my_pass'
);
} catch (PDOException $e) {
die($e->getMessage());
}
but this works ok:
$conn = mysql_connect('hostmysql.domain.com', 'my_user', 'my_pass');
if (!$conn) {
die('Could not connect: ' . mysql_error());
}
You have successfully connected to the host using mysql_connect, but you got no errors because you did not attempt to select the database.
Your user probably doesn't have access to your database.
Try running mysql_select_db("my_database"); after connected to the host and you should get the same error.
I registered on 000webhost and I can connect to mysql db pages that are uploaded there via:
mysql_connect('localhost', 'user', 'pass') or die('Could not connect to database');
mysql_select_db('name') or die('Could not select database');
But it seems it does not allow connecting remotely.
Is there any free host which has mysql db and allows remote connections ?
try heliohost.orgI used it sometime back and they got good service as well.The only problem is the registration get filled too quickly
000webhost does not provide MySQL remote access for free accounts,
have a look, so either go for an upgradation
Or try http://www.freemysql.net for remote free mysql server, But again its free hence very slow,
mysql_connect('SQL**.FREEMYSQL.NET', 'USER', 'PASS') or die('Could not connect to database');
mysql_select_db('DBNAME') or die('Could not select database');
Do not use "localhost"