Create test databases in MySQL

Sometimes you need to create a test database on a development MySQL server, to run some tests on, and you want to populate it with many records, to make your tests more meaningful. This document lists a couple of quick and dirty ways that I use to achieve that.

Using /usr/share/dict/words

On most Linux distributions, there is a file at /usr/share/dict/words which contains a list of words, each on a separate line. On my Linux Mint 15 installation, I count more than 99000 lines. The following commands will put those words in a table called words, in a database called test, one word per table row.

Create the table:

mysql> CREATE TABLE words (id INT AUTO_INCREMENT, word VARCHAR(256));

Populate the table with the words. I 'm using double quotes to wrap the words, since many of them already contain a single quote:

while read word; do mysql test -e "INSERT INTO words (word) VALUES (\"$word\")"; done < /usr/share/dict/words

Using /var/log/messages

I have also used the contents of /var/log/messages to populate a table, and that is documented in the blog post How to create a large MySQL database for tests. That is a painfully slow method, but can create tables that are many GigaBytes in size, which might be necessary for some tests.

virsh

List all VMs:

    virsh list --all

Automatically boot a VM when the host boots:

    virsh autostart vm.zindilis.net

Disable automatic start:

    virsh autostart --disable vm.zindilis.net

Manually power on a VM:

    virsh start vm.zindilis.net

Gracefully shutdown a VM:

    virsh shutdown vm.zindilis.net

Forcefully shutdown a VM:

    virsh destroy vm.zindilis.net

/etc/shadow

The fields, delimited by : are:

  1. Username
  2. Password (hashed)
  3. Last password change (in days since 1970-01-01)

Fields

Notes on specific fields.

Username

Fairly straightforward. The lenght used to be restricted to 8 characters maximum in very old distributions, now it's 32 characters on most distributions.

Password

An empty field, will allow the user to login without providing a password, whereas a field that starts with, or only contains an asterisk * indicates a disabled account.

The first characters of this field define the hashing algorithm, one of:

  1. MD5 - password starts with $1$
  2. Blowfish - password starts with $2a$
  3. SHA-256 - password starts with $5$
  4. SHA-512 - password starts with $6$

dirname

The dirname command will give you the path of the file that you provide as parameter, up to its parent directory. If the parameter is an absolute path, the absolute containing directory will be returned, otherwise it will be the relative path.

For example, for the file /home/marios/.bash_aliases:

{% highlight bash %} mariosz@super-mario:~$ dirname /home/marios/.bashaliases /home/marios mariosz@super-mario:~$ dirname ~/.bashaliases /home/marios mariosz@super-mario:~$ dirname .bash_aliases . {% endhighlight %}

See also

Set up an HTTP Repository from RHEL DVD

RHEL can be installed from various different sources. One of them is over the network, from an HTTP accessible repository. Here's how to create such a repository:

  1. You will first need to install httpd from the RHEL DVD. See Install packages from RHEL DVD with yum on how to do that.

  2. After you have installed httpd, enable it and start it:

    chkconfig httpd on
    service httpd start
    

    At this point, you should be able to open http://localhost/ from the same system on which you are working.

  3. Create a directory for the repository:

    mkdir /var/www/html/rhel
    
  4. Copy all the files from the DVD to the repository. Assuming that either the DVD or the .iso image is mounted at /media/rhel:

    cp --recursive --archive /media/rhel/. /var/www/html/rhel/
    
  5. Change the SELinux context of the files in the repository:

    chcon --recursive --reference=/var/www/html/ /var/www/html/rhel/
    
  6. At this point the repository is only accessible from the system on which it runs, since iptables by default does not allow HTTP traffic from other hosts. To open this access, edit your /etc/sysconfig/iptables and add this line before the COMMIT command:

    -A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
    

    ...and reload the firewall:

    service iptables reload
    

    Alternatively, you can do from the command line:

    iptables -I INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
    service iptables save
    

tail

tail can display the last lines of a text file, and can optionally follow the file and display new lines as they are appended to it.

See also

  • tailf: a utility that does the same as tail -f, but consumes less resources on the system.

Install packages from RHEL DVD with yum

There are a lot of packages available on the RedHat Enterprise Linux Installation DVD (I count 3764 .rpm files on version 6.5 Beta), and you can mount the DVD on your system and then use it as a repository to install them with yum. To do that:

  1. First you need access to the files on the DVD. If the disk is inserted in a physical DVD drive on your system, or if you are working in a virtual machine and you have the DVD attached to the VM's virtual optical drive, you can mount it with:

    mkdir /media/rhel
    mount /dev/cdrom /media/rhel
    

    Otherwise, if you have the .iso image locally on the system, you can mount that one instead:

    mkdir /media/rhel
    mount -o loop /root/rhel.iso /media/rhel
    
  2. Next, you need to create a Yum repository pointing to the mounted disk. To do that, create a new .repo file in the /etc/yum.repos.d/ directory:

    vi /etc/yum.repos.d/rhel-media.repo
    

    ...and populate it with the following lines:

    [rhel-media]
    name=rhel-media
    baseurl=file:///media/rhel
    gpgcheck=1
    enabled=1
    gpgfile=file:///media/rhel/RPM-GPG-KEY-redhat-release
    

    If you don't remember the configuration lines listed above, you can peak at other .repo files in /etc/yum.repos.d/.

You can then install packages, for example httpd with simply doing:

yum install httpd

You can also verify that the DVD is the source for that package with:

yum info  httpd | grep Repo
Repo        : rhel-media

Bash Scripting

Functions

About

Hello, I'm Marios Zindilis and this is my website. Opinions are my own. You also find me on LinkedIn and GitHub.

Unless otherwise specified, content is licensed under CC0.

Search