Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

How do I connect from php to mySQL using SSL?

Posted on 2008-11-13
7
Medium Priority
?
1,197 Views
Last Modified: 2013-12-13
How do I make a connection from a php script to a mysql database using SLL to encrypt the transferred data?
0
Comment
Question by:novocent
  • 4
  • 2
7 Comments
 
LVL 3

Expert Comment

by:mgrennan
ID: 22953196
This is a simple example.

<?php
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
?>

See http://us.php.net/function.mysql-connect
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 22953229
Where are the PHP and MySQL hosts (what URLs)?
0
 

Author Comment

by:novocent
ID: 22953590
mgrennan, I want to connect via SSL to secure the transfer of data between the php server and the mysql server. The mysql server is on a remote host.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Assisted Solution

by:mgrennan
mgrennan earned 2000 total points
ID: 22953854
So this must be a remote connections.  Setting up SSL with MySQL is a completed process involving creating and installing certificates.

Maybe your best best is to tunnel over ssh.  A command line:

ssh -L 3306:mysqlhsot:3306 -l user -N mysqlhost

Your application would then connect to localhost port 3306 and would be ssl connected to port 3306 on the remote server.
0
 
LVL 3

Assisted Solution

by:mgrennan
mgrennan earned 2000 total points
ID: 22953916
Here is the information on connect PHP with a certificate.

http://th.php.net/manual/en/mysqli.ssl-set.php

Here is the information on setting up MySQL certificates.

http://dev.mysql.com/doc/refman/5.0/en/secure-create-certs.html\
0
 

Author Comment

by:novocent
ID: 22954104
mgrennan, I will look into that info. I started to make and installed certificates earlier today, but never completed the process. I came across another idea and it seemed to work. I was hoping to confirm the solution: I used this connection:
$conn = mysql_connect("XXX.XXX.XXX.XXXX", "username", "password",true, MYSQL_CLIENT_SSL);
on the remote mysql database I used:
GRANT ALL PRIVILEGES ON *.* TO 'username'@'remoteIP' IDENTIFIED BY 'password' REQUIRE SSL;
It made a connection which I think is SSL, I know of no way to confirm that. I do know that trying to use only $conn = mysql_connect("XXX.XXX.XXX.XXXX", "username", "password") fails. It seemed so simple that I was doubting the SSL connection. And doubting whether the information transferred from the mysql database to the remote php was actually encrypted.  Any thoughts?



0
 
LVL 3

Accepted Solution

by:
mgrennan earned 2000 total points
ID: 22954537
Sounds like you have it work.  MYSQL_CLIENT_SSL is a good function.

Use WireShark to confirm the connection is encrypted.

http://www.wireshark.org/

Wireshark is a great tool and you should learn how to use it.  The trick to using it in this case is to use a filter to see only the connections between your guest and server.
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By, Vadim Tkachenko. In this article we’ll look at ClickHouse on its one year anniversary.
Backups and Disaster RecoveryIn this post, we’ll look at strategies for backups and disaster recovery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question