The MySQL environment
The MySQL environment
MySQL and MySQL Work bench should be installed in the Lab. So if in the lab you may
skip the installation part and jump to running scripts. The installation instructions will
be useful in allowing you to download and install the software on your home PCs.
Note that for Windows, you download an installer, which would, in turn, download the
components that you want to install.
1
2
You can download the 64-bit version (without debugging binaries), although
downloading the 32-bit version on the Downloads page will not be a problem in this
module. The 32-bit version will have a smaller footprint, which may be important to
you.
If you are asked for you to login, just choose “No thanks, just start my download”.
I have installed everything, but we will not use everything in this module. Critical for the
first half of the module is MySQL Server and MySQL Workbench. I also recommend
downloading the MySQL Documentation and the Samples and Examples. In the second
half of the module, we will also (probably) need MySQL for Visual Studio and the .NET
Connector.
Please note that the installer check for dependencies. Thus, if you don’t have all the
prerequisites installed, the installer will complain. Don’t worry about that you can
always come back later and install those componenets. This may be the case for Visual
Studio, .NET and PHP.
3
For most of the options, you can use the default options. There are a few details to
supply and some things to note.
For Type and Networking, you can choose the default.
Note that MySQL 8 has stronger authentication than previous versions. It is advisable to
use the strongest password encryption that is available.
4
Since databases potentially contain sensitive information, they must be protected.
Towards the end of this module, we will look at security considerations and
mechanisms, but for now, suffice to say that we need to protect the database.
Please note: MySQL accounts differ from login accounts for your operating system. For
example, the MySQL root user and the Unix/OSX root user are separate and have
nothing to do with each other, even though the username is the same in each case.
It is also good not to use one admin role in a database, as we lose traceability and cannot
enforce individual accountability. It is thus always a good idea to create individual users
who may have admin rights. That way, you can keep an individual accountable for his
actions. You may argue that this is unnecessary for a development database but
remember that development efforts should eventually end on a production server. That
requires the development environment to be configured as closely as possible to the
production environment. Also, note that I am setting a bad example here using a weak
5
password. I will use a much stronger password in a production environment (or even a
development environment dealing with high-stakes development).
The Windows Service information can stay default, although you may need to think
about it on large development servers. That level of database administration is far
outside the scope of this course.
Following the prompts, you will eventually reach the end of the installation.
6
Congratulations! You are now ready to start your database design and development
journey practically.
7
Running s cripts in MySQL
This subject regularly provides scripts to demonstrate concepts. Note that these scripts
can be run through the SQL Workbench or the SQL Command Prompt. While SQL
Workbench is a very nice environment when you play with SQL yourself, using it in
command line mode is better when running scripts. In verbose mode, it shows the
commands that run and the output all on one (long) page, not requiring you to switch
between tabs to try and figure out what is what.
About Scripts
Scripts are text files with commands you would be typing one after another. Regarding
SQL scripts, please note the following:
• I give SQL scripts the extension .sql. While this is the norm for SQL scripts, it is
not necessary - the only real requirement is that it is a text file with SQL
commands inside.
• Comments take the following forms: - /* … */ for long-form comments spanning
several lines - # for making the rest of the line a comment.
• Two dashes ( -- ) can also be used in place of the #
Note that the script can (and in most cases will) contain multiple commands. The
comments will not be displayed, but it is provided to have commentary on what is
happening.
Running a script is necessary and straightforward but “unimportant”. It is essential to
understand why the script produces the output it does. Of course, some of the
commands will be unfamiliar to you in the beginning. Don't worry; we will eventually
get to that. Please read the comments, and concentrate on the commands we are busy
discussing.
Note that the scripts are pretty often not best practice. In fact, in some cases, parts of it
are deliberately wrong so that you can see the result of doing things wrong and
compare it with the correct way.
Some commands will also rarely be used in production scripts. For example, most
scripts contain commands to ensure that any previous version of the database or tables
is removed before commencing with the script. This is quite useful in a development
and training environment, where one might “play” with various options and want to
start from scratch every time. In production environments, this is less useful, as data
contained in the tables will also be lost.
8
Running from the Command Line
On a Mac or Linux, you will simply type
/usr/local/mysql/bin/mysql -u root -p
You can replace root with the username, and if you put /user/local/mysql/bin in the
default search path, you could leave that out of the command. To ensure the SQL
statement is being repeated, add a -v to start the terminal in verbose mode.
But since most of you probably use a windows machine, let’s concentrate on that.
It is not necessary to pin it, but you will use it a lot. Now let us configure it some more.
Right-click the pinned shortcut and choose More > Open file location
You will be faced with a window as below:
(Note that the version number will differ if you run a different (newer) version)
Change the Target to (see the red):
"C:\Program Files\MySQL\MySQL Server 8.0\bin\mysql.exe" "--defaults-
file=C:\ProgramData\MySQL\MySQL Server 8.0\my.ini" "-uroot" "-p" "-v"
9
Verbose mode is very nice when running scripts, but less so when you type the
commands as it immediately repeats the command. Therefore, you may want to create
two versions, one to use when typing commands (non-verbose mode) and one for
running scripts (verbose mode).
You change the “Start in” to an existing directory to store your scripts, such as "C:\SQL-
scripts\".
Of course, you can choose your own place to store your scripts. Just make sure the
directory exists and remember where to put it.
If you don't do that, you will always have to specify the file path, which is cumbersome if
you use it a lot (as you should). Typing, for example
source C:\SQL-script\DatesDemo\SetupDemo.sql
10
This was an example of creating a table. Similarly, we can use queries. Run the
StartupDemo2.sql script. We will discuss what you have been seeing in the second
week.
11
12
Loading data into a table in MySQL
As systems progress, one is often required to populate a “new” database table with the
content from somewhere else. Some data will be in other databases, some in
proprietary file formats, some in spreadsheets, and some in text files. In a real-life
situation, there will probably be a staging area where the data is placed in a temporary
database without checks and balances. Data can be cleaned, and integrity checks can be
done before the production database is populated. While we will not go into detail, the
options, often spreadsheet and other proprietary data formats, will be converted to
plain text files and loaded into the staging database. We will, in this practical, look at
how MySQL deals with this. While the details differ somewhat from platform to
platform, the basic principles are present in similar toolsets on other database
platforms.
So here are the steps one should follow (assuming that you already have MySQL
installed).
13
must select their country of origin. This will be a two-step process. Firstly we will create
the necessary table, and secondly, we will upload the records from a file. On completion,
you should have a contact table with 196 records.
1. From the MySQL prompt, run the CreateCountry.sql script to create a table
suitable for the countries.
2. And load the data using the following command:
14
Configuring MySQL to allow bulk data uploads
If you don’t follow these instructions, you will likely get the following error.
ERROR 2068 (HY000): LOAD DATA LOCAL INFILE file request rejected due to
restrictions on access.
(If you are interested in details, you can follow the link and see the discussion on
StackOverflow.)
The error points to the following setting
SET GLOBAL local_infile = true;
You can set it in the session, but you must set it every time you start a new session. So
we will include the setting in the default initialisation file. But before that, consider the
second possible error. The MySQL daemon (process) will only be able to write to
selected directories. This is controlled by the secure_file_priv option, which has three
options:
• A NULL value means the data export or import is disabled
• An Empty value means data export or import is ultimately enabled
• A directory path value means data export or import is enabled only for the
specified path.
In practice, you will probably set secure-file-priv to a specific directory, and only
while data loading is necessary. The value is read-only, so it cannot be changed from the
MySQL prompt but must be set in the configuration file. Such a change requires a restart
of the database for the new setting to effect.
Some assistance
A caveat: when querying the variable at the MySQL prompt, it uses underscores (_), but
in the ini-file, the variable name contains dashes (-). This post provides valuable
information on the secure_file_priv option.
In this practical, you will edit the MySQL configuration file. Note that this instruction
was compiled and tested on Windows 10 with MySQL 8.0; you may need to tweak and
google a couple of settings if you have another setup.
Note that the configuration file is a system file and can thus not be edited by a “normal”
user. Since you will edit a text file, start Notepad, but right-click and choose the “Run as
administrator option”.
15
Open C:\ProgramData\MySQL\MySQL Server 8.0\my.ini
sFind the [mysql] and [mysqld] sections. For each, add a line local-infile
You will find that secure-file-priv is in the [mysqld] section and is set to a specific
directory. Comment that line out with a # and add the line that reads secure-file-
priv="" to ensure that all directories are accessible.
You can start, stop, or restart the MySQL database from the ‘Services’ utility. To start the
‘Services’ utility, use the Run window. Press the Win + R keys on your keyboard to open
the Run window. Then, type “services.msc” and hit Enter or press OK. A window like the
16
one below will show. You can select the MySQL process. Please note the name changes
with each version - the screenshot shows the process for MySQL 8.0.
Once you have restarted the MySQL services, you can now check from the MySQL
prompt if the changes had the required effect:
SHOW GLOBAL VARIABLES LIKE 'local_infile';
SHOW GLOBAL VARIABLES LIKE 'secure_file_priv';
17