PHP-MySQL connection string

Using the Host

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

Using Hostname

<?php
// we connect to example.com and port 3307
$link = mysql_connect('example.com:3307', 'mysql_user', 'mysql_password');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);

// we connect to localhost at port 3307
$link = mysql_connect('127.0.0.1:3307', 'mysql_user', 'mysql_password');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
?>

Using Socket Path

<?php
// we connect to localhost and socket e.g. /tmp/mysql.sock

// variant 1: omit localhost
$link = mysql_connect(':/tmp/mysql', 'mysql_user', 'mysql_password');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);

// variant 2: with localhost
$link = mysql_connect('localhost:/tmp/mysql.sock', 'mysql_user', 'mysql_password');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
?>

How to add mysql user manually

It may too easy for the mysql expert out there, it just a simple sharing to the Mysql Amateur user.

If you have SSH, just type “mysql” then you will be in mysql command screen

(1) First create user first

CREATE USER ‘dbuser‘@’localhost’ IDENTIFIED BY ‘password‘;

(2) Assign it to the database

GRANT ALL ON db.* TO ‘dbuser‘@’localhost’;

Note:
db: your database name
dbuser: your database username
password: the password

any database in your addon domain? (cPanel)

Well, today learn some trick from my senior. If there is any database use by any addon domain, instead of ask the owner, there is another trick > check their script if there is any database name. For those cpanel with have prefix username infront, then you can use the command as below.

grep -H -r “username_” *

Grep it and there you are!