LAB 01
INSTALL AND MANAGE MYSQL DATABASE
1. Install
All downloads for MySQL Community are located at MySQL Downloads:
https://dev.mysql.com/downloads/
Pick a suitable version of MySQL Community Server y ou will be running on
your platform and install.
Or install from xampp, lampp …
https://www.apachefriends.org/index.html
2. MySQL Server structure
- Configuration file: All system settings are stored in the configuration file.
The file name is my.ini on Windows or my.cnf on Linux, Unix. The main
content of the configuration file is as follows (lines beginning with the #
character are comment lines):
# The TCP / IP Port the MySQL Server will listen on
port = 3306
# Path to installation directory. All paths are
# usually resolved relative to this.
basedir = "C:/Program Files/MySQL/MySQL Server 8.0/"
# Path to the database root
datadir = "C:/Program Files/MySQL/MySQL Server 8.0/Data/"
• Port: specify the working port number of MySQL Server
• Basedir: the MySQL server installation directory.
• Datadir: the path to the data directory.
Note: You should change the basedir and datadir to improve the system
security.
MySQL directory structure
Directory Contents of Directory
bin Mysql server, client and utility programs
Where MySQL stores (reads and writes) data, and server log
data
files.
docs Release documentation
include Include (header) files
lib Libraries
Miscellaneous support files, including error messages, character
share set files, sample configuration files, SQL for database
installation
Figure 1: MySQL Installation Default Layout on Windows
3. Connect to MySQL server
Firstly, make sure that MySQL Server is running after the installation process.
MySQL Server can be started directly through the command.
shell> basedir mysqld.exe --console
basedir: is the directory containing the mysqld.exe program.
-u <username> --user=username User login
-p --password Ask for the password
immediately
-p <password> --password=xxx User password
-h hostname --host=hostname Specify the name or IP
address of the MySQL
server (the default value
is “localhost”)
-P port --port=port MySQL port
Two ways to log into the MySQL server
M1: basedir\mysql.exe –u user_name –p your_password
M2: basedir\mysql.exe --user=user_name -- password=your_password
4. Some Basic Operations with MySQL
After successfully connecting to the MySQL Server, you can query the
database.
For example:
mysql> SHOW DATABASES;
Disconnect from the MySQL server using:
mysql> EXIT;
After you have logged into the MySQL server using the mysql client, the
following step describes how to create and delete a database.
CREATE DATABASE [IF NOT EXISTS] database_name;
Note: SQL statements ending with asterisk; or \g, \G and press the Enter key.
The CREATE DATABASE statement will create a database called
“database_name”. IF NOT EXISTS is an option to avoid errors if a database
with the same name exists. If it already exists, the command will not be
executed.
USE database_name;
To select a database that you intend to work.
DROP DATABASE [IF EXISTS] database_name;
Database deletion means deleting the physical database, all data and related
objects. Like the CREATE DATABASE statement, the IF EXIST option will
not execute the command DELETE if the database does not exist
Practical Exercises
1. How to change the default port of MySQL server to 3307 and
connect to MySQL server at this port?
2. How to move the data directory of MySQL to another directory
and reconfigure the configuration file to redirect to new data
directory.
3. Write the correct SQL statement to list all the existing databases
in MySQL.
4. Write the correct SQL statement to create a new database called
“my_database”.
5. Write the correct SQL statement to delete the database
“my_database”.