Mysql

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Chapter 2 - Using MySQL

Command-line
MySQL comes with a command-line interface¹⁹ that let you run MySQL commands and SQL
queries. While there are graphical tools like phpMyAdmin²⁰ and MySQL Workbench²¹, command-
line interface will come in handy when you manage more databases and when you get more familiar
with MySQL.

MySQL Path
You should be able to run mysql and other command-line utilities like mysqladmin and mysqldump
(discussed later) from any folder. If these utilities are not accessible from any directory, add the path
to the utilities to a global path variable in your operating system.
If you have installed XAMPP in Windows, the path would be the bin folder under mysql folder. For
example, if you have installed XAMPP under C drive, path would be C:\xampp\mysql\bin. You can
verify the path by making sure the utilities are available in the chosen path.
Add this path to the path environment variable in Windows, and then you will be able to access the
utilities from any folder.

Logging into MySQL


You can log in to MySQL as root user (generally the user with all the privileges) by typing the
command below.

mysql -u root -p

The command above will prompt you to enter the password for user root. If your MySQL installation
is new and you haven’t changed the root password, most of the time the root password is blank
(just press the Enter key).
After that, you would see a prompt like below that lets you type commands. Prior to the prompt,
you would see few instructions and the MySQL version number.
¹⁹http://dev.mysql.com/doc/refman/5.5/en/mysql.html
²⁰http://www.phpmyadmin.net/
²¹http://www.mysql.com/products/workbench/
Chapter 2 - Using MySQL Command-line 6

mysql>

Executing Commands
All SQL commands you type at the MySQL prompt should have a semicolon (;) at their ends. The
commands will not run till you enter a semicolon (It’s possible to use \G instead of semicolon as
explained below).
In addition to the SQL commands, MySQL has its own set of commands. To see these commands,
type help at the MySQL prompt as below. These commands aren’t required to have a semicolon at
the end. After typing a command, hit Enter key to execute the command.

mysql> help

Command-line Pretty Output


If you find the output of a certain SQL command difficult to read, try \G in place of the semicolon
as shown in the following example. This will display the output in a vertical format and remove
surrounding dashed lines.

mysql> SHOW TABLE STATUS FROM company_db \G

SHOW TABLE STATUS command is covered in Database Commands chapter.

Multiple Line Commands


To achieve clarity, you can span a command over multiple lines. Just hit the Enter key after each
line, and MySQL will prompt an arrow indicating a new line. The following is a multiple line SQL
command to create a data table.

mysql> create table `employee` (


-> `id` int(10),
-> `first_name` varchar(40),
-> `last_name` varchar(40),
-> `age` tinyint(3),
-> `joined_date` date,
-> `records` text,
-> primary key (`id`)
-> );
Chapter 2 - Using MySQL Command-line 7

How to Clear Command-line


If you are on a Mac OS or a Linux command-line, you can use Ctrl+L for clearing the screen and
Ctrl+U for clearing the current line.

Logging out of MySQL


Use the exit command to log out of MySQL.

mysql> exit
Chapter 3 - MySQL GUI Tools
While you can run all MySQL statements in command-line, having a tool with Graphical User
Interface (GUI) can speed up your development.
A GUI tool can be especially helpful when you want to view data and table information. Following
are a few popular and free MySQL GUI tools.
phpMyAdmin²²

• A web application written in PHP.


• Rich set of features.
• Works in any OS with required PHP and MySQL setup.

SQL Buddy²³

• A web application written in PHP.


• Clean interface.
• Painless installation (Just extract to the web folder).
• Ability to log in via MySQL user accounts without creating user accounts for the tool.
• Works in any OS with required PHP and MySQL setup.

MySQL Workbench²⁴

• A Desktop application.
• Rich set of features (Database Design, Server Administration).
• Versions available for Microsoft Windows, Mac OS, Linux.

²²http://www.phpmyadmin.net/
²³http://sqlbuddy.com/
²⁴http://www.mysql.com/products/workbench/
Chapter 4 - Managing MySQL Users
In MySQL, you can create user accounts²⁵ with different privileges. Privileges can vary from
accessing several databases to accessing only one column in a table.

Root User
By default, MySQL has a super user called root that has all the privileges. You need to be logged in
as root to execute many MySQL administrative tasks, including managing users.

Changing Root Password


If you didn’t specifically set the root password when installing MySQL, most of the times it would
be empty. If the root password is empty, make sure to reset it with a proper password for better
security.
In command-line, you can use the following command to change root password. Type your preferred
password in place of newpassword. After hitting the Enter key, it will ask you to enter the current
password. If the current password is empty, just hit the Enter key.

mysqladmin -u root -p password 'newpassword'

Logging as a User
Use the following command to log in as root user. For logging in as a different user, type that
username in place of root. After hitting the Enter key, it will ask you to enter the password. After
entering the correct password, you would see the MySQL prompt (mysql>) where you can enter
MySQL commands.

mysql -u root -p
²⁵http://dev.mysql.com/doc/refman/5.5/en/adding-users.html
Chapter 4 - Managing MySQL Users 10

Viewing Existing Users


MySQL user details are stored in a table called user of a default database called mysql. In this user
table, usernames are stored in a column called user, and corresponding host names are stored in a
column called host.
Based on these facts, you can use the following SQL command to view the username and the host
of existing users. You need to log in as root first.

SELECT user, host FROM mysql.user;

Creating a New User


For creating a user called robin with the password robin123 at localhost (is the default host most
of the time), log in as root and use the following command.

CREATE USER 'robin'@'localhost' IDENTIFIED BY 'robin123';

Granting Privileges to a User


MySQL has a series of privileges²⁶. For a general PHP application, you only need a user with
SELECT, INSERT, UPDATE, and DELETE privileges for the database you chose. You can grant
these privileges to a user called ronbin for a database called my_database using the following
command.

GRANT SELECT, INSERT, UPDATE, DELETE ON my_database.* TO 'robin'@'localhost';

To grant all the privileges, use ALL as below.

GRANT ALL ON my_database.* TO 'robin'@'localhost';

Changing a User Password


To change the password of a user called robin to robin456, log in as the root user and use the
following command.

²⁶http://dev.mysql.com/doc/refman/5.5/en/privileges-provided.html
Chapter 4 - Managing MySQL Users 11

SET PASSWORD FOR 'robin'@'localhost' = PASSWORD('robin456');

Deleting a User
Log in as root and use the following command to delete user robin.

DROP USER robin@localhost;

Be careful when you delete a user, since applications that used the credentials of a deleted user may
malfunction.

Summary
PHP needs the host name, username, and password of a privileged MySQL user to connect to a
MySQL database. When it comes to production-level PHP applications, instead of using root user,
for improved security it’s a good practice to use a dedicated user with only the required privileges.
You will only need to deal with MySQL users when you manage your own web server or when you
develop web applications locally. If you are on a shared web-hosting environment, most of the time,
you will be provided privileged MySQL user accounts or a GUI tool to manage MySQL users.

You might also like