Normally, there's an ad running in this spot, but you're using an ad blocker. To help support our blog, which provides free tutorials for everybody, we just ask that you whitelist or follow us on facebook, twitter, or subscribe using the form to the leftabove. Thank you!

    Automating a MySQL Database Installation on a Virtual Machine with Fabric

    Select Your OS:

    In this guide, you're going to learn how to use a Fabric script to automate a MySQL database installation on a remote virtual machine.

    In order to complete this guide, you will need to know how to set up a local virtual machine to run the Fabric scripts against. You will also need to be familiar with setting up Virtualenv, which will allow us to install all the Fabric dependencies in an isolated virtual environment so there are no dependency conflicts with any other Python projects we set up in the future. In order to get up to speed, make sure you complete the guides in the prerequisites section.

    Fabric DB Scripts VM Pack 1

    Purchase the entire collection of Fabric DB Scripts at the Codebox Systems Shop and auto-provision MongoDB, MySQL, and PostgreSQL in seconds.

    Shop now

    System requirements

    • A 32-bit or 64-bit computer with at least 8GB of RAM. 16GB or more is recommended.
    • At least 50GB of free disk space.

    Set Up the Project and Virtualenv

    After completing the prerequisites, you may still be in an active Virtualenv. If the username in your terminal session is prepended with (venv), you will want to deactivate the virtualenv by running deactivate. For this tutorial, we're going to want to start from scratch with a new project and Virtualenv. This will allow us to install Fabric to our virtual environment rather than to the global dependencies list, and keep our system clean. Let's run some commands.

    Create the Project Skeleton

    Note: An existing Vagrant VM is required.

    Also, the VM must be running when you run the Fabric command. If you do not have a running VM, please go through the prerequisites.

          cd ~
          mkdir fabric-mysql
          cd fabric-mysql
          virtualenv --python=/usr/local/lib/python2.7 venv
          source venv/bin/activate

    Install Fabric Into the Virtualenv

    For this tutorial, we're going to install Fabric version 1.13.2.

          pip install fabric==1.13.2

    Fabfile Automation

    Create the Fabfile

    Create a file and name it with the following contents:

    Run the Fabfile

    Follow all of the prompts for setting a default MySQL root password, MySQL user, and MySQL password.

          fab vagrant install_mysql

    Possible Error: Fatal error: Host key for did not match pre-existing key!

    • Host OS: Ubuntu 16.04
    • Guest OS: Ubuntu 14.04
    • Vagrant versions: 1.8.1, 1.9.5, 2.0.0
    • VirtualBox versions: 5.0.26, 5.1.22 r115126 (Qt5.5.1)
    • Python 2.7.12
    • Fabric 1.13.2
            [localhost] local: vagrant global-status | grep running
    [localhost] local: vagrant ssh-config e48abe0 | grep IdentityFile
    [] Executing task 'install_mysql'
    [] run: install_mysql  
    Fatal error: Host key for did not match pre-existing key! Server's key was changed recently, or possible man-in-the-middle attack.  
    Underlying exception:
        ('', , )  

    Solution: Remove the old key from the known_hosts file

    1. Run the following command in the terminal:
    2.           ssh-keygen  -R []:2222
    3. Rerun the fab vagrant install_mysql command. If you get the same error, you will have to manually delete the line from ~/.ssh/known_hosts.

    Access MySQL from Host

    Install mysql-client on the host.

            sudo apt-get install mysql-client

    Install Homebrew from

          # Install MySQL
          brew install mysql

    And access MySQL.

        mysql -hMY.VM.IP.ADDRESS -umyuser -p

    Did you like this tutorial? Help us pay for server costs by following us on Facebook, Twitter, and subscribing below, where you'll get post notifications, training webinar invites, and free bundles.