Installing MySQL for development
MySQL is an excellent starting point for getting on track in a developmental career. It is also well-suited to local development on your machine, since the setup is pretty easy.
How to set up MySQL on your machine depends on the operating system. As we mentioned in Chapter 1, Preparing Your Development Environment, we are assuming that you are using a Debian-based system. For this, you can use the following instructions. If you already have a working setup for MySQL or Apache, these commands may not work, or may not be required in the first place.
Execute the following steps to get MySQL running:
- First, you should always install all of the updates available for your system:
sudo apt-get update && sudo apt-get upgrade -y
We want to install MySQL and a GUI, in order to see what we have inside of our database. The most common GUI for a MySQL server is phpMyAdmin. It requires the installation of a web server and PHP. We are going to install Apache as our web server.
- Install all dependencies with the following command:
sudo apt-get install apache2 mysql-server php php-pear php-mysql
- After the installation, you will need to run the MySQL setup in the root shell. You will have to enter the root password for this. Alternatively, you can run sudo -i:
su -
- Now, you can execute the MySQL installation command; follow the steps as prompted. From my point of view, you can ignore most of these steps, but be careful when you are asked for the root password of your MySQL instance. Since this is a development server on your local machine, you can skip the security settings:
mysql_secure_installation
- We must create a separate user for development, aside from the root and phpMyAdmin user. It is discouraged to use the root user at all. Log in to our MySQL Server with the root user in order to accomplish this:
mysql -u root
- Now, run the following SQL command. You can replace the PASSWORD string with the password that you want. It is the password that you will use for the database connection in your application, but also when logging in to phpMyAdmin. This command creates a user called devuser, with root privileges that are acceptable for local development:
GRANT ALL PRIVILEGES ON *.* TO 'devuser'@'%' IDENTIFIED BY 'PASSWORD';
- You can install phpMyAdmin, since our MySQL server has been set up. You will be asked for a web server when executing the following command. Select apache2 with the spacebar, and navigate to ok by hitting the Tab key. Select the automatic setup method for phpMyAdmin, when asked for it. You should not do this manually.
Furthermore, phpMyAdmin will want you to enter a password. I recommend that you choose the same password that you chose for the root user:
sudo apt-get install phpmyadmin
- After the installation, we will need to set up Apache, in order to serve phpMyAdmin. The following ln command creates a symbolic link in the root folder of the Apache public HTML folder. Apache will now serve phpMyAdmin:
cd /var/www/html/
sudo ln -s /usr/share/phpmyadmin
We can now visit phpMyAdmin under http://localhost/phpmyadmin and log in with the newly created user. It should look like the following screenshot:
We have now finished the complete database installation for our development environment.
PhpMyAdmin chooses the language according to your environment, so it might differ slightly from the preceding screenshot.