Mysql | How to access database remotely

|
| By Webner

To access Mysql database remotely a good way is to create a new Mysql user and grant it permissions to access database from remote, as explained below:

You can create user with two ways

From Mysql command prompt:

1. Open command prompt.

2. Open the terminal of MYSQL in wamp using following command:

MYSQL -u root -p password

3. Create a user with following command:

CREATE USER 'yourUsername'@'%' IDENTIFIED BY 'yourPassword';

Note: % for accessing from any host

4. Grant privileges to user:

GRANT ALL PRIVILEGES ON *.* TO 'yourUsername'@'localhost' IDENTIFIED BY 'yourPassword' WITH GRANT OPTION;

User created successfully.

Note: You can give lower permissions as required. This statement is just for an example.

Using PHPmyadmin GUI:

1. Open PHPMyAdmin.

2. Click on privileges as shown below:

3. Add a new user as shown below:

Enter the username, Host (% if any host or local if localhost) and password. You can generate an automatic password (if you want)

4. Grant privileges:

You can give selective privileges too.

5. Click on Go.

You can also use this user information in your code to access the database programmatically,example of PHP code is given below:

$servername = "xx.xx.xxx.xx";//ip address
$username = "username";
$dbname = "databasename";
$password="password";
$conn = new MYSQLi($servername, $username, $password, $dbname);
if ($conn->connect_error)
{
die("Connection failed: " . $conn->connect_error);
}

Leave a Reply

Your email address will not be published. Required fields are marked *