SU_PYROW

Setup and configure MySQL in Slackware Linux

By SU_PYROW

MySQL is a popular open source database which being used as database server by many organizations around the world. Normally, MySQL is used together with PHP server-side scripting language, Apache web server and Linux as the server platform. This tutorial is a step by step guide on how to setup and configure MySQL in Slackware Linux. If you are looking for this, then let's get started now.

First thing to do is to check whether mysql user and mysql group already exist in the system. You should have them if you installed mysql package during Slackware 12.2 installation.

root@node0:~# less /etc/passwd | grep mysql
mysql:x:27:27:MySQL:/var/lib/mysql:/bin/false
root@node0:~#
root@node0:~# less /etc/group | grep mysql
mysql:x:27:

Create new mysql user and mysql group if you don't have them. Learn how to create a new user in Slackware with Linux adduser command - add new user account in Linux system tutorial. You can find information about Linux group in groupadd command - manage groups in Linux system tutorial.

Check if a directory name mysql exist in /var/run directory.

root@node0:~# ls /var/run/ | grep mysql
mysql/
root@node0:~#

Create a new directory named mysql in /var/run directory if it's not there. Learn how to create new directory in Slackware in mkdir tutorial. When you are done, check mysql directory ownership. It must be owned by mysql user and mysql group.

root@node0:~# ls -l /var/run/ | grep mysql
drwxr-xr-x 2 mysql     mysql     4096 2011-10-05 13:29 mysql/
root@node0:~#

You can change directory ownership with Linux chown command. See the example below:

root@node0:~# chown -R mysql.mysql /var/run/mysql/
root@node0:~#

Install or upgrade mysql package in your Slackware system. See example on how to install mysql using slackpkg command below:

root@node0:~# slackpkg install mysql

Looking for mysql in package list. Please wait... DONE

No packages match the pattern for install. Try:

        /usr/sbin/slackpkg reinstall|upgrade

If you see the same result as the example below, try running the command again but using slackpkg upgrade option instead. Don't worry of you see the same result again. You probably have mysql installed and upgrade to the latest version already. You can confirm it by checking the installed packages in log directory. See the example below:

root@node0:~# ls -l /var/log/packages/ | grep mysql
-rw-r--r-- 1 root root    8159 2009-02-28 01:25 mysql-5.0.67-i486-1

Configure MySQL in Slackware 12.2 or above

There is a slight difference on how to configure MySQL in Slackware 12.2. You can follow the step by step MySQL configuration example below:

Create mysql user and mysql group:

root@node0:~# groupadd mysql
root@node0:~# useradd -G mysql -d /var/lib/mysql -s /bin/false mysql

You can also use usermod command to add mysql user to mysql group if mysql user already exist. See the example below:

root@node0:~# usermod -G mysql mysql
root@node0:~# id mysql
uid=27(mysql) gid=27(mysql) groups=27(mysql)

You have to create a new mysql configuration file called my.cnf. This can be done easily by copying the example (existing) configuration files in the /etc directory. You can view all available mysql configuration files in /etc directory using grep command. See the example below:

root@node0:~# ls -l /etc/ | grep my
-rw-r--r--  1 root root     4972 2011-10-05 13:29 my-huge.cnf
-rw-r--r--  1 root root     4948 2011-10-05 13:29 my-large.cnf
-rw-r--r--  1 root root     4955 2011-10-05 13:29 my-medium.cnf
-rw-r--r--  1 root root     2525 2011-10-05 13:29 my-small.cnf
root@node0:~#
root@node0:~# cp /etc/my-medium.cnf /etc/my.cnf

Create mysql database as Slackware 12.2 recommended:

root@node0:~# mysql_install_db --user=mysql
Installing MySQL system tables...
090423 04:55:12 [Warning] option 'max_join_size': unsigned value 18446744073709551615 adjusted to 4294967295
090423 04:55:12 [Warning] option 'max_join_size': unsigned value 18446744073709551615 adjusted to 4294967295
OK
Filling help tables...
090423 04:55:12 [Warning] option 'max_join_size': unsigned value 18446744073709551615 adjusted to 4294967295
090423 04:55:12 [Warning] option 'max_join_size': unsigned value 18446744073709551615 adjusted to 4294967295
OK

To start mysqld at boot time you have to copy support-files/mysql.server to the right place for your system

PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER ! To do so, start the server, then issue the following commands:

