Tag: MySQL

Guide: Installing MySQL on Ubuntu 16.04 / 17.10

In this guide we will install and configure MySQL on Ubuntu 16.04 / 17.10. 1. Install MySQL Let’s begin by updating the list of available packages and installing MySQL. sudo apt-get update sudo apt-get install mysql-server You will be prompted to create a password for the MySQL root user. It’s recommended that you use a strong…

Continue reading Guide: Installing MySQL on Ubuntu 16.04 / 17.10

Ubuntu: How to create a new database in MySQL

Firstly, you must log in as root. mysql -u root -p Now you can create a new database. Replace your_dbname with the name of your database. CREATE DATABASE your_dbname; You may want ot grant privileges to this database. GRANT ALL PRIVILEGES ON your_dbname.* To ‘your_username’@’localhost’ IDENTIFIED BY ‘your_password’; Once you have finalised the permissions, always be…

Continue reading Ubuntu: How to create a new database in MySQL

Ubuntu: How to create a new user in MySQL

Firstly, you must log in as root. mysql -u root -p Now you can create a new user. Replace your_username and password with your own. CREATE USER ‘your_username’@’localhost’ IDENTIFIED BY ‘password’; Now you can grant privileges. Replace your_dbname, your_username and your_password with your own details. GRANT ALL PRIVILEGES ON your_dbname.* To ‘your_username’@’localhost’ IDENTIFIED BY ‘your_password’; Once you have…

Continue reading Ubuntu: How to create a new user in MySQL

Database connection in PHP with PDO example

Use the following PHP to connect to your MySQL database using PDO. $db = new PDO(‘mysql:host=localhost;dbname=testdb;charset=utf8mb4’, ‘username’, ‘password’); Replace localhost, testdb, username and password with your own details. Select rows in a table In this example we are going to select the firstname and lastname from a table called members. foreach($db->query(‘SELECT * FROM members’) as…

Continue reading Database connection in PHP with PDO example

Database connection in PHP with MySQLi examples

Use the following PHP block to connect to your database with mysqli function db_connect($host,$user,$pass,$db) { $mysqli = new mysqli($host, $user, $pass, $db); $mysqli->set_charset(“utf8”); if($mysqli->connect_error) die(‘Connect Error (‘ . mysqli_connect_errno() . ‘) ‘. mysqli_connect_error()); return $mysqli; } $mysqli = db_connect(‘localhost’,’username’,’password’,’database’); Replace localhost, username, password and database with your own details. Select rows in a table Here’s…

Continue reading Database connection in PHP with MySQLi examples

How to assign a user to a database through MySQL command line in Linux

Firstly, you must log in as root. mysql -u root -p Now you can grant privileges. Replace your_dbname, your_username and your_password with your own details. GRANT ALL PRIVILEGES ON your_dbname.* To ‘your_username’@’localhost’ IDENTIFIED BY ‘your_password’; Once you have finalised the permissions, always be sure to reload all the privileges. FLUSH PRIVILEGES;

Continue reading How to assign a user to a database through MySQL command line in Linux