Hi,

I’m a newbie to databases. What I’ve done is:

  • setup PhpMyAdmin Droplet
  • logged onto PhpMyAdmin using ‘admin’ user and a certain password 'password’, and I created a database called 'test’
  • connected using WinSCP & uploaded php file
  • run the php file as follows:

<?php
$con = mysqliconnect('localhost’, 'admin’, 'password’, 'test’) or die(“Connection failed: ” . mysqliconnect_errno());
?>

Somehow, now matter what I try, it keeps giving me a connection error.
Am I doing something wrong?

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
1 answer

MySQL users are identified by a username and host. Additionally you should only be using your MySQL admin account to administer the MySQL server itself but not to access various databases and not to read and write information that are connected to by apps. This is for security.

Since you have created a database already called test you will want to create a new user that will access that database and read/write data to it.

To do that login to mysql as your admin account and create a user with access:

CREATE USER 'testuser'@'localhost' IDENTIFIED BY 'replace-with-desired-password';
GRANT ALL PRIVILEGES ON test.* TO 'testuser'@'localhost';

Here you are creating a new user called testuser which can connect from localhost with the password you specified.

Then you are granting that user privileges to access, read, write, etc to the database test.*. When you write test.* that means you are giving this user access to the database test, and the * means all tables for that database.

You can also find more in depth information about users and permissions here:
https://www.digitalocean.com/community/tutorials/how-to-create-a-new-user-and-grant-permissions-in-mysql

by Etel Sverdlov
MySQL is a powerful database management system used for organizing and retrieving data. This tutorial explains how to to create new MySQL users and how to grant them the appropriate permissions.
Submit an Answer