0

I have Xampp installed in windows and I am creating an application using Laravel 5.3. I am trying to execute a query on another server on local network but when I try to do that the MySql server authenticate the user that is on my local server with is (username: "root" && password:"") while the remote server have (username: "root" && password:"root") and i don't know why. here is my laravel connection under config/database.php

'smsgateway' => [
                'driver'    => 'mysql',
                'host'      => '**.**.**.**',
                'database'  => 'database',
                'username'  => 'root',
                'password'  => 'root',
                'charset'   => 'utf8',
                'collation' => 'utf8_unicode_ci',
                'prefix'    => '',
                'strict'    => false,
    ],

how i use the connection

  $smsgateway =  \DB::connection('smsgateway');
        // dd($smsgateway);
        $smsgateway->statement($sql);

I tried to connect using a native PHP code but I face the same problem here is my code

$servername = "**.**.**.**;
$username = "root";
$password = "root";

try {
    $conn = new PDO("mysql:host=$servername;dbname=database", $username,      $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "Connected successfully"; 
    }
catch(PDOException $e)
    {
    echo "Connection failed: " . $e->getMessage();
    }

it gives me

Connection failed: SQLSTATE[HY000] [1045] Access denied for user 'root'@'myIPAddress' (using password: YES)

jophab
  • 5,356
  • 14
  • 41
  • 60
  • your error message "Access denied for user" says all. – Eugen Feb 05 '17 at 08:36
  • my college uses the same code and it is working perfectly. my code tries to connect to it using my local root user. i mean the error is ` Connection failed: SQLSTATE[HY000] [1045] Access denied for user 'root'@'**.**.**.115' (using password: YES)` that ip is my machine ip while the server that i try to connect to is '**.**.**.20' – user3055120 Feb 05 '17 at 08:40
  • if it work's from same pc, but not from remote pc, you need to set permission for remote access in your DB. – Eugen Feb 05 '17 at 08:45
  • i have suggestion make another user for your mysql and check it's work or not – Alireza Feb 05 '17 at 08:46
  • @Eugen it works from the same PC and from another one, but not from mine !! – user3055120 Feb 05 '17 at 08:54
  • the code works fine, but i get "access denied" error then i set false login data. – Eugen Feb 05 '17 at 09:03
  • http://stackoverflow.com/questions/28073934/pdoexception-sqlstatehy000-1045-access-denied-for-user-rootip-using – jophab Feb 05 '17 at 10:18

3 Answers3

2
  1. You need to grant the permission to access the database from local
  2. Use these commands and then revert , help url here grant remote access of MySQL database from any IP address
Community
  • 1
  • 1
Sabarish
  • 126
  • 1
  • 10
0

In my own case, due to valid security concerns, i opted to develop an API to connect to the remote mysql database. I did this using php's CURL library. On the foreign domain (the domain you are connecting from), i did a curl post to the database domain (the domain holding the database).

From here its quite easy as all you do is save the $_POST parameters using prepared statements on the local database. I just thought to put this answer here. It might help someone out there

For example, you would like to save two values to the remote database.

<?php

$array = array('key1' => $key1, 'key2' => $key2);
$url = 'www.site.com/api.php';    

#do curl post to remote database
$ch = curl_init();
if ($ch !== false) {
    curl_setopt($ch, CURLOPT_URL, $url); //this is the url of the domain where the database is being hosted
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json')); //if you are returning json
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($array));
    $response = curl_exec($ch);
    curl_close($ch);
    return $response;
}
#on the #url script, all you do is access the values via `$_POST`. example: `$_POST['key1'];`
?>
Rotimi
  • 4,783
  • 4
  • 18
  • 27
-1

Don't use root in password. Password field should be blank on your XAMPP set up.