If you’re on a Mac, and need a MariaDB server for development, you can install one using MacPorts… but the setup is not self-explanatory.
Today, I will show you how to install MariaDB 10.3 (or any 10.x) server on Mac!
1. MacPorts
First install [MacPorts from here] and then get back to this guide.
2. Installing and setting up MariaDB
Now, open a terminal and run:
sudo port selfupdate
sudo port install mariadb-10.3-server
Which should build and install MariaDB server, and create the _mysql user and group for us.
Now let’s create the database directory and give it the right permissions:
sudo mkdir -p /opt/local/var/db/mariadb-10.3
sudo chown -R _mysql:_mysql /opt/local/var/db/mariadb-10.3
Now let’s create the default database files using the included program, which we will run as the _mysql user:
sudo -u _mysql /opt/local/lib/mariadb-10.3/bin/mysql_install_db
If you want to run MariaDB server over a network (TCP) socket (port 3306) in addition to a UNIX socket, run:
sudo nano /opt/local/etc/mariadb-10.3/my.cnf
And comment out the line with # so that it reads:
#!include /opt/local/etc/mariadb-10.3/macports-default.cnf
Now let’s start the MariaDB server, as the _mysql user, with:
sudo -u _mysql /opt/local/lib/mariadb-10.3/bin/mysqld_safe --datadir='/opt/local/var/db/mariadb-10.3'
You can connect to it to test that it’s working with:
/opt/local/lib/mariadb-10.3/bin/mysql
Now let’s secure the installation, setting up passwords and things:
/opt/local/lib/mariadb-10.3/bin/mysql_secure_installation
Follow the steps, mostly answering with yes and setting the password.
3. Adding MariaDB to the PATH
Now, typing out these long commands every time is tedious. If you only need one version of MariaDB, we can add it to the path.
macOS Catalina and newer:
nano ~/.zprofile
macOS Mojave and earlier:
nano ~/.bashrc
And add the following contents to the file:
# MariaDB
export PATH="/opt/local/lib/mariadb-10.3/bin/:$PATH"
Now close and reopen your terminal. You should now be able to run the mysql command without the long path. Tip: If you get an error with your username, try mysql -u root
.
4. Automatic log-in
Now, typing username and password every time can be annoying, so add your MySQL username and password to a local config file!
nano ~/.my.cnf
And add the following contents:
[mysql]
user=your_mysql_username
password=your_mysql_password
[mysqladmin]
user=your_mysql_username
password=your_mysql_password
Of course, replacing those placeholders with your actual username and password.
5. Shutting down MariaDB server
Assuming you added MariaDB to the PATH (point 3) and set up automatic log-in (point 4), you can shut down the MariaDB server with the command:
mysqladmin shutdown
6. Properly starting and stopping MariaDB server
From now on, we won’t start the MariaDB server with a long, ugly command, and instead we can simply run:
sudo port load mariadb-10.3-server
to start, and
sudo port unload mariadb-10.3-server
to stop it again.
Conclusion
While it might seem overwhelming and confusing at first, with my guide it becomes easy as pie! Just make sure to complete all steps, and you’ll be good to go.
Did this guide help you out? I’m an individual doing this in my spare time. Feel free [to donate a coffee] so I can keep this blog going!
That’s all there is to it!
“Properly starting and stopping MariaDB Server” doesn’t work for me:
~% sudo port load mariadb-10.6-server
Password:
Error: Failed to load mariadb-10.6-server: Launchd plist /Library/LaunchDaemons/org.macports.mariadb-10.6-server.plist was not found
Error: See /opt/local/var/macports/logs/_opt_local_var_macports_sources_rsync.macports.org_macports_release_tarballs_ports_databases_mariadb-10.6/mariadb-10.6-server/main.log for details.
Error: Follow https://guide.macports.org/#project.tickets if you believe there
is a bug.
Error: Processing of port mariadb-10.6-server failed
Hi, it seems the file you pointed to, doesn’t exist.
Okay. I got mariadb-10.6 installed and working. After I got the permission errors, I deleted the directory that I created in this step:
% sudo mkdir -p /opt/local/var/db/mariadb-10.3
by doing this:
/opt/local/var/db% sudo rm -rf mariadb-10.6
Then I recreated the directory and changed the ownership:
% sudo mkdir -p /opt/local/var/db/mariadb-10.6
% sudo chown -R _mysql:_mysql /opt/local/var/db/mariadb-10.6
Then I installed the default database files:
% sudo -u _mysql /opt/local/lib/mariadb-10.6/bin/mysql_install_db
and that worked without error. Here is the output:
=======
Installing MariaDB/MySQL system tables in ‘/opt/local/var/db/mariadb-10.6’ …
OK
To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system
Two all-privilege accounts were created.
One is root@localhost, it has no password, but you need to
be system ‘root’ user to connect. Use, for example, sudo mysql
The second is _mysql@localhost, it has no password either, but
you need to be the system ‘_mysql’ user to connect.
After connecting you can set the password, if you would need to be
able to connect as any of these users with a password and without sudo
See the MariaDB Knowledgebase at https://mariadb.com/kb
You can start the MariaDB daemon with:
cd ‘/opt/local’ ; /opt/local/lib/mariadb-10.6/bin/mysqld_safe –datadir=’/opt/local/var/db/mariadb-10.6′
You can test the MariaDB daemon with mysql-test-run.pl
cd ‘/opt/local/mysql-test’ ; perl mysql-test-run.pl
Please report any problems at https://mariadb.org/jira
The latest information about MariaDB is available at https://mariadb.org/.
Consider joining MariaDB’s strong and vibrant community:
https://mariadb.org/get-involved/
========
Then, I tried to start mariadb:
/opt/local/var/db% sudo -u _mysql /opt/local/lib/mariadb-10.6/bin/mysqld_safe –datadir=’/opt/local/var/db/mariadb-10.6′
But I got a permission error:
220511 16:26:13 mysqld_safe Logging to ‘/opt/local/var/db/mariadb-10.6/7studs-MacBook-Pro.local.err’.
mkdir: /opt/local/var/run/mariadb-10.6: Permission denied
220511 16:26:13 mysqld_safe Fatal error Can’t create database directory ”
So, I decided to create the directory mentioned in the error message myself:
% sudo mkdir /opt/local/var/run/mariadb-10.6
and I changed the ownership of the directory:
% sudo chown -R _mysql:_mysql /opt/local/var/run/mariadb-10.6
Then, mariadb started successfully:
/opt/local/var/run% sudo -u _mysql /opt/local/lib/mariadb-10.6/bin/mysqld_safe –datadir=’/opt/local/var/db/mariadb-10.6′
220511 16:30:48 mysqld_safe Logging to ‘/opt/local/var/db/mariadb-10.6/7studs-MacBook-Pro.local.err’.
220511 16:30:48 mysqld_safe Starting mariadbd daemon with databases from /opt/local/var/db/mariadb-10.6
In another Terminal window, I typed:
~% mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 10.6.7-MariaDB Source distribution
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.
MariaDB [(none)]> show databases;
+——————–+
| Database |
+——————–+
| information_schema |
| test |
+——————–+
2 rows in set (0.002 sec)
Yeah!
Note that this web page’s software changes double dashes into single dashes. I actually posted double dashes preceding “datadir”.
Hi
Thanks for sharing your efforts. However there is a simpler solution to the permissions issue when running mysqld_safe: the permissions are too restrictive on all the SUBdirectories of /opt/local/var/db/mariadb-10.6. So all you need to do is :
sudo chmod -R 777 /opt/local/var/db/
and the problem goes away.
Hi,
I get permission errors when I follow your instructions after I installed the port mariadb-10.6:
~% sudo chown -R _mysql:_mysql /opt/local/var/db/mariadb-10.6
Password:
~% sudo -u _mysql /opt/local/lib/mariadb-10.6/bin/mysql_install_db (this is the 2nd time I ran this command, but I wanted to show you the errors I was getting the first time I ran it.)
shell-init: error retrieving current directory: getcwd: cannot access parent directories: Permission denied
job-working-directory: error retrieving current directory: getcwd: cannot access parent directories: Permission denied
job-working-directory: error retrieving current directory: getcwd: cannot access parent directories: Permission denied
job-working-directory: error retrieving current directory: getcwd: cannot access parent directories: Permission denied
mysql.user table already exists!
Run mysql_upgrade, not mysql_install_db
~% sudo -u _mysql /opt/local/lib/mariadb-10.6/bin/mysqld_safe –datadir=’/opt/local/var/db/mariadb-10.6′
shell-init: error retrieving current directory: getcwd: cannot access parent directories: Permission denied
job-working-directory: error retrieving current directory: getcwd: cannot access parent directories: Permission denied
job-working-directory: error retrieving current directory: getcwd: cannot access parent directories: Permission denied
job-working-directory: error retrieving current directory: getcwd: cannot access parent directories: Permission denied
job-working-directory: error retrieving current directory: getcwd: cannot access parent directories: Permission denied
chdir: error retrieving current directory: getcwd: cannot access parent directories: Permission denied
job-working-directory: error retrieving current directory: getcwd: cannot access parent directories: Permission denied
job-working-directory: error retrieving current directory: getcwd: cannot access parent directories: Permission denied
job-working-directory: error retrieving current directory: getcwd: cannot access parent directories: Permission denied
220511 15:56:44 mysqld_safe Logging to ‘/opt/local/var/db/mariadb-10.6/7studs-MacBook-Pro.local.err’.
job-working-directory: error retrieving current directory: getcwd: cannot access parent directories: Permission denied
mkdir: /opt/local/var/run/mariadb-10.6: Permission denied
220511 15:56:44 mysqld_safe Fatal error Can’t create database directory ”
job-working-directory: error retrieving current directory: getcwd: cannot access parent directories: Permission denied
~%