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)
You need to grant the permission to access the database from local
Use these commands and then revert , help url here
grant remote access of MySQL database from any IP address
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'];`
?>
Don't use root in password. Password field should be blank on your XAMPP set up.
Related
i am getting following error but all settings are correct no issue but advice if someone has done smtp setting of 365 office
connectors are added in admin center and changed the max sp as advised
authentication failure [
SMTP: Invalid response code received from server
(code: 535, response: 5.7.3 Authentication unsuccessful
[LO2P265CA0220.GBRP265.PROD.OUTLOOK.COM]
)
]
here is code:
<?php
require_once "Mail.php";
$from = '<info#domain.com>';
$to = '<dodmaon#domani.com>';
$subject = 'Insert subject here';
$body = "Hello world! this is the content of the email";
$headers = array(
'From' => $from,
'To' => $to,
'Subject' => $subject
);
$smtp = Mail::factory('smtp', array(
'host' => 'smtp.office365.com',
'SMTPSecure' => 'tls',
'port' => '587',
'StartTLS' => true,
'auth' => true,
'username' => 'info#domain.com',
'password' => '321password'
));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo '<p>'.$mail->getMessage().'</p>';
} else {
echo '<p>Message successfully sent!</p>';
}
?>
If you are very sure that all settings including Username and Password are correctly configured, then the problem is a Microsoft thing. And how to solve it is this;
you need to login from a browser (https://accounts.live.com) using the username and password you have configured in your application.
While logged in, go to "security and privacy" and look for the link to "recent activity" alternatively, you can just enter this address after logging in to go there directly https://account.live.com/Activity.
From here you should see the attempts from the email you are sending from and click the option "This was me" next/under to it.
Also consider disabling 2step authenticator if on.
When you have completed this, wait a few minutes and then try again.
Let me know if this worked. :)
I want create database with yii2.
$servername = "localhost";
$username = "username";
$password = "password";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Create database
$sql = "CREATE DATABASE myDB";
if ($conn->query($sql) === TRUE) {
echo "Database created successfully";
} else {
echo "Error creating database: " . $conn->error;
}
$conn->close();
With this code we can create DB but i want to know whether it is possible in Yii2 manner
Currently there is no way you can do it with Yii2. It is because too much complexity for a framework to have everything works on many database engines (MySQL, Oracle, MSSQL, etc). Too many efforts to code, yet the functionality can be done using your query.
IMO, there is nothing wrong with creating a database using current db connection. This can be found in SaaS multi-tenant architecture, when a new client registers an account, then a new separated DB created to encapsulate mostly everything / security reason.
It's not recommended to create tables at run-time. Already set-up the schema and run your SQL / non-SQL query.
For this, you need to create a file (say myDB.php) in your config folder and provide database details as follows :
<?php
return [
'class' => 'yii\db\Connection',
'dsn' => 'dblib:host=localhost;dbname=yourDBName',
'username' => 'yourUsername',
'password' => 'yourPassword',
'charset' => 'utf8',
];
Register this database in your web.php.Add following line :
'my_db' => require(__DIR__ . '/myDB.php'),
In controller, use this db as follows :
$query = 'select columnID from table where ID=:ID';
Yii::$app->my_db->createCommand($query)
->bindValue(':ID',1)
->queryOne();
Just figured this out and wanted to share.
I am able to connect to MySQL using SSL with the PHP PDO from my local machine. The same code fails when run from a Google Compute Engine instance. I know the certs and IP address are setup correctly because connecting via the MySQL command line client using SSL on the instance works perfectly.
MySQL command line works on Compute:
mysql --ssl-ca=server-ca.pem --ssl-cert=client-cert.pem --ssl-key=client-key.pem \
--host=111.111.111.111 --user=someuser --password
PHP PDO does not work on Compute:
<?php
new PDO ('mysql:host=111.111.111.111;port=3306;dbname=mydatabase',
'someuser',
'somepassword,
array(
PDO::MYSQL_ATTR_SSL_KEY => '/somedir/ssl/client-key.pem',
PDO::MYSQL_ATTR_SSL_CERT => '/somedir/ssl/client-cert.pem',
PDO::MYSQL_ATTR_SSL_CA => '/somedir/ssl/ca-cert.pem'
)
);
PDO gives this error on Compute:
PHP Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2026] SSL connection error: ASN: bad other signature confirmation'
Remove the CA cert from the PDO connection.
Remove this key/value from the array:
PDO::MYSQL_ATTR_SSL_CA => '/somedir/ssl/ca-cert.pem'
Like so:
<?php
new PDO ('mysql:host=111.111.111.111;port=3306;dbname=mydatabase',
'someuser',
'somepassword,
array(
PDO::MYSQL_ATTR_SSL_KEY => '/somedir/ssl/client-key.pem',
PDO::MYSQL_ATTR_SSL_CERT => '/somedir/ssl/client-cert.pem'
)
);
PDO with SSL will silently fail back to an insecure connection under certain conditions. To test that SSL is working run this query on the connection:
SHOW STATUS LIKE 'Ssl_cipher';
It should return something like:
Ssl_cipher => DHE-RSA-AES256-SHA
Otherwise the it will return an empty value.
Just add one more PDO attribute (as per comments by Desislav Kamenov in http://php.net/manual/en/ref.pdo-mysql.php) into your array so that your PDO becomes ...
new PDO ('mysql:host=111.111.111.111;port=3306;dbname=mydatabase',
'someuser',
'somepassword',
array(
PDO::MYSQL_ATTR_SSL_KEY => '/somedir/ssl/client-key.pem',
PDO::MYSQL_ATTR_SSL_CERT => '/somedir/ssl/client-cert.pem',
PDO::MYSQL_ATTR_SSL_CA => '/somedir/ssl/ca-cert.pem',
PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT => false
)
);
I want to use SSH tunnel to connect to MySQL DB on remote host.
I've set up the tunnel with command:
ssh user#host -L 3307:remote_mysql_hostname:3306
I can successfull connect with HeidiSQL using this settings:
hostname: localhost
user: remote_mysql_user_login
password: remote_mysql_user_password
port: 3307
But when i use PDO in PHP to connect, i get:
Access denied for user 'remote_mysql_user_login'#'localhost' (using password: YES)
My PDO Dns is sth like this:
mysql:type=Core_Db_Adapter_Pdo_Mysql;host=localhost;port=3307;dbname=db_name;
Where is the trick?
Solution:
#symcbean thanks!
The problem was (as suggested by #symcbean) in hostname.
Changing to '127.0.0.1' fix the problem.**
I borrowed from your example and was able to connect to a remote host using your same SSH tunnel setup. Can you share the PHP script you are using to get some more info about your setup?
You may try a hostname other than 'localhost' for your remote database you are connecting to.
setup ssh tunnel - I setup an entry in /etc/hosts for my_db_host_name instead of using localhost
ssh my_user#my_remote_host -L 3307:my_db_host_name:3306
<?php
$dsn = 'mysql:host=my_db_host_name;dbname=my_db;port=3307';
$username = 'my_user';
$password = 'my_pass';
$options = array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
);
$dbh = new PDO($dsn, $username, $password, $options);
$result = $dbh->query("select * from my_table");
try {
foreach($result as $row) {
echo "column1: " . $row['column1'];
}
} catch (PDOException $pde) {
echo "PDO exception: $pde";
}
echo "done \n";
?>
I have my own mysql_connect ...etc until i wanted to use ZEND framework in particular with Zend_DB .How do I pass my connection to be used as an adapter to ZEND?
$myconn = mysql_connect('...blab',blah etc...)
eg. Zend_DB_table::setAdapter($myconn);
Don't connect to DB on your own, rather use the factory
$db = Zend_Db::factory('Pdo_Mysql', array(
'host' => '127.0.0.1',
'username' => 'webuser',
'password' => 'xxxxxxxx',
'dbname' => 'test'
));
This way you can connect to DB, but it will connect only once you need the connection and thus optimize for performance...