> For the complete documentation index, see [llms.txt](https://book.bsdcn.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://book.bsdcn.org/ask/flat/chapter-35-database-management/di-35.4-jie-mysql.md).

# 35.4 MySQL

MySQL is an open-source relational database management system (Relational Database Management System, RDBMS) primarily developed by Oracle Corporation.

MySQL 9.7 is the latest Long Term Support (LTS) version, and MySQL 8.4 is the previous Long Term Support (LTS) version.

## Installing MySQL 8.4 LTS

Install using pkg:

```sh
# pkg install mysql84-server
```

Or install using Ports:

```sh
# cd /usr/ports/databases/mysql84-server/
# make install clean
```

View the post-installation instructions:

```sh
# pkg info -D mysql84-server
```

## Installing MySQL 9.7 LTS

Install using pkg:

```sh
# pkg install mysql97-server
```

Or install using Ports:

```sh
# cd /usr/ports/databases/mysql97-server/
# make install clean
```

View the post-installation instructions:

```sh
# pkg info -D mysql97-server
```

## Starting the Service

After installation is complete, you need to start the MySQL service before you can use it.

Set the MySQL service to start on boot:

```sh
# service mysql-server enable
```

Start the MySQL service immediately:

```sh
# service mysql-server start
```

## Logging In

After the service starts, you can log in to MySQL using the command-line client (which will be automatically installed as a dependency). According to the installation instructions, the default password for MySQL 8.4 and 9.7 is empty; just press Enter when prompted for a password.

Log in to the MySQL server as the root user, and enter the password when prompted:

```sql
# mysql -uroot -p
Enter password: # Press Enter directly here
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.4.9 Source distribution

Copyright (c) 2000, 2026, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>
```

## Changing the Password

For database security, it is recommended to change the root user password immediately after the first login. The following command sets the root user password of the database to `SecurePass123!`; the change takes effect immediately, and then exits MySQL.

```sql
mysql> alter user 'root'@'localhost' identified by 'SecurePass123!'; -- Change the root user password to SecurePass123!; ALTER USER directly modifies the grant tables and takes effect immediately
Query OK, 0 rows affected (0.01 sec)

mysql> quit; -- Exit the MySQL command-line client
Bye
```

> **Tip**
>
> The `SecurePass123!` in the above example is a placeholder and needs to be replaced with an actual value.

Log in again:

```sql
# mysql -uroot -p  # Log in to MySQL as the root user, and enter the password when prompted
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.4.9 Source distribution

Copyright (c) 2000, 2026, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases; -- Display all databases in the current MySQL instance
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

mysql> quit; -- Exit the MySQL command-line client
Bye
```

## MySQL Configuration File

The MySQL configuration file structure is as follows.

```sh
/
└── usr/
    └── local/
       └── etc/
           └── mysql/
                ├── my.cnf        # MySQL configuration file
                └── my.cnf.sample # MySQL configuration file sample

```

If you need to further configure MySQL, you can modify its configuration file. The configuration file can adjust various database parameters, such as memory usage, number of connections, etc.

The default MySQL configuration file template is located at **/usr/local/etc/mysql/my.cnf.sample**, and the actual default configuration file is located at **/usr/local/etc/mysql/my.cnf** (default content is empty).

Copy the MySQL sample configuration file to the active configuration file:

```sh
# cp /usr/local/etc/mysql/my.cnf.sample /usr/local/etc/mysql/my.cnf
```

Then modify the **/usr/local/etc/mysql/my.cnf** file as needed.

If you need to use a custom configuration file path, you can write the following in the **/etc/rc.conf** file:

```ini
mysql_optfile="/abc/xyz.cnf"
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://book.bsdcn.org/ask/flat/chapter-35-database-management/di-35.4-jie-mysql.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