/usr/bin/mysqladmin -u root password 'new-password'
/usr/bin/mysqladmin -u root -h slackware password 'new-password'

Alternatively you can run:

/usr/bin/mysql_secure_installation

which will also give you the option of removing the test databases and anonymous user created by default. This is strongly recommended for production servers.

See the manual for more instructions.

You can start the MySQL daemon with:

cd /usr ; /usr/bin/mysqld_safe &

You can test the MySQL daemon with mysql-test-run.pl

cd mysql-test ; perl mysql-test-run.pl

Please report any problems with the /usr/bin/mysqlbug script!

The latest information about MySQL is available on the web at http://www.mysql.com
Support MySQL by buying support/licenses at http://shop.mysql.com
root@node0:~#

To start mysql daemon at boot time, we can change the mysqld script file permission, the /etc/rc.d/rc.mysqld.

root@node0:~# chmod 755 /etc/rc.d/rc.mysqld
root@node0:~# ls -l /etc/rc.d/ | grep mysqld
-rwxr-xr-x 1 root root  2585 2011-10-05 13:29 rc.mysqld*

start mysql daemon (mysql server) first.

root@node0:~# /usr/bin/mysqld_safe &

Create password for mysql root user:

root@node0:~# /usr/bin/mysqladmin -u root password 'new_password'

There is a tool that provide basic security for mysql including setup root password that you can run. The tool is mysql_secure_installation. It's recommended you run this command. See the example below:

root@node0:~# mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!

In order to log into MySQL to secure it, we'll need the current password for the root user. If you've just installed MySQL, and you haven't set the root password yet, the password will be blank, so you should just press enter here.

Enter current password for root (enter for none):

In order to log into MySQL to secure it, we'll need the current password for the root user. If you've just installed MySQL, and you haven't set the root password yet, the password will be blank, so you should just press enter here.

Enter current password for root (enter for none):

OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MySQL root user without the proper authorisation.

You already have a root password set, so you can safely answer 'n'.

Change the root password? [Y/n] Y
 ... Success!

By default, a MySQL installation has an anonymous user, allowing anyone to log into MySQL without having to have a user account created for them. This is intended only for testing, and to make the installation go a bit smoother. You should remove them before moving into a production environment.

Remove anonymous users? [Y/n] Y
 ... Success!

Normally, root should only be allowed to connect from 'localhost'. This ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] Y
 ... Success!

By default, MySQL comes with a database named 'test' that anyone can access. This is also intended only for testing, and should be removed before moving into a production environment.

Remove test database and access to it? [Y/n] Y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far will take effect immediately.


Reload privilege tables now? [Y/n] Y

 ... Success!

Cleaning up...

All done! If you've completed all of the above steps, your MySQL installation should now be secure.

Thanks for using MySQL!

root@node0:~#

That's it. The MySQL server is ready. We can test login and create a new database.

Testing MySQL

We can test logging into MySQL now:

su_pyrow@node0:~$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.0.67-log Source distribution

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql>
To view databases, we can use SHOW DATABASES command:

mysql> SHOW databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
+--------------------+
2 rows in set (0.00 sec)

mysql>

We can create a new database with CREATE DATABASE command. See the example on how to create a new database below:

mysql> CREATE DATABASE basicconfig
    -> ;
Query OK, 1 row affected (0.00 sec)

mysql> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| basicconfig        |
| mysql              |
+--------------------+
3 rows in set (0.00 sec)

mysql>

We can delete a database with DROP DATABASE command. See the example on how to drop a database example below:

mysql> DROP DATABASE basicconfig;
Query OK, 0 rows affected (0.01 sec)

mysql> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
+--------------------+
2 rows in set (0.00 sec)

mysql>

Finally, to exit mysql, use QUIT command or \q command:

mysql> QUIT
Bye
su_pyrow@node0:~$

Uninstall MySQL and remove MySQL data

if you need to remove mysql from Slackware, these are the steps:

Stop mysql service:

root@node0:~# killall -9 mysqld mysqld_safe
[1]+  Killed                  /usr/bin/mysqld_safe
root@node0:~#

Uninstall mysql using slackpkg:

root@node0:~# slackpkg remove mysql

Remove remaining mysql data and directory.

root@node0:~# rm -r /var/lib/mysql/
root@node0:~# rm /etc/my.cnf

Run updatedb command and invoke locate mysql to find remaining mysql data if you want.

This document was last modified on: 133120 13:08:26 UTC