Please check the tutorial page for updated tutorials on this topic!
MySQL is an open source database management system (RDBMS) provided by a commercial company acquired by Oracle. Although the software is free, the company provides commercial support and consultancy (this is a similar model to certain Linux distributions).
An RDBMS is a software tool to store, access and update (often large) amounts of data structured as interrelated tables. Originally, databases were typically used for adminstrative purposes such as storing employee- or inventory information. Nowadays, databases are also widely used to store the raw content from which dynamic web sites are generated. This allows the same information to be presented in different ways. Because SQL is a standardized language to update or access an RDBMS, it also avoids relying on proprietary storage formats with associated risks of obsolescence or lock-in to particular software.Although there are various other open source and commercial RDBMS systems available, MySQL is commonly used in web development in conjunction with Linux, Apache (or lighttpd), and php. Wikipedia, for example, runs on MediaWiki software written in PHP and uses a MySQL database.
Contents
Setting up MySQL
Installation
Uli kindly provided a packaged version of MySQL for the NAS in his repository.
MySQL is not installed as part of fun_plug by default, but you should already have downloaded a copy as part of the general tutorial on how to download, install and upgrade packages. Let’s first make sure you still have the latest version (as Uli upgrades his repository regularly).
Note that the installation command below could take a while:
funpkg -i /ffp/pkg/additional/*/mysql-*.tgz
If you have a version of mysql installed that is outdated, you will need to run in upgrade mode instead (see here for help):
funpkg -u /ffp/pkg/additional/*/mysql-*.tgz
Configuration
MySQL can be configured with a file called my.cnf. After installation you can configure several MySQL settings by copying an example-file from /ffp/etc/examples/mysql/ to /ffp/etc/:
cp /ffp/etc/examples/mysql/my-small.cnf /ffp/etc/my.cnf
Directories
MySQL stores the data of its databases in files which are in turn stored in a directory named /srv/mysql/. Instead of creating the directory at that location, you may prefer to create a symbolic link from /srv/ to the target-directory of your choice.
First we create it:
mkdir -p /ffp/opt/srv/mysql mkdir -p /ffp/opt/srv/tmp/mysql
Now we link it to /srv/:
ln -s /ffp/opt/srv/ /srv
This link will be lost after rebooting the device, so you have to add the following two lines to the end of the file /ffp/etc/fun_plug.init to recreate the link every time the NAS boots. You can edit this file using an editor like nano:
# create custom link to the server-folder ln -s /ffp/opt/srv/ /srv
Initialization
MySQL needs some internal databases for the initial startup which can be installed by issuing the mysql_install_db command:
cd /srv/ ls -al /ffp/bin/mysql_install_db ls -al
This results in several warnings (which you can ignore) about adjusted sizes system- and help tables. Typical partial output:
081116 22:05:32 [Warning] option 'max_join_size': unsigned value 18446744073709551615 adjusted to 4294967295 081116 22:05:32 [Warning] option 'max_join_size': unsigned value 18446744073709551615 adjusted to 4294967295 081116 22:05:32 [Warning] option 'myisam_max_extra_sort_file_size': unsigned value 2147483648 adjusted to 2147483647 081116 22:05:32 [Warning] option 'thread_stack': unsigned value 65536 adjusted to 131072
Now we manually start the MySQL server for further configuration:
sh /ffp/start/mysqld.sh start
Note that you will have to press Enter to get your prompt back (unlike other daemons).
After the first start, we have to secure the installation:
/ffp/bin/mysql_secure_installation
You will be asked several questions (shown below in abridged form) and can answer Y(es) for each of them.
For "Enter current password for root (enter for none):" you press enter because the default root password is empty.
For the new root password, it is best to use a different password than the user root of the system: this is just for owning the administration rights to the database and is unrelated to overall control over the machine.
Enter current password for root (enter for none): OK, successfully used password, moving on... Set root password? [Y/n] Y New password: Re-enter new password: Password updated successfully! Remove anonymous users? [Y/n] Y Disallow root login remotely? [Y/n] Y Remove test database and access to it? [Y/n] Y Reload privilege tables now? [Y/n] Y All done!
To activate this service permanently on every boot you need to enter this command:
chmod a+x /ffp/start/mysqld.sh
Testing MySQL
After MySQL has started, you can test your installation using the following ways:
Command-line
Enter the following command on the command-line:
mysql -pThis will open a special mysql-command-line, where you can enter regular SQL-Commands. Now change to the database “mysql”:
USE mysql;Then select the Host, User and Passwort from the Database:
SELECT Host, User, Password FROM user;
Finally exit the mysql-command-line:
exit;
A sample output will look like this:
root@CH3SNAS:/srv/mysql# mysql -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 5.0.67 Source distribution Type 'help;' or '\h' for help. Type '\c' to clear the buffer. mysql> USE mysql; Database changed mysql> SELECT Host, User, Password FROM user; +-----------+------+-------------------------------------------+ | Host | User | Password | +-----------+------+-------------------------------------------+ | localhost | root | *8D2414F01991E3B0B86E14D2469EACA0B6D78B99 | | CH3SNAS | root | *8D2414F01991E3B0B86E14D2469EACA0B6D78B99 | | 127.0.0.1 | root | *8D2414F01991E3B0B86E14D2469EACA0B6D78B99 | +-----------+------+-------------------------------------------+ 3 rows in set (0.01 sec) mysql> exit; Bye
By the way: As you can see, passwords are crypted (in this case it was “nas-tweaks.net“).
PHP
For the following, you need to install lighttpd and php in case you haven’t already done so.
You also need to enable the mysql module of lighttpd by editing the /ffp/etc/php.ini file:
; Linux extensions extension=calendar.so ;extension=ctype.so ;extension=ftp.so ;extension=gd.so ;extension=mbstring.so extension=mysql.so ;etc
Make sure you define the folder which contains all the above modules in line 536 of the php.ini file. Usually it should be:
extension_dir = "/ffp/lib/php/extensions/no-debug-non-zts-20060613/"You will need to restart the web server if you enabled the my-sql extension, or changed the extension_dir using:
sh /ffp/start/lighttpd.sh restart
Then place a file called testmysql.php in the document-root (as configured here e.g. /srv/www/pages) with the following content (replace YOURROOTPASS with the password of mysql user root):
<?php // Connect to the database mysql_connect("localhost", "root","YOURROOTPASS"); // Select the database "mysql" mysql_select_db("mysql"); // Query the database for the Users: $result = mysql_query("SELECT Host, User, Password FROM user;"); // Print the results while($row = mysql_fetch_object($result)) { echo $row->User . "@" . $row->Host . " has the encrypted password: " . $row->Password; } // Close the connection to the database mysql_close(); ?>
If opening this page in your browser doesn’t give the expected results, check the password, and if needed close and open the browser again.
When you are done, you may want remove the root password (or delete this .php file) to avoid exposing the mysql password in the line mysql_connect("localhost", "root","YOURROOTPASS");.
Users and privileges
Adding additional Users
As you should never use the root-password of your database, you can add additional users in the mysql-command-line (enter “mysql -uroot -p” on the command-line).
Please consult the mysql-manual for more examples.
- A User with all privileges, who can only connect from localhost:
GRANT ALL PRIVILEGES ON *.* TO 'YOURUSERNAME'@'localhost' IDENTIFIED BY 'YOURPASSWORD' WITH GRANT OPTION;
- A User with limited privileges, who can only connect from localhost:
GRANT SELECT,INSERT,UPDATE,DELETE ON *.* TO 'YOUROTHERUSERNAME'@'localhost' IDENTIFIED BY 'YOUROTHERPASSWORD' WITH GRANT OPTION;
- A User with limited privileges on a certain database:
CREATE DATABASE databasename; GRANT SELECT,INSERT,UPDATE,DELETE ON databasename.* TO 'YOURSPECIALUSERNAME'@'localhost' IDENTIFIED BY 'YOURSPECIALPASSWORD' WITH GRANT OPTION;
After you send add or alter the rights, please make sure, that these get loaded by executing the following command in the mysql-command-line:
FLUSH PRIVILEGES;Allowing external access
Per default external access is not allowed as this is a security risk. But many tools like HeidiSQL or other external administrator-programs rely on access from the outside of your NAS.
Caution: You should explicitly check the rights of your users! All MySQL-users should have passwords!
First follow the section on “running mysql under a user with limited rights“, then follow these instructions:
Stop the mysql-server:
sh /ffp/start/mysqld.sh stop
Edit /ffp/etc/my.cnf and add a comment to the line skip-networking, so that it looks like this:
#skip-networkingEdit /ffp/start/mysqld.sh and find the line beginning with mysqld_flags and remove “--skip-networking” between the two quotation marks. Save the file afterwards.
Running mysql under a user with limited rights
Per default the MySQL gets started with root-rights. This means, that if the MySQL-server is breached by a intruder, the system probably can be corrupted. Be advised to use the mysql-server only in secure areas (e.g. your local LAN without internet access) and to disable external access (default).
If you want to secure your installation please follow the following steps:
Stop the mysql-server:
sh /ffp/start/mysqld.sh stop
Add a new user with limited rights:
useradd -U -s /bin/false mysql store-passwd.sh
This will create a user mysql who is in the group mysql (-U add a new group) and who cannot log in. It will probably show up in the Webinterface, but cannot be used!
This user needs access to the directories of MySQL:
cd /srv chown -R mysql:mysql mysql cd /ffp/var/run/ chown mysql:mysql mysql
Edit /ffp/start/mysqld.sh and find the line beginning with mysqld_flags and remove “--user=root” between the two quotation marks. Save the file afterwards.
Then start MySQL again:
sh /ffp/start/mysqld.sh start
With these changes, MySQL is started under the user mysql.
Removing MySQL
If you want to remove MySQL and its databases, you proceed like described in the general tutorial on packages.
First, stop mysql:
sh /ffp/start/mysqld.sh stop
Then remove the package with funpkg:
funpkg -r /ffp/pkg/additional/*/mysql*.tgz
Afterwards you have to remove the databases (careful!). Change to the folder /srv/ and delete the folder mysql:
cd /srv/ rm -R mysql
Furthermore, you should undo the change in /ffp/etc/fun_plug.init, possibly remove the (harmless) symbolic link /srv/, but especially disable execution of the startup script:
chmod a-x /ffp/start/mysqld.sh
Voilá, MySQL is removed.

Found a small bug in this tutorial. It’s kind of typo mistake but I got error when I ran this command:
mysql>SELECT Host, USER, Password FROM USER;Error 1146: Table mysql.USER doesn't exist.
Next window in this tutorial with the same line of command it’s a bit different and it reads:
mysql> SELECT Host, User, Password FROM user;and this is correct and it gives output as expected.
Thanks, fixed. The highlighter for the code changed it, now it’s not highlighted but syntactically correct
Regards,
Uli
Hi,
I followed the guide and all worked fine unitl the secure install. It says to type the root password in, so i leave it bank and it gives an error:
Enter current password for root (enter for none):ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/ffp/var/run/mysql/mysql.sock' (2)
I dont know what to do. The lighttp and php have been installed already using the guides here (they are excellent by the way).
Any help would be great,
Steve
forgot to add this is on a DNS-320
Hi Steve,
Did you start the MySQL-Server?
Best Regards,
Uli
Working wiht some php apps that now require mysqli.so extension for php. Is ther a possibility you could compile and package this extension. My PHP/mySQL version combo happens to be 5.2.9 and 5.1.28. I have tried to use the PHP extension generator using phpize, but ran into issues with the c compiler portion of the ./configure script.
Hello JLM,
with the next version of php, i will compile mysqli. Currently compiling php takes too long but i will consider recompiling it as soon as possible.
Cheers,
Uli
Have you compiled php to include the mysqli extension? I need it badly and I’d appreciate it a lot if you can share it.
There is a typo in this article that breaks the MySQL install:
# create custom link to the server-folderln -s /mnt/HD_a2/www/ /srv
should actually be
# create custom link to the server-folderln -s /mnt/HD_a2/srv/ /srv
Fixed, thanks a lot!
no problem, thanks for the awesome tutorial… MySQL, and subsiquently phpMyAdmin, are finally working as expected and rockin’ it out. Cheers.
Hi firstly THANK YOU for the fantastic tutorials made my life a breeze. Secondly…
item 1.1 Updating mysql code is wrong:
It reads: “
funpkg -u /ffp/pkg/additional/mysql-*.tgz”should say: “
funpkg -u /ffp/pkg/additional/dev-db/mysql-*.tgz”I’d worked my way through the guide only to discover MySQL wasn’t being loaded by PHP. Took me a while to realise it was a version issue as I’d presumed it was already the latest version!
Fixed, thank you very much!
Hey – one quick fix for the “Running mysql under a user with limited rights” section:
You also need to chown mysql:mysql tmp/mysql so that mysql can create its temp files. It will fall back on inefficient methods (like “repair by keycache”) if it can’t write temp files, but you really want to have the temp files dir writable.
I was fine until I tested a mysqltest.php
2011-07-14 00:23:24: (mod_fastcgi.c.2698) FastCGI-stderr: PHP Fatal error: Call to undefined function mysql_connect() in /mnt/HD_a2/www/pages/testmysql.php on line 3
Did I miss an include somewhere?
I got it. PHP.ini
extension=msql.so
should read
extension=mysql.so
Not sure if the file was delivered that way or I finger fudged it.
Hello
Iam new in ffp .i install mysql on my ffp 0.5 successfully but i need access remotely to MySQL server but i can’t access it remotely
DNS-321 and myIP 192.168.0.10 user root or Any other user
Thanks
Hello, can cause long handle page display?
The course here -
http://www.webpagetest.org/result/110821_K4_1CHA0/
Server always has a long answer to the first date, then it is fast.
configuration – the latest version wordpress, php, mysql and lighhtpd
Hi,
thats normal as PHP needs a moment to load.
Cheers,
Uli
OK, but up 7 seconds is really enough.
There’s something to speed up? Preload?
Hi, my name is Jose, I am from Spain so please be patient with my English, I will try to do my best.
I have a DNS-323, I just started to install ‘fonz fun_plug’ and as fas as I know everything has gone well, I didn’t get any error.
Right know I have finished to install the lighttpd and that’s why I am writting this message.
It is supposed that my ports has changed from 80 to 81 (I mean to connect the standard configuration screen) and I will enjoy my new web server on 80. But nothing at all has happened in that way.
I still have the configuration screen in 192.168.1.11 as always (wich is my local ip for the NAS), even I have rebooted the system.
Althought, I created the three files; index, page1 and page2.html as the manual indicates me and located them into /www I can not reach them, of course.
Does anybody know what was wrong? I had read very carefully all the steps many times and I think I have not forgotten any of them.
It is like ‘sh /ffp/start/kickwebs.sh’ were not doing its job.
Thanks for your help.
I will be looking for an answer.
Kind regards.
Jose.
Is there anyway to disable mysqld_safe logging?
Thanks
I have a problem when testing mysql with lighttp+php: I get a message saying:
PHP Fatal error: Call to undefined function mysql_connect() )
I’ve followed exactly the procedure given in your tutorial. Now my status is
lighttp is working (index.html is OK)
php is working (index.php is OK)
mysql is working (using the command line)
but php + mysql is not working ! (the page testmysql.php is returning the error mentionned above)
I’ve checked again and again the php.ini was correctly configured. After some peeking around, I found that the file mysql.sock ends up in the folder /mnt/HD_a4/ffp/var/
it might be normal, but when reading the different configuration file, I get the idea that it should be located in /mnt/HD_a4/ffp/var/run/mysql and there is nothing there!
shall I be concerned about it ?
Hi,
I installed mysql under funplug 0.7 and everytime I want to the secure the installation, I get the following problem:
Enter current password for root (enter for none):
ERROR 2013 (HY000): Lost connection to MySQL server at ‘sending authentication information’, system error: 32
everything else up to this step worked fine..
Any ideas?
Thanks
Spot
Spot / Uli
I am having the same problem. I’m working with a DNS-321 and used slacker to update/install the pkgs.
Paul
I had the same problem for my DNS-320, and later found out that the installed mysql package was out dated. The problem was resolved by updating the mysql package using this command:
funpkg -u /ffp/pkg/additional/*/mysql-*.tgz
as stated in the tutorial.
– Wilkin
Hi, I’ve secured my installation and everything is working great. Except when I reboot the NAS, mysql starts as root which doesn’t have access anymore
If I kill the process and start mysql using the start script all if fine. Have I missed a step here?
James