How To Install MySQL on Embedded FreeNAS ?

FreeNAS Logo

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

  1. Edit the Socket, declare the path to where you want your mysql.sock to be saved.
  2. 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.

37 Responses to How To Install MySQL on Embedded FreeNAS ?

  1. Simon says:

    How doI create the script?

    #!/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

    • Just create a new empty script using vi or nano and then put those commands inside that file, save it as .sh file.
      e.g.
      nano /mnt/disk1/script/hooks.sh
      it will create a new file, named hooks.sh then write the commands.

  2. Simon says:

    Thanx. already got the script working. But another problem is that the pkg_add comment ends up with not installing the package. When trying it on the FreeNAS installation drive itself, it says that the drive is full (32GB are there) and creating those directories like you stated above on another drive, makes the install not possible because of the system not finding certain folders. Any clue? I tried several versions of freeNAS with this (8.0.3, 8.0.4, 8.2).
    Thanx again

  3. freud says:

    I keep getting this errors :
    /: write failed, filesystem is full
    info/mysql.info: Write failed: No space left on device: No space left on device
    tar: Error exit delayed from previous errors.
    pkg_add: leave_playpen: can’t chdir back to ”

    Any clue? Thanks a milion

  4. Michael says:

    Hi,

    Is the method to install MySQL on the full version of FreeNas the same? I have FreeNAS 8.0.4.

    Thanks

    • I am not sure about that, but I think it might be a bit different.

      • Michael says:

        I got it working on FreeNAS-8.0.4-RELEASE-p1-x64 (11059). Pretty much followed the instructions but I did the install and symlinks to a different disk.

      • chris says:

        How do you install to a different disk? Sorry I am very new to linux

      • Yes, you can. Based on my posting, on step 3, you just need to change the path where do you want to install it. Change the /mnt/disk1/Services/ become the directory where you want to install the packages.

      • chris says:

        Could I install a 2nd USB drive ans install.in on that?

      • Yes, you could. You just need to mount the USB drive into you system first. Then you can follow the exact step but just need to change the installation directory.

  5. Hakarune says:

    OK I am getting an error…
    I installed wget on your other tutorial, but when installing mySQL via this tutorial, I get an error after I create the script and run it.
    :::: error: ln: /usr/lib/X11: File exists

    I tried to find the error by bashing instead and got this:
    :::: + ln -s /mnt/disk1/Services/pkg/lib/X11 /mnt/disk1/Services/pkg/lib/mysql /usr/lib
    :::: ln: /usr/lib/X11: File exists

    Any ideas? I tried moving X11 and trying again, but still doesn’t work… am I missing something?

    I would love help ASAP, trying to get WordPress up and running on this thing.

    • Hakarune says:

      Also, I tried to ignore error, but on the next step I get:
      :: mysql_install_db: Command not found

      • Try to symlinks /bin of ur MySQL into ur system’s /bin.

      • Hakarune says:

        Not sure if I follow what you mean… How would I do that? I followed the directions and made the exact same file locations and names…. everything is identical…

        The /mnt/disk1/Services/pkg/bin has all these, but none work. Do you mean for me to: “ln -s /mnt/disk1/Services/pkg/bin/mysql_install_db /bin/mysql_install_db” for every command? (or to “/usr/bin”??

        ## bin ##
        innochecksum
        mysql_config
        mysql_tzinfo_to_sql
        mysqlbug
        mysqlshow
        msql2mysql
        mysql_convert_table_format
        mysql_upgrade
        mysqlcheck
        mysqlslap
        my_print_defaults
        mysql_find_rows
        mysql_waitpid
        mysqld_multi
        mysqltest
        myisam_ftdump
        mysql_fix_extensions
        mysql_zap
        mysqld_safe
        perror
        myisamchk
        mysql_install_db
        mysqlaccess
        mysqldump
        replace
        myisamlog
        mysql_plugin
        mysqlaccess.conf
        mysqldumpslow
        resolve_stack_dump
        myisampack
        mysql_secure_installation
        mysqladmin
        mysqlhotcopy
        resolveip
        mysql
        mysql_setpermission
        mysqlbinlog
        mysqlimport

      • Hakarune says:

        So… I went ahead and manually symlinked every single command, I can now run step 15 (mysql_install_db –user=mysql –basedir=/mnt/disk1/Services/pkg –datadir=/mnt/disk1/Services/db/mysql) but it returns this:

        “FATAL ERROR: Could not find ./bin/my_print_defaults”

        I checked /bin and it’s there, the link is there in /bin and I can run it myself, but during install in can’t….

      • Hakarune says:

        There’s no way to del or edit prev posts…. so… I was able to bypass that most recent problem my copying all symlinks to root folders instead of the usr folders, but now I get the error: “FATAL ERROR: Could not find ./share/mysql/fill_help_tables.sql” and I can’t figure out how to bypass it…..

  6. Hakarune says:

    OK, so I started over anew. I started from scratch, fresh new install of OS embeded and I’m getting the ‘mysql_install_db: Command not found’ error still. I symlinked everything manually after the script failed to produce the desired results. It failed again after I tried that. Here is the error log I documented. I don’t understand what is going on here at all….

    http://pastebin.com/9QQnRLXy

    • Hakarune says:

      ok, got past the ‘‘mysql_install_db: Command not found’ via rebash, but back to the “FATAL ERROR: Could not find ./bin/my_print_defaults”

      Any help would be appreciated

      • Michael says:

        I had quite a few similar problems to you, in the end I installed mysql to a seperate disk mounted as read / write.

        I also had to set the main freenas disk to read write, “mount -uw /” before I started the whole process.

  7. alexey says:

    If you have “FATAL ERROR: Could not find ./bin/my_print_defaults”
    1. Try install Mysql5.0 or Mysql5.1 – it work
    2. If need Mysql5.5 please execute before Step 15
    command
    cd /usr/local

  8. Vizi says:

    Hi Satya, a great how to. thanks for it! Everything worked fine for me and alsmost everything seems to work. except of one thing, I do not know where when and which password i’ve set for mysql.
    I can’t find this step and can not remember having ever done this step. (i set up mysql user with no password).
    Could you please help me?
    Thank you very much in advance!
    Vizi

  9. Vizi says:

    Hi,

    thanks again. i could finaly solve it by resetting the root password. i did it with the –init-file method.

    For those, who have similar problems here is the solution:

    > Find the mysql *.pid file (sql process) | In my case it is called ‘freenas.local.pid’ and is located here: ‘/mnt/hdd/Services/db/mysql/freenas.local.pid’

    > check if my sql is running by using this ‘/mnt/hdd/Services/pkg/share/mysql/mysql.server status’
    now kill the process ‘kill `cat /mnt/hdd/Services/db/mysql/freenas.local.pid`’

    >check if it’s really dead ‘/mnt/hdd/Services/pkg/share/mysql/mysql.server status’
    >now create the init file called ‘pwd-init’ ‘nano /mnt/hdd/Services/db/mysql/pwd-init’

    >insert content:
    SET PASSWORD FOR ‘root’@’localhost’ = PASSWORD(‘MyNewPassword’);
    FLUSH PRIVILEGES;

    > and NOW the difference to the all guides (i could find) 🙂 In the guide above we include parameters in order to start the sqld_safe. THESE are not mentioned in those guides :/

    /mnt/hdd/Services/pkg/bin/mysqld_safe -–user=mysql -–basedir=/mnt/hdd/Services/pkg –-datadir=/mnt/hdd/Services/db/mysql –init-file=/mnt/hdd/Services/db/mysql/pwd-init &

    These lines worked for me… I hope they can also help someone.

    Cheers

  10. javier says:

    when I make the scipt it doesnt works it seems like /usr/local/bin doesnt have RW permissions and I cant stablish them
    thanks

  11. Scott says:

    Hi,
    I got to step 14 but when i run the script i get the following error

    [root@freenas ~]# /mnt/Media/Services/Script/hooks.sh
    bash: /mnt/Media/Services/Script/hooks.sh: /bin/sh#Custom: bad interpreter: No s
    uch file or directory
    [root@freenas ~]#

    • Redianto S. says:

      Hi,

      1. Ensure that you refer to the correct path.
      2. Ensure that the hooks.sh is executable. Try to do chmod +x /mnt/Media/Services/Script/hooks.sh

      Cheers 😉

  12. Marco says:

    Hi There,
    I hope you can help me. Iam struggling at step3. I using putty to connect to my server. Login as root. But if i try this step “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/.” He tolds me “no such file or directory” (my command line: mkdir /mnt/disk1/Services/) What is wrong.
    Greets

  13. Marco says:

    ok it works:)
    First mkdir /mnt/disk1
    then mkdir /mnt/disk1/Services…got it

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: