This is an old revision of the document!


MySQL Server: Installation & Management

The next fundamental piece of our web development environment is, of course, a database management system of choice: MySQL.

Installing MySQL server is an easy task, simply issue the following commands in a Terminal window:

> sudo apt-get install mysql-server

You will be asked to enter the password you wish to set for the mysql root user.
Once the install process has completed, verify that mysql is running using:

> sudo service mysql status

Easy! Once you have installed MySQL, we should activate it with this command:

> sudo mysql_install_db

Finish up by running the MySQL set up script:

> sudo /usr/bin/mysql_secure_installation

In order for other computers on your network to view the server you have created, you must first edit the “Bind Address”. Begin by opening up Terminal to edit the my.cnf file.

> sudo nano /etc/mysql/my.cnf

Change the line

bind-address = 127.0.0.1

Changing the 127.0.0.1 to your IP address.

> sudo apt-get install libapache2-mod-auth-mysql php5-mysql

Now just restart Apache and you are all set!

It is fundamental to protect access to you databases, setting a root password is essential to this point. In case the installer didn't ask you for a password, or you left it blank, you'll have to set it “manually”, here is how:
Note that the same procedure can be used to change the current root user's password.

mysql -u root
mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('yourpassword');

Make sure to change yourpassword to a password of your choice.



This is for a VirtualBox system only
As was done for our Virtual Hosts files, we'll configure our VirtualBox VM so that all our database files will be stored inside a directory that does not reside inside our VM's but rather on our host's filesystem.

Please refer to the apache virtual host setup section for deeper details on this configuration, as it relies on the same principles.

Let's create this directory on the host's side (feel free to adapt this folder location to wherever you see fit):

> mkdir /home/<username>/Documents/webdev/mysql

Then, configure it as a VirtualBox shared folder:

> VBoxManage sharedfolder add "lamp-xd" --name "mysql-share" --hostpath "/home/<username>/Documents/webdev/mysql"

Prepare MySQL Storage Location

By default all MySQL related databases files are stored under /var/lib/mysql. What we'll do is first stop the mysql service, then create a copy of the existing data, mount our “host” shared folder with the correct access rights, replace the copied data inside the newly mounted directory and restart the mysql service.

Inside the VM:

> service mysql stop
> sudo cp -a /var/lib/mysql /var/lib/mysql.bak

> id mysql
uid=104(mysql) gid=108(mysql) groups=108(mysql)

> sudo nano /etc/fstab
ADD:
# automount mysqlshare as mysql user
mysql-share	/var/lib/mysql/	vboxsf	defaults,uid=104,gid=108	0	0

> sudo mount /var/lib/mysql
> cp -a /var/lib/mysql.bak/* /var/lib/mysql/
> service mysql start