Reading:  

Beginners guide to MySQL and MariaDB


Establishing Connection

With MySQL you would either want to connect to using MySQL client to query the database you have created or connect programmatically using a programming language such as PHP or C# etc to manipulated data. 

MySQL Client

When you want to use MySQL client, you would normally issue the following command

> mysql -uroot -p [ENTER]

You will then be asked to enter root user password to get access to MySQL prompt. -u is followed by the name of the database user you wish to use. This must be an already existing account. root account is pretty much a super user can have access to everything. Here is how it looks when you are successfully logged in. 

MySQL login

There are heaps of options to go with mysql command, you can find all those details on this page

Connection using a scripting language PHP

We recommend using PDO to connect to MySQL databases from your PHP script. PDO can connect to heaps more databases than a traditional MySQLi  module. MySQLi's sole purpose is to connect to MySQL DB. However if you wish to use MySQLi, you can. There is no difference in terms of security or reliability when you connect to MySQL database.

MySQLi Installation

The MySQLi extension is automatically installed when you end up installing a PHP MySQL package.

For installation details, go to: http://php.net/manual/en/mysqli.installation.php


PDO Installation

If you wish to go with PDO, here are the installation details, go to: http://php.net/manual/en/pdo.installation.php

Using PDO for connecting to MySQL, here's a sample code 

<?php
$server = "localhost";
$user = "username";
$pwd = "password";

try {
    $connection = new PDO("mysql:host=$server;dbname=testDB", $user, $pwd);
    // PDO can throw exceptions rather than Fatal errors, so let's change the error mode to exception
    $connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "connection successful"; 
    }
catch(PDOException $e)
    {
    echo "Connection failed: " . $e->getMessage();
    }
?>

 

Parameter

Description

server

The default value is localhost: 3306. It is database hostname server.

user

User Name to access database.

password

Password to access database.

dbname

PDO needs a valid database name to connect to. If this is not supplied, then connection basically fails.

$connection

instance of PDO, that gives access to the database you are connecting with. 

 

Closing the connection

Connection are automatically closed when your script execution ends, however you may want to close connection before that, which in many ways is a good practise as well, here is how its done

$connection->close();

or

$connection = null;

 

Connecting to MySQL using C#

You can connect to MySQL from pretty much any language you use. This is possible from of the wide variety of connectors already available. 

First you would need to download MySQL Connector/Net from MySQL website (https://dev.mysql.com/downloads/connector/net/6.9.html), Once downloaded, you will then install it. Installation instructions can be found here

Once all is set then opening a connection to your MySQL server is rather simple and is done using MySQLConnection class as shown below

MySql.Data.MySqlClient.MySqlConnection connection;
string connectionString;

connectionString = "server=127.0.0.1;uid=guest;" +
    "pwd=guiest123;database=testDB;";

try
{
    connection = new MySql.Data.MySqlClient.MySqlConnection();
    connection.ConnectionString =connectionString;
    connection.Open();
}
catch (MySql.Data.MySqlClient.MySqlException ex)
{
    MessageBox.Show(ex.Message);
}

To close connection, you will have to call Close method of your connection as shown below

connection.Close();

Here is a quick video tutorial by ProgrammingKnowledge guys on Youtube

In the next part of this tutorial, we will explore how to create a database.

 

Description

In this tutorial, we will cover few topics that will give you a heads on start to build your knowledge on. Topics that we will cover briefly but still providing enough information are listed below

  • Overview
  • Installing on Linux and Windows
  • Some useful admin queries for starters
  • Connection
  • Create Database
  • Drop Database
  • Select Database
  • Data Type
  • Create Table
  • Drop Table
  • Inserting and Selecting data
  • Where Clause
  • Updating and deleting data
  • Like Clause
  • Sorting Result
  • Using Joins
  • Brief introduction to Regex, Transactions and Indexes
  • Alter Command
  • Temporary Tables
  • Database Info
  • Using Sequence
  • Database Export and Import
  • Resetting MySQL/MariaDB Administrator password


Audience

Absolute beginners looking to get a sneak peak into what MySQL. Please remember that this is not a full on guide but a quick introduction to the subject.

Learning Objectives

Get to know MySQL and MariaDB

Author: Subject Coach
Added on: 23rd Jun 2015

You must be logged in as Student to ask a Question.

None just yet!