MySQL Guide: Setup, Queries, Transactions, and Backups
MySQL is a client/server relational database used for websites, business applications, and other systems that need several clients to share structured data.
For query syntax, start with the SQL guide. The database engine guides explain how MySQL compares with the other options.
Choose MySQL when your framework, host, and team already support it. The setup decision that matters most is access. Give each application its own restricted account and keep administrative credentials away from ordinary requests.
When Should You Use MySQL?
MySQL fits a conventional server application with relational data, concurrent clients, established MySQL tooling, and hosting that supports the selected release. Content systems, shops, internal tools, and reporting applications commonly match that shape.
Choose it when the surrounding system supports these conditions:
- The framework and connector support the intended MySQL release.
- The data belongs in tables with keys, constraints, joins, and transactions.
- The deployment provides tested backups, monitoring, and an upgrade path.
- The team understands MySQL permissions, SQL modes, and InnoDB behavior.
- Existing applications or operational tools already depend on MySQL compatibility.
Use PostgreSQL when its types, constraints, extensions, or indexing options better match the data. Use SQLite when an embedded, single-file database removes an unnecessary server.
How Do You Install and Connect to MySQL?
Install MySQL Server for your operating system, start its service, and connect with the mysql client. Use the official installer or package repository for your platform. Package names, repository tracks, authentication defaults, and configuration paths vary.
Choose a supported LTS release for a stable application baseline, or Innovation when you need its newer features and can keep up with more frequent upgrades. The commands below use standard MySQL account and InnoDB features.
mysql --version
mysql -u root -p Ask the connected server for the details that affect the application:
SELECT VERSION();
SHOW VARIABLES LIKE 'version_comment';
SHOW VARIABLES LIKE 'default_storage_engine';
SHOW VARIABLES LIKE 'sql_mode';
SHOW VARIABLES LIKE 'character_set_server'; This check identifies the server release, distribution, storage-engine default, SQL mode, and character set. Record the output with deployment notes because a client binary version does not prove which remote server accepted the connection.
How Do You Create a Restricted MySQL User?
Create a MySQL user with CREATE USER, then grant only the permissions the application needs on its database. Keep schema changes in a separate administrator or migration account.
CREATE DATABASE codewalkers_app
CHARACTER SET utf8mb4;
CREATE USER 'app_runtime'@'127.0.0.1'
IDENTIFIED BY 'replace-with-a-generated-secret';
GRANT SELECT, INSERT, UPDATE, DELETE
ON codewalkers_app.*
TO 'app_runtime'@'127.0.0.1';
SHOW GRANTS FOR 'app_runtime'@'127.0.0.1'; MySQL accounts include both a user name and a host. The account above is different from 'app_runtime'@'%', which accepts a much broader connection origin. Match the host rule to the deployment and keep schema-changing privileges in a separate migration account.
Test the runtime identity through the same network path used by the application:
mysql -u app_runtime -p -h 127.0.0.1 codewalkers_app Store the generated password in the deployment platform's secret store. Rotate it if it appears in a committed file, log, screenshot, or terminal transcript.
How Do MySQL InnoDB Transactions Work?
MySQL InnoDB transactions group related writes so you can commit them together or roll them back. InnoDB also provides row-level locking, crash recovery, and foreign-key checks.
Reconnect as an administrator or migration user, select codewalkers_app, and create two related tables. The restricted runtime user above cannot create tables:
USE codewalkers_app;
CREATE TABLE orders (
id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
customer_email VARCHAR(255) NOT NULL,
status VARCHAR(20) NOT NULL DEFAULT 'pending'
) ENGINE=InnoDB;
CREATE TABLE order_items (
id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
order_id BIGINT UNSIGNED NOT NULL,
sku VARCHAR(80) NOT NULL,
quantity INT UNSIGNED NOT NULL,
CONSTRAINT order_items_order_fk
FOREIGN KEY (order_id) REFERENCES orders (id)
) ENGINE=InnoDB; The next transaction inserts the order and its first item as one unit:
START TRANSACTION;
INSERT INTO orders (customer_email)
VALUES ('reader@example.com');
SET @order_id = LAST_INSERT_ID();
INSERT INTO order_items (order_id, sku, quantity)
VALUES (@order_id, 'SQL-GUIDE', 1);
COMMIT; A failed statement does not always undo the whole transaction. For example, a foreign-key error normally rolls back only that statement, while a deadlock rolls back the transaction. Handle the error in application code and issue ROLLBACK rather than committing a partial order.
Application code must still begin, commit, and roll back the correct unit of work. Read SQL transactions and ACID before combining database changes with files, email, queues, or payment calls that cannot join the same transaction.
How Do You Configure MySQL for an Application?
Configure MySQL through its server and client option files, then restart and check the settings on the running server. Find the files used by the installed distribution instead of assuming a path from another operating system.
Keep the first configuration small and visible:
[mysqld]
default_storage_engine=InnoDB
character_set_server=utf8mb4
collation_server=utf8mb4_0900_ai_ci
sql_mode=ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
[client]
default-character-set=utf8mb4 Confirm each value after restarting the server. Strict SQL mode turns several silent data conversions into errors, which exposes invalid writes earlier. Character set and collation must also match application requirements before existing data makes a later conversion expensive.
Connection limits, InnoDB memory, durability settings, binary logging, and replica settings depend on measured workload and recovery requirements. Copying a large configuration from another server can consume memory or change durability without solving the local bottleneck.
How Do You Back Up and Upgrade MySQL?
Back up MySQL with a logical dump or a supported physical backup tool, test the restore, and rehearse upgrades on a copy. Logical dumps are portable and readable but can be slower and larger. Physical backups can restore large databases faster but depend more closely on server files, versions, and tooling.
A small InnoDB database can begin with a logical dump and a rehearsed restore:
mysqldump \
--single-transaction \
--routines \
--events \
--databases codewalkers_app > codewalkers_app.sql
mysql < codewalkers_app.sql Run the restore against a separate test server with the appropriate administrator credentials, not against your live application database.
--single-transaction creates a consistent snapshot for transactional InnoDB tables without locking them for the full dump. It does not give the same guarantee to non-transactional tables. Avoid schema changes while the dump runs. Verify restored row counts, constraints, routines, events, and representative application queries.
Before an upgrade, identify whether the source and destination are LTS or Innovation releases. Follow MySQL's supported upgrade path, read every intervening release note, test on a restored copy, compare important query plans, and rehearse rollback. A replica can help availability during some upgrades, but it remains another copy of current changes rather than a historical backup.
How Did MySQL Begin?
The MySQL project began its public history during 1995. The original developers first tried to connect their own ISAM routines through mSQL, then built a new SQL interface when mSQL did not meet their speed and flexibility requirements. The similar API made existing mSQL code easier to port.
The project was named after one of co-founder Michael "Monty" Widenius's daughters. MySQL later became closely associated with the PHP-based web stack because widely available hosting put a server database within reach of small website projects.
That history explains the many PHP examples and the connection to MariaDB. It does not make old setup instructions current. MySQL now publishes separate Long-Term Support and Innovation tracks, so every installation needs an explicit release choice.
Common Pitfalls
Copying an Old Version Number
Installation fails or the managed service does not offer the tutorial's version. A stale article was treated as release authority. Check MySQL's current release documentation, choose the intended LTS or Innovation track, and confirm connector plus framework support.
Running the Application as Root
A compromised request can alter schemas or read unrelated databases. Administrative credentials were reused by the runtime application. Create a host-scoped runtime user with only required data privileges and reserve schema changes for a separate migration account.
Forgetting the Transaction Boundary
An order exists without its items after the second statement fails. Related writes ran under separate autocommit transactions. Start one transaction around the complete database write set, commit after every statement succeeds, and roll back on failure.
Treating Replication as Backup
An accidental delete appears on every replica. Replication was expected to preserve an earlier state. Keep independent backups, enable point-in-time recovery when required, and test a restore without relying on the live replication topology.
For related decisions, read PHP PDO, SQL indexes and query optimization, MySQL vs PostgreSQL vs SQLite.
Frequently Asked Questions
Should a new application use MySQL LTS or Innovation?
Choose MySQL LTS when the application needs a stable feature set and a longer support window. Choose Innovation only when the team needs its newer features and can test more frequent behavior changes. Confirm the exact server, connector, framework, and managed-service support before deployment.
Is InnoDB the default MySQL storage engine?
InnoDB is the default storage engine in current MySQL releases unless the server configuration changes that default. It supports transactions, row-level locking, crash recovery, and foreign keys. Check the running server with SHOW VARIABLES rather than assuming every installation kept the default.
Does PHP PDO make MySQL queries safe?
PHP PDO helps protect MySQL queries from SQL injection when the application uses prepared statements and parameter binding correctly. Bind untrusted values instead of concatenating them into SQL, validate identifiers through fixed allowlists, use a restricted database account, and escape output separately when database text enters HTML.
How do MySQL binary logs support point-in-time recovery?
MySQL binary logs record database changes that can be replayed after restoring a full backup. A point-in-time recovery can restore that backup, replay the required log events, and stop before a damaging statement or at a chosen time. Retain and protect the logs for the recovery window, record their coordinates, and rehearse the complete restore sequence.
Self-Check
-
Which account should handle ordinary application requests?
A. The root account. B. A restricted runtime account. C. The schema owner.
Answer: B. The runtime account needs only the data privileges used by the application, while administrative and migration privileges stay separate.
-
What happens after this sequence?
START TRANSACTION; INSERT INTO orders (customer_email) VALUES ('reader@example.com'); ROLLBACK; SELECT COUNT(*) FROM orders WHERE customer_email = 'reader@example.com';Answer: The count is zero, assuming no matching row existed before the transaction.
ROLLBACKremoves the uncommitted insert. -
Which command proves the running server version?
A.
mysql --version. B.SELECT VERSION(). C.SHOW GRANTS.Answer: B. The client command reports the local client version, while
SELECT VERSION()asks the connected server. -
Why does replication not replace backups?
Answer: Replication normally copies destructive changes as faithfully as valid ones. Backups and point-in-time recovery preserve states that can be restored after an accidental change reaches the replicas.
-
What should happen if the second statement in a related write fails?
A. Commit the first statement. B. Ignore the error. C. Roll back the transaction.
Answer: C. Rolling back prevents a partial database state when the statements belong to one unit of work.
Sources
-
[1]
Getting Started with MySQL(dev.mysql.com)
-
[2]
InnoDB Error Handling(dev.mysql.com)
-
[3]
History of MySQL(dev.mysql.com)
-
[4]
MySQL Releases: Innovation and LTS(dev.mysql.com)
-
[5]
Changes in MySQL 26.7.0(dev.mysql.com)
-
[6]
MySQL 9.7 FAQ: Server SQL Mode(dev.mysql.com)
-
[7]
Introduction to InnoDB(dev.mysql.com)
-
[8]
Using Option Files(dev.mysql.com)
-
[9]
MySQL Account Management(dev.mysql.com)
-
[10]
Backup and Recovery(dev.mysql.com)
Read Next
Understand MariaDB's relationship to MySQL, current release model, storage engines, install/config differences, and when a PHP or SQL project should consider it.
Compare MySQL, PostgreSQL, and SQLite by deployment, write concurrency, data rules, JSON, search, and recovery needs.
Use PDO prepared statements to connect PHP to MySQL safely and keep SQL input handling clear.
Learn what SQL indexes do, what they cost, and how to use EXPLAIN without guessing.