How To Install MySQL on Embedded FreeNAS ?
07/03/2012 37 Comments
Step 1
Create a new group.
Open your FreeNAS WebGUI -> Access -> Users and Groups -> Groups.
Create a new group whose name is mysql.
Step 2
Create a new user.
Open your FreeNAS WebGUI -> Access -> Users and Groups -> Users.
Create a new user, with the following details:-
Name: mysql
Shell: nologin
Primary group: mysql
Step 3
SSH into your FreeNAS as root.
Create a new folder where your packages will be saved, e.g. the new folder name will be pkg and it is under the /mnt/disk1/Services/.
Set the permission of the folder into777.
We will call this folder as BASEDIR.
mkdir /mnt/disk1/Services/pkg
chmod -R 777 /mnt/disk1/Services/pkg
Step 4
Create another folder to hold temporary files during package installation, e.g. folder name is tmp and it is under /mnt/disk1/.
Set the permission of the folder into 777.
Assign mysql user and group as the owner.
mkdir /mnt/disk1/tmp
chmod -R 777 /mnt/disk1/tmp
chown -R mysql:mysql /mnt/disk1/tmp
Step 5
Create a folder for the database files, e.g. the folder name is db/mysql and located in/mnt/disk1/Services/.
Set the permission of the folder into 777.
Assign mysql user and group as the owner.
We will call this folder as DATADIR.
mkdir /mnt/disk1/Services/db
mkdir /mnt/disk1/Services/db/mysql
chmod -R 777 /mnt/disk1/Services/db
chown -R mysql:mysql /mnt/disk1/Services/db
Step 6
Check your FreeNAS version by running this command:-
uname -a
Once, you ran the command, you will get the output as shown below.
The output tells us that the system is running under FreeBSD 9.0-RELEASE on x64 architecture.
Step 7
Get the URL for the package site based on your system, the URL format:-
ftp://ftp.freebsd.org/pub/FreeBSD/ports/<ARCHITECTURE>/<SYSTEM_VERSION>/Latest/
You might go to ftp://ftp.freebsd.org/pub/FreeBSD/ports
to check for the correct URL.
For example the URL of my system would be:-
ftp://ftp.freebsd.org/pub/FreeBSD/ports/amd64/packages-9.0-release/Latest/
Step 8
Run the following command to set the package site for your system. The package site URL is based on the URL that you got from Step 7.
setenv PACKAGESITE<THE_PACKAGE_SITE_URL>
Thus, the command for my system would be:-
setenv PACKAGESITE ftp://ftp.freebsd.org/pub/FreeBSD/ports/amd64/packages-9.0-release/Latest/
Step 9
Set the folder that has been created before on Step 4 as a temporary directory.
setenv PKG_TMPDIR /mnt/disk1/tmp
Step 10
Install mysql into your packages destination folder that has been created before on Step 3.
pkg_add -r mysql55-server -P /mnt/disk1/Services/pkg
Step 11
Open the my-small.cnf:-
nano /mnt/disk1/Services/pkg/share/mysql/my-small.cnf
- Edit the Socket, declare the path to where you want your mysql.sock to be saved.
- Add the User and declare it as mysql
[client]
socket = /mnt/disk1/tmp/mysql.sock
[mysqld]
user = mysql
socket = /mnt/disk1/tmp/mysql.sock
Save it as my.cnf.
Move it into etc folder under your BASEDIR.
mv /mnt/disk1/Services/pkg/share/mysql/my.cnf /mnt/disk1/Services/pkg/etc/my.cnf
Step 12
Open and edit mysql.server.
Add the path of BASEDIR and DATADIR on the following basedir and datadir variable in mysql.server.
nano /mnt/disk1/Services/pkg/share/mysql/mysql.server
basedir=/mnt/disk1/Services/pkg
datadir=/mnt/disk1/Services/db/mysql
Save it.
Step 13
Create an empty file, mysql_log, on /mnt/disk1/ as a log file for the mysql daemon.
nano /mnt/disk1/mysql_log
Save it.
Step 14
We need to create necessary symlinks with our system, thus you need to create a script with the content as followed:-
#!/bin/sh#Custom Library Symlinks
ln -s /mnt/disk1/Services/pkg/lib/* /usr/lib#Symlinks Bin
ln -s /mnt/disk1/Services/pkg/bin/* /usr/local/bin#MySQL Symlinks
ln -s /mnt/disk1/Services/pkg/libexec/mysqld /usr/local/libexec/mysqld
ln -s /mnt/disk1/Services/pkg/etc/my.cnf /etc/my.cnf
You might change the script based on where you installed the packages.
Save the script
For example, my script is saved as hooks.sh and then we need to set the permission of the script to be executable:-
chmod +x /mnt/disk1/script/hooks.sh
Run the script.
/mnt/disk1/script/hooks.sh
Step 15
Once you ran the script above, it will create necessary symlinks with your system.
If your script above ran without problem, you might able to run the command below:-
mysql_install_db –user=mysql –basedir=/mnt/disk1/Services/pkg –datadir=/mnt/disk1/Services/db/mysql
You might change the basedir and datadir paths according to your system.
Step 16
Run the mysql daemon with the following command:-
mysqld_safe –user=mysql –basedir=/mnt/disk1/Services/pkg –datadir=/mnt/disk1/Services/db/mysql
Step 17
Check your whether your MySQL is running by run this command:-
/mnt/disk1/Services/pkg/share/mysql/mysql.server status
If your MySQL is running, you might find the output as shown below:-
Furthermore, you will be able to run the MySQL by calling the mysql command.
Step 18
To run the MySQL automatically during boot time, add the following line to hooks.sh, which have been created before on Step 14.
echo `date` `/mnt/disk1/Services/pkg/share/mysql/mysql.server start` >> /mnt/disk1/mysql_log
Open your FreeNAS WebGUI, go to System —> Advanced —> Command scripts.
Add the script that you have created before on Step 9 on the command field:-
/mnt/disk1/hooks.sh
Select the type as PostInit.