MySQL – Creating a new user

It’s funny how you forget the simple things when you haven’t done them in a while.

Tonight I wanted to setup MySQL on my Windows 7 machine. The installation was simple and I happily had my root account created. However, I didn’t want to use my root account for development – mainly because I didn’t want the password to be sorted in clear text.

In order to create a new user, I used the command line MySQL client tool which allows me to execute commands against the server. You enter this via the command.

mysql --user="root" ––password

This will then prompt you for the root password. I could have entered this on the command line as well, but again it would be in the clear.

From the tool, I enter the following two commands. The first creates the user, the second assigns permissions.

CREATE USER 'new_username'@'localhost' IDENTIFIED BY 'password';

GRANT ALL ON *.* TO 'new_username'@'localhost';

I can then happily use this new user with my application. I wanted to post this in case anyone else keeps forgetting like me…

One thought on “MySQL – Creating a new user”

  1. I do always forget also but I wrote a bash script to remind me of it.
    So that I can just cut and paste it into mysql 🙂

    #!/usr/local/bin/bash

    if [ $# -eq 0 ] # Must have command-line args to demo script.
    then
    echo “Please invoke this script with one or more command-line arguments.

    echo “$0 dbname username password”
    exit;
    fi

    echo “CREATE DATABASE IF NOT EXISTS $1;”
    echo “GRANT ALL PRIVILEGES ON $1.* TO ‘$2’@’localhost'”
    echo “IDENTIFIED BY ‘$3’ WITH GRANT OPTION;”

Leave a Reply

Your email address will not be published. Required fields are marked *