MySQL on Windows

MySQL Installation

MySQL has a number of products these days, including a the 5.5 and 5.6 branches of its community-based server. This document describes a recent 5.5 version. The package you'll need is the MySQL installation file (from the course website).
32-bit: mysql-5.5.41-win32.msi
64-bit: mysql-5.5.41-winx64.msi
This software (in other versions) is available from the MySQL site (although it's sometimes not obvious how to get it):
MySQL home:
Start by double-clicking the ".msi" file. Make these choices: Here are the configuration steps. The only ones which do not take the default behavior are steps 7 and 8.
  1. Select Configuration Type: take the default, Detailed Configuration. It's a good idea just to see the choices that you're making even if you take the defaults.
  2. Server Type: probably the default, Developer Machine.
  3. Database Usage: probably the default, Multifunctional Database.
  4. InnoDB Tablespace Settings: Take the defaults.
  5. Concurrent Connections Settings: Take the default, Decision Support (DSS/OLAP)
  6. Networking Options: Keep the defaults, Enable TCP/IP Networking and Enable Strict Mode.
  7. Default Character Set: probably the default, Standard Character Set.
  8. Windows options: You want both, so check the second checkbox.
    Install As Windows Service
    Include Bin Directory in Windows PATH
  9. Security Options: Uncheck this checkbox to maintain the initial empty root password.
    Modify Security Settings
  10. Ready To Execute: Go for it! Press the Execute button.

    If the "Start Service" fails, go back to the Windows Options panel and select a different name for the Windows service. If the Apply security settings fails, it may be that you already have a password.
The installation of binaries, configuration files, etc., is the folder:
\Program Files\Mysql\MySQL Server 5.5
The actual databases reside in a subfolder named data, typically in an "Application Data" folder:
\Documents and Settings\All Users\Application Data\MySQL Server 5.5\data
The Windows Programs menu contains the item:
MySQL command-line client: (accessses the command-line mysql as administrative)
After the installation MySQL will probably start up automatically as a background service. Perhaps the easiest way to get to Services is to key this word into the Search Box of the Run menu. Look for MySQL in the list of services, the startup type should be "automatic" and it should be "started" after installation.

Command line management of MySQL

The binaries in the bin subfolder of the installation folder. Hopefully MySQL has already installed this folder in your PATH environment variable. If not, add it through the system Environment Variables. Use the Search Box or
Control Panel → System → Advanced → Environment Variables

The MySQL shell client

Assuming that the MySQL installation directory's bin subdiretory in your PATH, you can use the mysql executable to connect to and deliver SQL commands to your MySQL database via a command-line SQL interpreter.

The MySQL adminstrator's user name is "root". The MySQL DBMS contains an adminstrative database named mysql in which all access information is stored. The root user has global priviledges to do any modifications or additions.

We only ever want to use the administrative database, mysql, for it's intended purpose, to control access to other databases. Furthermore, we usually want to avoid accessing the MySQL DBMS as root user because there is always the remote possibility that we inadvertently alter the tables in the mysql database and thereby foul up the DBMS — not every author shares my reticence of being MySQL root user.

To run some basic tests it's useful to have an unpriviledged, password-less, "guest" user which can access a test database. On other systems, the MySQL initialization may create the test database which can be accessed by any MySQL user. An anonymous user may also often created. The recent Ubuntu versions do none of these creations.

The following sequence of commands are meant to create some basic setup features and show the effects (highlighted ones most important): There are a number of repeated commands which can easily be accessed by using the "up arrow" key.
$ mysql -u root
mysql> show databases;
mysql> create database test;                         (it may already exist)
mysql> show databases;
mysql> select user,host,password from mysql.user;
mysql> create user guest@localhost;                  (it may already exist)
mysql> select user,host,password from mysql.user;
mysql> select user,host from mysql.db;
mysql> grant all on test.* to guest@localhost;
mysql> select user,host from mysql.db;
mysql> quit
Test the effectiveness by accessing the test database as the guest user:
$ mysql -u guest test
If for some reason this doesn't work, try forcing a reload of the administrative database:
$ mysqladmin -u root reload
Compare the differences in priviledges between the root and guest users (the \G statement terminator is used to field information in list form instead of tabular form):
$ mysql -u root mysql
mysql> select * from user where user='root' and host='localhost' \G
mysql> select * from user where user='guest' and host='localhost' \G

SQL syntax learning examples

Here are some examples you can use to help learn basic SQL commands. The test database accessible by the guest user without password is assumed. Start a command shell and, from the command-line, execute:
$ mysql -u guest               
mysql> use test;
mysql> create table things (thing varchar(10), qty int);
mysql> show tables;
mysql> describe things;
mysql> insert into things values ( 'book', 10 );
mysql> insert into things values ( 'pencil', 4 ), ( 'book', 5 );
mysql> select * from things;
mysql> delete from things where thing='book';
mysql> select * from things;
mysql> insert into things values  ('table', 2), ('chair',12);
mysql> select * from things;
mysql> update things set qty=qty-1 where qty>10;
mysql> select * from things;
mysql> update things set qty=qty+1 where thing='table';
mysql> select * from things;
mysql> drop table things;
mysql> quit

Using passwords with mysql client

If a user access is password-protected, the mysql client requires the usage of the -p option to provide the password. This can be done in one of two ways:
  1. The simple, unadorned -p option, with prompt:
    $ mysql -p  -u ....
    Enter password: MY-PASSWORD
  2. The -p option with password appended (useful for testing, but not a good idea in general):
    $ mysql -pMY-PASSWORD  -u ....

Using Cygwin for shell access

If, like me, you prefer the Cygwin shell, you can still get access to your Windows MySQL database via a mysql client. Although the Windows mysql client is is accessible though the Windows PATH, it is not the one to use.

Instead, install the mysql client package in Cygwin, but don't install the mysql server package. Then you will be able to access the Windows MySQL database with an added option like this:
$ mysql --protocol TCP ...
To simplify this usage, create an alias by adding this line to your ~/.bashrc file:
alias mysql='mysql --protocol TCP'
Open a new Cygwin shell and my can now use the mysql client as you would on a Windows command shell, e.g.:
$ mysql -u root

Discussion points

Creating a MySQL users with a password

If you want to create a user some_user with the password some_password, the replacement command would be this:
mysql> create user some_user@localhost identified by 'some password';

Establishing privileges for a mysql user

We saw above how the mysql command-line interpreter can grant privileges by which users can access the databases. Here are some other examples:
  1. To create a new highly-privileged user, priv@host (host the desired entry host):
    $ mysql -u root
    mysql> grant all on *.* to priv@host;
  2. To give a user restricted@host "read only" privileges on somedb, we replace the "all" in grant by the desired restricted access:
    $ mysql -u root
    mysql> grant select on somedb.* to restricted@host;

MySQL Command-line administration

MySQL software provides a number of useful commands to manipulate its databases, including: You can see the entire set of choices from the shell using tab completion by doing:
$ mysql[TAB][TAB]
For example, try these commands:
$ mysqlshow -u root
$ mysqlshow -u root mysql
$ mysqlshow -u root mysql user
$ mysqldump -u root mysql user
To ensure that changes made to the mysql database are not picked up, do one of this::
$ mysqladmin -u root reload
There's also the "refresh" command which has a somewhat different outcome.

Backup and reload

The mysql commands provide an excellent scheme by which a database can be "backed up" and then "reloaded". Do the backup like this:
$ mysqldump -u root somedb > somedb.sql
The somedb.sql file contains the data in all the tables plus the commands needed to recreate these tables. A restoration from an non-existent database would be done like this:
$ mysql -u root 
mysql> create somedb
mysql> use somedb
mysql> source somedb.sql

MySQL access principles

MySQL is a network-oriented DBMS. Client programs may reside on different hosts than the server. The access rights of MySQL client has to a MySQL DBMS database is determined at the beginning by three factors managed by the administrative mysql database: Three tables are consulted to determine access rights:
  1. user table: When the client on host connects to the MySQL server, the pair
    (user, host)
    is matched against the (user,host) values in the rows of the user table. If no such user or host exists, an empty is used to match. If the password field is non-empty the client must provide the password. The client obtains global privileges from the remaining fields in the record.
  2. host table: When a client on host attempts to use a specific database, the pair:
    (host, database)
    is used to match against the entries in the host table, and thereby augment the privileges available to the client when accessing from this host.
  3. db table: Each database, with user information is listed in the db table.
    (user, database)
    is used to match against the entries in the db table, and therefore further augment the privileges available to the client as this particular user.
Of the three tables, The db table is the most common place where privileges are assigned for non-root users since it is the most specific to the database.

Further refinements, which we will omit the discuss of these access privileges, governed by dedicated tables in the mysql database: A few tests exhibit some of the ideas discussed here:
$ mysql -u root mysql
mysql> describe user;
mysql> select user,host,password from user;
mysql> describe host;
mysql> select host,db from host;
mysql> describe db;
mysql> select user,db from db;
mysql> select * from db where db='test' \G          (mostly 'Y')
The root user has all global privileges and the anonymous user has none. Nevertheless, the anonymous user gains privileges for the test database by matching an entry in the db table.

Using MySQL on an external server

Assume you have a server machine running the MySQL service and you want to connect to it from an external client. This situation is common in an intranet where one might have a dedicated databse server with other clients and or servers using it. It could also be useful on a single computer running virtual machines.

For sake of definiteness, we will assume the server running MySQL is a recent Ubuntu system with Assume that it has a static IP address EXTERNAL_IP_ADDRESS. The client is external and wants to use this MySQL service on this server. Here are the steps necessary:
  1. Deal with the firewall

    If you have a firewall running, then somehow or another, you need to get through to the mysql port (3306) from the outside. On Ubuntu, using the ufw firewall management, look at the output of
    $ sudo ufw status
    to see if there is a firewall, and if it is open for the mysql port (with TCP). If not, you can open it by doing:
    $ sudo ufw allow mysql/tcp
  2. Have MySQL listen on the external network interface

    By default the MySQL server only listens on an internal socket and the localhost network interface. As root, edit the MySQL configuration file, /etc/mysql/my.cnf and look for the bind-address line for the server:
    bind-address            =
    Right underneath that line, add this one:
    bind-address            = EXTERNAL_IP_ADDRESS
    We need to restart the service for this to take effect, but we can do it after the next change.
  3. Give access to a user from external host

    The guest user we created above only has access from localhost. We need access from all external clients of interest. The easiest way is to make a guest user with access from any host:
    $ mysql -u root
    mysql> drop user guest@localhost;       (if you have already added it)
    mysql> create user guest;               (unspecified host means any)
    mysql> grant all on test.* to guest;
    mysql> select user,host,password from mysql.user;  (host for the guest user is %)
That's it. Now restart MySQL to pick up all the changes:
$ sudo restart mysql
If you have the mysql command-line client program on the client, you would use this form to access the MySQL DBMS on the server:
$ mysql -h EXTERNAL_IP_ADDRESS -u guest test

Tunnelling to the MySQL server port

If you only have access to the server through ssh (port 22), then it is possible to set up a tunnel through ssh to the mysql port (3306) and use the tunnel port as an alternative access. This tunnelling setup would be done on the client machine and so is dependent of the operating system. On Windows the PuTTy client can be set up to do the tunneling. Otherwise, the ssh command itself supports a tunneling setup. One point about tunneling is that the mysql command-line client would no longer function because it does not support external access via an alternative port.

© Robert M. Kline