How to Install MySQL 8 on FreeBSD

MySQL is one of the most popular open source database management systems. In this tutorial, we will show you how to install MySQL 8 on FreeBSD.

Posted on

MySQL 8 is the latest major version upgrade of MySQL. It comes with many new features and improvements such as better performance, new SQL functions, new JSON data type support, atomic DDL statements, invisible indexes, etc.

In this tutorial, we will install MySQL 8 on FreeBSD using the pkg package manager.

Prerequisites

Before you install MySQL, make sure you have the following:

  • FreeBSD 12 or higher
  • Root privileges or access to sudo
  • An internet connection

Step 1 - Install pkg

Update FreeBSD and install the pkg package manager:

freebsd-update fetch
freebsd-update install
pkg update && pkg install pkg

Step 2 - Install MySQL

Use pkg to install MySQL 8:

pkg install mysql80-server

This will install mysql80-server package on FreeBSD.

Step 3 - Configure MySQL

Initialize the data directory and set a root password:

mysqld --initialize --user=mysql
mysqladmin -u root password 'new_password'

Start the MySQL service:

service mysql-server start

Step 4 - Create MySQL User

Create a dedicated MySQL user for your apps:

CREATE USER 'myapp'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON *.* TO 'myapp'@'localhost';

Step 5 - Test MySQL

Connect to MySQL and test it:

mysql -u myapp -p
SHOW DATABASES;

Step 6 - Create Sample Database

Create a test database and table:

CREATE DATABASE mytestdb;
USE mytestdb;

CREATE TABLE users (
  id INT AUTO_INCREMENT PRIMARY KEY,
  name VARCHAR(50),
  email VARCHAR(50)
);

MySQL is now installed and can be used!

Conclusion

We have demonstrated how to install MySQL 8 on FreeBSD using the pkg package manager. MySQL can now be used for your database applications on FreeBSD.

If you want to learn more about MySQL, please use our MySQL tutorials and MySQL Reference.