0% found this document useful (0 votes)
8 views

The MySQL environment

Uploaded by

vhare457
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

The MySQL environment

Uploaded by

vhare457
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

IDBD201 Firs t P ractical

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.

Ins talling MySQL


Download the MySQL Community Server from the official MySQL downloads page at
https://www.mysql.com/downloads/
Please note that you do not have to register on the site, everything you need to do here
you can do anonymously.

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.

Using MySQL Workbench


For this week, we will use the command line. Therefore, we will look at SQL Workbench
a bit later.

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.

Using Command Line on Windows


From the Windows menu, choose
MySQL > MySQL 8.0 Command Line Client > Pin to Start

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:

Change to verbose mode


At first attempt the target should be:
"C:\Program Files\MySQL\MySQL Server 8.0\bin\mysql.exe" "--defaults-
file=C:\ProgramData\MySQL\MySQL Server 8.0\my.ini" "-uroot" "-p"

(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"

The change is adding a -v option to start it in verbose mode.

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).

Change the startup directory.


Change the startup directory to the root of where you store your scripts. This way, you
can use relative paths when specifying scripts to run.
When you start, the “Start in” directory (in the properties window) will be something
like:
"C:\Program Files\MySQL\MySQL Server 8.0\bin\".

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

is much longer than


source DatesDemo\SetupDemo.sql

especially if you do it hundreds of times!

Other approaches to running the command-line interface.


Another approach is to start from a terminal window in your host operating system.
You can then include the MySQL binary directory in your PATH variable to ensure that it
finds the executable when you type mysql -u root -p at the command prompt.

Running your first script


To run a script is straightforward., just type source followed by the file that contains
scripts. Copy the StartupDemo1.sql into a Week1 subdirectory in your SQL-scripts
directory. Then
source Week1\StartupDemo1.sql
Note what happens:

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).

Step 1 - Install and configure the tools.


Allowing data to be uploaded from text files introduce some security risks. For this
reason, the data loading functionality is not the default with MySQL. There is little risk
in an educational setting like ours, but that is not the case in the production
environment. However, loading from a text source seldom, if ever, happens to a
production database but will often happen on a staging setup.
Please follow the instructions in the “Configuring MySQL to allow bulk data uploads”
on page 15.

Step 2 - Prepare the input.


What should be obvious is that the database can be no better than the input files’
information. It is thus critical to consider the data quality upfront as many tools that will
generate text files do not have the checks and balances for proper integrity and data
quality. Of course, suppose your text file generated from an existing single database
with all the checks and balances incorporated into the database is notably simple. In
that case, if this data is coming from a spreadsheet that a group of people maintained,
you may be in for a treat. Anyway, in this course, I will provide the input files and
prepare them before the time but keep in mind that your life practice would be as
simple.
For this practical download the list of countries from
https://gist.github.com/kalinchernev/486393efcca01623b18d.

Step 3 - Run commands to load data.


Running the actual commands is the simple part. Depending on your text file’s structure
and contents, you would have to configure various options, but I think they are
straightforward. We will start this practical by creating a table that can contain all the
countries in the world. This is convenient if you have a worldwide system where users

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:

LOAD DATA LOCAL INFILE 'Data/countries.txt'


INTO TABLE Country (Name);

Please adjust the path and filename to point to your datafile.


If at this stage, you experience problems, you probably did not do everything in step one
quite correct. If errors occur, please return to step one and check your work.

Step 4 - Confirm success.


Before you start, you have to think about how you will know that your data transfer was
successful. It is usually easy if it is a small table like the one we are dealing with. In our
example, we can look at the contents in the database and see that it “looks right”. This
step might not be as simple if we talk about a big organisation and tens or hundreds of
thousands of records.
You can check whether you have 196 records by selecting all records and seeing how
many are returned, or you can type
SELECT COUNT(*) FROM Country;

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 check the setting from the MySQL prompt


SHOW GLOBAL VARIABLES LIKE 'local_infile';

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

You might also like