How do I connect from php to mySQL using SSL?

Posted on 2008-11-13
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?
Question by:novocent
    LVL 3

    Expert Comment

    This is a simple example.

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

    LVL 107

    Expert Comment

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

    Author Comment

    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.
    LVL 3

    Assisted Solution

    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.
    LVL 3

    Assisted Solution

    Here is the information on connect PHP with a certificate.

    Here is the information on setting up MySQL certificates.\

    Author Comment

    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?

    LVL 3

    Accepted Solution

    Sounds like you have it work.  MYSQL_CLIENT_SSL is a good function.

    Use WireShark to confirm the connection is encrypted.

    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.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
    These days socially coordinated efforts have turned into a critical requirement for enterprises.
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    734 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

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now