MySQL is a typical Client/Server system, So it is composed of two logical parts a server which provides services and a client, which requests them. The database is on the server and the user will have to use a client to connect to the server in order to use the database.
In order to use MySQL, you will need the MySQL Server and MySQL Client. When you install MySQL Server,you will get also the command-line client (MySQL Monitor).
You can download the MySQL Server from https://dev.mysql.com/downloads/.
If you are using Windows, you can choose the MySQL Installer for Windows. With that tool, you can install both MySQL Server and MySQL Workbench.
Instead of installing the full MySQL Server, you can choose to use UniServer, especially if you're looking for a lighter and more portable solution. UniServer is a compact Windows-based server package that includes MySQL (or MariaDB), Apache, and PHP, all pre-configured for quick and easy use. It’s ideal for local development and testing environments where you don’t need the full-scale features of a production-grade MySQL Server. UniServer doesn’t require installation in the traditional sense — it can run directly from a folder or even a USB drive, making it convenient and non-intrusive. This makes it a great option for developers who want a simple setup without modifying system-wide configurations or installing background services.
The UniServer software includes a MySQL server and a MySQL client. UniServer is user-friendly software, and its installation is simple:
When you start UniServer, you will see the Control Panel:
This panel allows you to start and stop the MySQL Server. You can also open the Server Console (Windows Command Prompt) or the MySQL Console (MySQL monitor).
When you click the button MySQL Console, you will execute command
mysql -h localhost -u root -pRootPassWhere RootPass is your MySQL root password.
For macOS users, especially those with Apple Silicon (M1/M2/M3) processors, the easiest way to install MySQL Server is using Homebrew, a popular package manager for macOS. This method is preferred by many Mac developers because it simplifies installation, updates, and service management through the command line.
Installation steps using Homebrew:
brew install mysqlbrew services start mysqlmysql_secure_installationHomebrew makes it easy to manage MySQL:
brew services start mysqlbrew services stop mysqlbrew services restart mysqlbrew upgrade mysqlAlternatively, you can download the official MySQL Installer for macOS (ARM64 DMG Archive for Apple Silicon) from https://dev.mysql.com/downloads/mysql/. Choose the version that matches your macOS and processor architecture.
Video about installing UniServer and MySQL Workbench:
https://youtu.be/_yV1Fj7Vms4
In order to connect to MySQLServer, you will need some client. The client can be the text based client (mysql monitor) or some graphical client example: MySQLWorkbench, HeidiSQL, SQLYog, DBeaver.
In order to connect to MySQL, you have to give these parameters:
In order to connect from Client to Server the parameters are:
mysql -u mysqlUser -h mysqlserver.company.com -D databasename -p
The default TCP-Port for MySQL is 3306. If your server is using some other port then you have to add the port number to your connection like this
mysql -u username -h hostname -P 8889 -D database -p
You can check which port your server is using with the command
show variables LIKE 'port';
After a user logs in using mysql-client, the MySQL server validates the credentials (username, password, IP, etc.).
If successful, the server establishes a session with a unique session ID.
Each SQL query (SELECT, INSERT, UPDATE, DELETE, etc.) sent by the client is linked to the established session.
The server identifies the session based on its connection ID and executes the query accordingly.
MySQL checks user privileges only once at the start of the session. The server does not revalidate privileges for every query. Instead, it uses the privileges fetched during login.
The server processes the query and returns results to the client. If the user’s privileges are modified while the session is active,
the changes take effect only after the user logs in again, unless explicitly refreshed with commands like FLUSH PRIVILEGES.
While you don't need to be a database administrator to develop applications with MySQL, understanding basic server configuration is valuable. The MySQL configuration file contains settings that affect how the server behaves, and some common development issues can be resolved by adjusting these settings.
MySQL server settings are stored in a configuration file:
my.ini (usually in MySQL installation directory, e.g., C:\Program Files\MySQL\MySQL Server 8.0\my.ini)my.cnf (locations: /etc/my.cnf, /etc/mysql/my.cnf, or ~/.my.cnf)UniServerZ\core\mysql\my.iniYou can find which configuration file MySQL is using with this command:
mysql --help | grep "Default options"
Here are the most common settings developers need to modify:
| Setting | Purpose | Example Value |
|---|---|---|
character_set_server |
Default character encoding for the server. Use utf8mb4 to support all Unicode characters including emojis. | character_set_server = utf8mb4 |
collation_server |
Default collation (sorting rules) for the server. | collation_server = utf8mb4_unicode_ci |
max_allowed_packet |
Maximum size of a single packet/query. Increase if you work with large files (images, BLOBs) or long SQL queries. | max_allowed_packet = 64M |
default_authentication_plugin |
Default authentication method for new users (see Authentication plugin section). | default_authentication_plugin = mysql_native_password |
bind-address |
IP address MySQL listens on. Default 127.0.0.1 (IPv4 localhost only) or ::1 (IPv6 localhost). Use 0.0.0.0 to allow connections from any IPv4 address, or :: for any IPv6 address. Warning: Only use 0.0.0.0 or :: in development environments, not in production without proper firewall rules. |
bind-address = 0.0.0.0 |
port |
TCP port the MySQL server listens on. | port = 3306 |
max_connections |
Maximum number of simultaneous client connections. | max_connections = 151 |
Steps:
my.ini or my.cnf) in a text editor with administrator/root privileges[mysqld] section (server settings)[mysqld]:
[mysqld] character_set_server = utf8mb4 collation_server = utf8mb4_unicode_ci max_allowed_packet = 64M
net stop mysql then net start mysqlbrew services restart mysqlsudo systemctl restart mysqlYou can check current server settings without editing the configuration file:
-- Show all variables SHOW VARIABLES; -- Show specific variable SHOW VARIABLES LIKE 'character_set%'; SHOW VARIABLES LIKE 'max_allowed_packet'; -- Show current character sets in use SHOW VARIABLES LIKE '%character%';
# or ; at the start of a line for comments[mysqld] sectionThis is a very short manual to MySQL, you can found more details example from https://dev.mysql.com/doc/refman/8.0/en/mysql-commands.html
| Action | Command |
|---|---|
| Create a new database | mysql> create database databasename; |
| List all databases | mysql> show databases; |
| Change the default database | mysql> use databasename; |
| List the tables in the database | mysql> show tables; |
| Check the structure of the table | mysql> describe tablename; |
| Delete a database | mysql> drop database databasename; |
| Delete a table | mysql> drop table tablename; |
| Change password |
mysql> SET PASSWORD=PASSWORD('new_password'); in MYSQL 8 mysql> SET PASSWORD='new_password'; |
| Change password to another user | mysql> ALTER USER username@host IDENTIFIED BY 'newpassword'; |
| Check the engines of your tables | mysql> show table status; |
| Check the engines in server | mysql> show engines; |
The following animation demonstrates how to use basic MySQL commands in the MySQL console:
Key takeaways from the demonstration:
There might be differences between different MySQL-server versions.
| Type | Size | Description | Common Use Cases |
| CHAR[Length] | Length bytes | A fixed-length field from 0 to 255 characters long. | Fixed-length codes (country codes: 'FI', 'SE', postal codes, phone prefixes) |
| VARCHAR(Length) | String length + 1 bytes | A variable-length field from 0 to 255 characters long. | Names, email addresses, usernames, short descriptions |
| TINYTEXT | String length + 1 bytes | A string with a maximum length of 255 characters. | Short text snippets (rarely used, VARCHAR is usually preferred) |
| TEXT | String length + 2 bytes | A string with a maximum length of 65,535 characters. | Product descriptions, blog post content, comments |
| MEDIUMTEXT | String length + 3 bytes | A string with a maximum length of 16,777,215 characters. | Long articles, documentation, large text content |
| LONGTEXT | String length + 4 bytes | A string with a maximum length of 4,294,967,295 characters. | Very large text data, entire books, JSON documents |
| TINYINT[Length] | 1 byte | Range of -128 to 127 or 0 to 255 unsigned. | Boolean flags (0/1), small counters, age, status codes (1-10) |
| SMALLINT[Length] | 2 bytes | Range of -32,768 to 32,767 or 0 to 65535 unsigned. | Year values, quantity in stock, port numbers |
| MEDIUMINT[Length] | 3 bytes | Range of -8,388,608 to 8,388,607 or 0 to 16,777,215 unsigned. | Population counts, medium-size counters (rarely used) |
| INT[Length] | 4 bytes | Range of -2,147,483,648 to 2,147,483,647 or 0 to 4,294,967,295 unsigned. | Primary keys (ID), foreign keys, most numeric data |
| BIGINT[Length] | 8 bytes | Range of -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 or 0 to 18,446,744,073,709,551,615 unsigned. | Large IDs (e.g., Twitter/X post IDs), timestamps in milliseconds |
| FLOAT | 4 bytes | A small number with a floating decimal point. | Scientific calculations, measurements (avoid for currency!) |
| DOUBLE[Length, Decimals] | 8 bytes | A large number with a floating decimal point. | Coordinates (latitude/longitude), scientific data (avoid for currency!) |
| DECIMAL[Length, Decimals] | Length + 1 or Length + 2 bytes | A DOUBLE stored as a string, allowing for a fixed decimal point. | Currency/money values, prices, balances (exact decimal arithmetic) |
| DATE | 3 bytes | In the format of YYYY-MM-DD. | Birth dates, deadlines, event dates (no time needed) |
| DATETIME | 8 bytes | In the format of YYYY-MM-DD HH:MM:SS. | Scheduled events, appointments (fixed time, no timezone conversion) |
| TIMESTAMP | 8 bytes |
In the format of YYYYMMDDHHMMSS. (In older MySQL-servers there is Year 2038 problem) |
Created_at, updated_at, logs (auto-converts to session timezone) |
| TIME | 3 bytes | In the format of HH:MM:SS | Opening hours, duration, time of day (no date needed) |
| ENUM | 1 or 2 bytes | Short for enumeration, which means that each column can have one of several possible values. | Status fields ('active', 'inactive', 'pending'), gender, priority levels |
| SET | 1, 2, 3, 4, or 8 bytes | Like ENUM except that each column can have more than one of several possible values. | Tags, permissions, features ('read', 'write', 'delete') |
Storage engines are MySQL components that handle the SQL operations for different table types. InnoDB is the default and most general-purpose storage engine, and Oracle recommends using it for tables except for specialized use cases.
When you create a table, you have to design which table type you are going to use. The various storage engines provided with MySQL are designed with different use cases in mind.
You can use below command to check which engines your server supports:
SHOW ENGINES\G
And below command will show what are the table types in your database:
SHOW TABLE STATUS\G
Here are some table types and their properties:
| Type | Properties |
|---|---|
| InnoDB | InnoDB is a transaction-safe (ACID compliant) storage engine for MySQL that has commit, rollback, and crash-recovery capabilities to protect user data. InnoDB row-level locking (without escalation to coarser granularity locks) and Oracle-style consistent nonlocking reads increase multi-user concurrency and performance. InnoDB stores user data in clustered indexes to reduce I/O for common queries based on primary keys. To maintain data integrity, InnoDB also supports FOREIGN KEY referential-integrity constraints |
| MyISAM | These tables have a small footprint. Table-level locking limits the performance in read/write workloads, so it is often used in read-only or read-mostly workloads in Web and data warehousing configurations. |
| Memory | Stores all data in RAM, for fast access in environments that require quick lookups of non-critical data. This engine was formerly known as the HEAP engine. Its use cases are decreasing; InnoDB with its buffer pool memory area provides a general-purpose and durable way to keep most or all data in memory, and NDBCLUSTER provides fast key-value lookups for huge distributed data sets. |
You can backup databases and tables with MySqlDump. It is very simple application, which will create the sql-code that you can use to create the databases and tables with the data.
Example Below command will backup Project database to file project.sql
mysqldump -u root Project -p > project.sqlExample Below command will backup Project database to file project.sql including also the stored procedures
mysqldump -u root Project -p --routines > project.sqlAnd the next command will backup only the table Book
mysqldump -u root Project Book -p >book.sqlAnd then you can use those codes to return your database and tables.
mysql -u root -D Project -p < mybackup.sql
When a database is exported to another MySQL server using a dump file, different character encodings can cause an error. In such cases, it is advisable to remove those annotations from the dump file. This can be done, for example, by executing the following command:
sed -E 's/DEFAULT CHARSET=[a-zA-Z0-9_]+//g; s/COLLATE=[a-zA-Z0-9_]+//g' source.sql > target.sql
You can also use MySQL Workbench to handle the dump-files. In the Server menu there is option Data Export, which will create the dump-file and option Data Import will execute the commands from dump file.
MySQL server has a special database called mysql, which is a system database. It contains metadata about the server configuration, including:
Important: You should NOT modify tables in the mysql database directly.
Instead, use SQL commands like CREATE USER, GRANT, REVOKE,
etc. Direct modifications can corrupt the system and cause authentication problems.
User management is easiest to handle with some graphical client as MySQLWorkbench, dBForgeStudio or HeidiSQL. But it is still useful to know the basic things behind this system.
MySQL stores all user information to table user inside database mysql.
You can check which users you have in your server, like this:
SELECT User FROM mysql.user;
And if you want to check also which are the allowed hosts for the users, you use can this:
SELECT User,Host FROM mysql.user;
You can create new user, which can connect from localhost like this
CREATE USER 'testuser'@'localhost' IDENTIFIED BY 'test_pass';
You can create new user, which can connect from everywhere like this
CREATE USER 'testuser'@'%' IDENTIFIED BY 'test_pass';
You can create new user, which can connect from domain mydomain.com like this
CREATE USER 'testuser'@'%.mydomain.com' IDENTIFIED BY 'test_pass';
If you want to set the authentication to mysql_native_password, you can create the user like this
CREATE USER 'netuser'@'localhost' IDENTIFIED WITH mysql_native_password BY 'netpass';
MySQL supports different authentication plugins, for example:
Why it matters: Some older applications or client libraries may not support caching_sha2_password and will fail to connect. In those cases, you need to either update the client or use mysql_native_password.
If you don't set it in the CREATE USER command, MySQL will use the default of your server. You can check what is the default in your server with the command:
SHOW VARIABLES LIKE '%default_authentication%';You can set mysql_native_password as the default by adding below line to my.cnf (Mac, Linux) or to my.ini (Windows):
default_authentication_plugin=mysql_native_password
You can use Grant commands to manage users privileges.
Below command will add SELECT privileges to all tables in database Project to user testman, if he connects from localhost.
GRANT SELECT ON Project.* TO 'testman'@'localhost';Below command will add SELECT and INSERT privileges to all tables in database Project to user testman, if he connects from localhost.
GRANT SELECT,INSERT ON Project.* TO 'testman'@'localhost';Below command will add all privileges to procedure debit_transfer in database Project to user testman, if he connects from localhost.
GRANT EXECUTE ON Project.debit_transfer TO 'testman'@'localhost';Below command will add all privileges to all tables in database Project to user testman, if he connects from localhost.
GRANT ALL ON Project.* TO 'testman'@'localhost';Below command will show which privileges user testman has, if he connects from localhost.
SHOW GRANTS FOR 'testman'@'localhost';
REVOKE SELECT ON Project.* FROM 'testman'@'localhost';Below command will remove SELECT and INSERT privileges to all tables in database Project to user testman, if he connects from localhost.
REVOKE SELECT,INSERT ON Project.* FROM 'testman'@'localhost';Below command will remove all privileges to all tables in database Project to user testman, if he connects from localhost.
REVOKE ALL ON Project.* FROM 'testman'@'localhost';
You can delete the the user with below command
DROP USER 'testman'@'localhost';
MySQL Workbench is a unified visual tool for database architects, developers, and DBAs. MySQL Workbench provides data modeling, SQL development, and comprehensive administration tools for server configuration, user administration, backup, and much more. MySQL Workbench is available on Windows, Linux and Mac OS X.
You can find a tutorial for using MySQLWorkbench from https://www.guru99.com/introduction-to-mysql-workbench.html
And the manual for MySQLWorkbench from https://dev.mysql.com/doc/workbench/en/
If you have problems with the installation check this https://dev.mysql.com/doc/workbench/en/wb-installing.html
(Example in Windows you might need to install C++ Redistributable.)
It is not safe to connect to the Server over public internet using TCP/IP. And quite often the company does not even allow to connect to the server over the internet. Then one option is to use VPN and another is to use TCP/IP over SSH. The last option is described in the image. So inside the companys network there is a SSH-server which has also a MySQL-client. Then it is possible to connect to that SSH-server and then from that server connect to the MySQL-Server.
You can also connect with MySQL Workbench using TCP/IP over SSH, if you configure the connection like in below image.
Some MySQLWorkbench versions does not have the option useSSL if available and that might be a problem when you are connecting to your local-server, which does not have SSL-support. You can change the configuration like this
>2|Require,3|Require and Verify CA,4|Require and Verify Identitywith these:
>0|No,1|If available,2|Require,3|Require and Verify CA,4|Require and Verify Identity
Managing time zones correctly in a MySQL server is critical for applications that rely on accurate timekeeping across different regions or time-sensitive operations. MySQL supports time zone conversions, but the server must be properly configured to use named time zones (like Europe/Helsinki), especially in international environments.
Setting the correct time zone ensures consistency in how time-related data is stored and retrieved. If the server's time zone differs from the application's or user's time zone, it can lead to confusing data mismatches, incorrect reporting, and even critical errors in systems like logging, scheduling, or financial transactions.
The TIMESTAMP data type in MySQL is time zone-aware. It stores values in UTC internally and automatically converts them to the session's time zone when queried. This makes it suitable for multi-user systems where users are in different time zones.
In contrast, DATETIME stores the value exactly as given, without any time zone conversion, which can be useful when the time is fixed and not relative to any time zone (e.g., a historical event).
When you query a TIMESTAMP field, the time displayed depends on the MySQL session time zone setting, not the client computer's time zone. The same stored time will appear different to users in different sessions if they have different time zone settings.
Example:
-- Session 1: In Finland (Europe/Helsinki, UTC+2) SET time_zone = 'Europe/Helsinki'; INSERT INTO logs (event_time) VALUES (NOW()); -- Inserts: 2024-12-31 15:00:00 -- MySQL stores internally: 2024-12-31 13:00:00 UTC -- Session 2: In New York (America/New_York, UTC-5) SET time_zone = 'America/New_York'; SELECT event_time FROM logs; -- Displays: 2024-12-31 08:00:00 -- Session 3: In Finland SET time_zone = 'Europe/Helsinki'; SELECT event_time FROM logs; -- Displays: 2024-12-31 15:00:00
Important: The client computer's time zone does not automatically affect the displayed time. Your client application must explicitly set the session time zone when connecting to MySQL.
Example in Node.js (mysql2):
const mysql = require('mysql2');
const connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'password',
database: 'mydb',
timezone: 'Europe/Helsinki' // Sets session time zone
});
Example in PHP (PDO):
$db = new PDO('mysql:host=localhost;dbname=mydb', 'root', 'password');
$db->exec("SET time_zone = 'Europe/Helsinki'");
If you don't set the session time zone explicitly, MySQL uses the server's default time zone, which may not match your users' locations.
The TIMESTAMP data type is ideal when you need to track events relative to time zones or log actions with precision in a global context. Because TIMESTAMP values are stored in UTC and automatically converted to the session’s time zone, they are particularly useful in multi-user systems where users operate in different geographic regions.
It's commonly used for tracking record creation and update times (e.g., created_at, updated_at) in web applications, logging systems, or auditing changes where the exact moment an event occurred is more important than how it appears in a fixed time zone.
Note that in older versions of MySQL (up to 8.0), TIMESTAMP has a limited date range, supporting only dates between 1970 and 2038 due to its 32-bit Unix time representation. If you need to store dates outside that range—such as future appointments, contracts, or historical records—DATETIME is a safer and more flexible choice.
You can first check if the time zone is already active in your server, by executing command
SELECT name FROM mysql.time_zone_name;If the result is not empty, it is active. If it is not active, you can fix it like this:
Then you can use the Time Zone in SELECT-queries like this:
SELECT CONVERT_TZ(NOW(), 'UTC', 'Europe/Helsinki') AS 'Finish_time'; SELECT CONVERT_TZ(NOW(), 'UTC', 'Europe/Stockholm') AS 'Swedish_time';
In Linux, you can activate Time Zones, by giving below command
mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root -p mysql