Mysql
Mysql
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.
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
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²²
SQL Buddy²³
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.
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
²⁶http://dev.mysql.com/doc/refman/5.5/en/privileges-provided.html
Chapter 4 - Managing MySQL Users 11
Deleting a User
Log in as root and use the following command to delete user robin.
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.