This site requires JavaScript to be enabled
Welcome Guest|
Recent searches

MySQL Connection String Examples

Number of views : 43
Article Number : KB0011582
Published on : 2020-07-15
Last modified : 2020-07-15 19:05:10
Knowledge Base : IT Public Self Help


This page contains examples of MySQL connection strings that will allow you to connect with various applications, including Django, Drupal, Perl, PHP, Python, and Wordpress. The connection parameters for each example are as follows:

  • Database Name: myDatabase
  • Database User: myUsername
  • Database Password: myPassword
  • Database Host:
  • Database Port: 3306

Note that applications should not use the administrative user name (ie. dept100) to connect to the database. They should only use the database user name (ie. dept100_sample) to connect. This is because the administrative user name has elevated permissions that the users of your application should not have access to.

All connections to your database must be encrypted. In order to make an encrypted connection, you will need to specify the MySQL SSL certificate (either the CA file or alternatively the client certificate and key) in the connection string. If your application is hosted in a shared environment, the SSL certificate may already be available. Ask the administrators of the shared environment for its location. If the shared environment does not have the certificate or if your application is hosted on your own server, you will need to download the certificate yourself.


Django Connection String

The Django MySQL connection string should be placed in your file. Modify the file’s existing DATABASES section using the information for your database, host, and port.

  'default': {
    'ENGINE': 'django.db.backends.mysql',
    'NAME': 'myDatabase',
    'USER': 'myUsername',
    'PASSWORD': 'myPassword',
    'HOST': '',
    'PORT': '3306',
    'OPTIONS': {
      'ssl': {
        'ca': '/path/to/ca-mysqldb02-cert.pem',


Drupal Connection String

The Drupal MySQL connection string should be placed in your sites/default/settings.php file. Modify the file’s existing $databases array using information for your database, host, and port. Note that this syntax requires PHP 5.3.7 or later and Drupal 8 to make encrypted connections to MySQL.

$databases['default']['default'] = array (
  'database' => 'myDatabase',
  'username' => 'myUsername',
  'password' => 'myPassword',
  'host'     => '',
  'port'     => '3306',
  'driver'   => 'mysql',
  'prefix'   => '',
  'driver options' => array(
    PDO::MYSQL_ATTR_SSL_CA => '/path/to/ca-mysqldb02-cert.pem',


Perl Connection String

The Perl MySQL connection string requires both the Perl Database Interface (DBI) and the Perl MySQL driver (DBD::mysql).

use DBI;
use DBD::mysql;

$db = "myDatabase";
$host = "";
$port = "3306";
$user = "myUsername";
$pass = "myPassword";

$key = "/path/to/mysql.key.pem";
$cert = "/path/to/mysql.cert.pem";
$ca = "/path/to/ca-mysqldb02-cert.pem";

$ssl = "mysql_ssl=1;mysql_ssl_client_key=$key;mysql_ssl_client_cert=$cert;mysql_ssl_ca_file=$ca";

my $dsn = "dbi:mysql:database=$db;host=$host;port=$port;$ssl";
my $dbh = DBI->connect($dsn, $user, $pass, { RaiseError => 1 }) or
  die "Could not connect to MySQL: $DBI::errstr";


PHP Connection String

The MySQL Improved Extension (mysqli) can be used to connect to MySQL from PHP.

  $db = "myDatabase";
  $host = "";
  $port = "3306";
  $user = "myUsername";
  $pass = "myPassword";

  $dbh = mysqli_init();
  $dbh->ssl_set(NULL, NULL, '/path/to/ca-mysqldb02-cert.pem', NULL, NULL);
  $dbh->real_connect($host, $user, $pass, $db, $port, NULL, MYSQLI_CLIENT_SSL_DONT_VERIFY_SERVER_CERT);

It is strongly recommended that the database credentials be placed in a separate include file (.inc) that is not world-readable. For example, in Linux the permissions on this include file would be 640. It is also recommended that access to this include file be restricted in Apache. One way of accomplishing this is with a .htaccess file. For example, you could include the following:

<Files *.inc>
  Order Allow,Deny
  Deny from all


Python Connection String

The MySQLdb Python interface can be used to connect to MySQL from Python.

import MySQLdb

ssl_settings = { 'ca': '/path/to/ca-mysqldb02-cert.pem' }

dbh = MySQLdb.connect(host="",



The Wordpress MySQL connection string should be placed in your wp-config.php file.

/** The name of the database for WordPress */
define( 'DB_NAME', 'myDatabase' );

/** MySQL database username */
define( 'DB_USER', 'myUsername' );

/** MySQL database password */
define( 'DB_PASSWORD', 'myPassword' );

/** MySQL hostname */
define( 'DB_HOST', '' );


Thank You! Your feedback has been submitted.