Mastering MySQL For The Web by Sufyan Bin Uzayr
Mastering MySQL For The Web by Sufyan Bin Uzayr
Mastering MySQL For The Web by Sufyan Bin Uzayr
DOI: 10.1201/9781003229629
Typeset in Minion
by KnowledgeWorks Global Ltd.
Contents
v
vi ◾ Contents
APPRAISAL, 275
INDEX, 285
About the Editor
xi
Chapter 1
Introduction
to MySQL
IN THIS CHAPTER
DOI: 10.1201/9781003229629-1 1
2 ◾ Mastering MySqul for Web
1 https://www.javatpoint.com/mysql-versions, Javatpoint
4 ◾ Mastering MySqul for Web
DATABASE BASICS
2 https://cloudxlab.com/assessment/displayslide/1384/mysql-database-termi-
nology, CloudxLab
8 ◾ Mastering MySqul for Web
MySQL DATABASE
MySQL is a fast, easy-to-use RDBMS that is utilized by
many small and big businesses. MySQL is so widely popu-
lar because of many different reasons:
3 https://www.edureka.co/blog/what-is-mysql/, Edureka
Introduction to MySQL ◾ 11
4 https://www.edureka.co/blog/what-is-mysql/, Edureka
Introduction to MySQL ◾ 13
• SSL support
• Query caching
• Sub-SELECTs (i.e., nested SELECTs)
• Built-in replication support
• Asynchronous replication: master–slave from one
master to many slaves or many masters to one slave
• Semi-synchronous replication: master-to-slave repli-
cation where the master waits on replication
• Synchronous replication: Multi-master replication is
provided in MySQL Cluster.
• Virtual Synchronous: Self-managed groups of MySQL
servers with multi-master support can be done using
Galera Cluster or the built-in Group Replication plugin.
• Full-text indexing and searching
• Embedded database library
• Unicode support
• Partitioned tables with the pruning of partitions in
the optimizer
• Shared-nothing clustering through MySQL Cluster
• Multiple storage engines, allowing one to choose the
one that is most effective for each table in the application
• Native storage engines InnoDB, MyISAM, Merge,
Memory (heap), Federated, Archive, CSV, Blackhole,
and NDB Cluster
16 ◾ Mastering MySqul for Web
5 https://dev.mysql.com/doc/workbench/en/wb-what-is-new-63.html, MySQL
Introduction to MySQL ◾ 23
6 https://dev.mysql.com/doc/workbench/en/wb-what-is-new.html, MySQL
24 ◾ Mastering MySqul for Web
DISADVANTAGES
Nevertheless, the MySQL database server has its disadvan-
tages. Some of these disadvantages include the following:
7 https://dev.mysql.com/doc/refman/5.6/en/identifier-length.html, MySQL
34 ◾ Mastering MySqul for Web
8 https://dev.mysql.com/doc/mysql-reslimits-excerpt/5.7/en/grant-tables-
scope-column-properties.html, MySQL
Introduction to MySQL ◾ 35
Getting Started
with MySQL
IN THIS CHAPTER
DOI: 10.1201/9781003229629-2 41
42 ◾ Mastering MySqul for Web
1 https://dev.mysql.com/doc/refman/8.0/en/binary-installation.html, MySQL
Getting Started with MySQL ◾ 47
# cd /usr/ports/databases/mysql80-server
# make
# cd /usr/ports/databases/mysql80-server
# make deinstall
• MySQL Shell
• MySQL Router
• MySQL Workbench
• MySQL for Visual Studio
• MySQL Connectors (for .NET / Python / ODBC /
Java / C++)
• MySQL Documentation
• MySQL Samples and Examples
Even though the Server data files check box was removed
from MySQL Server 8.0. and higher you can still perform
the following actions freely:
2 https://dev.mysql.com/doc/workbench/en/wb-mysql-connections-secure-
auth.html, MySQL
60 ◾ Mastering MySqul for Web
alias mysql=/usr/local/mysql/bin/mysql
alias mysqladmin=/usr/local/mysql/bin/
mysqladmin
alias mysql/usr/local/mysql/bin/mysql
alias mysqladmin/usr/local/mysql/bin/
mysqladmin
3 https://dev.mysql.com/doc/mysql-macos-excerpt/5.7/en/macos-installation-
notes.html, MySQL
Getting Started with MySQL ◾ 65
(Continued)
Getting Started with MySQL ◾ 67
4 https://dev.mysql.com/doc/workbench/en/wb-manage-server-connections.
html, MySQL
Getting Started with MySQL ◾ 71
5 https://dev.mysql.com/doc/workbench/en/wb-manage-server-connections.
html, MySQL
Getting Started with MySQL ◾ 73
6 https://dev.mysql.com/doc/workbench/en/wb-manage-server-connections.
html, MySQL
74 ◾ Mastering MySqul for Web
7 https://dev.mysql.com/doc/workbench/en/wb-manage-server-connections.
html, MySQL
Getting Started with MySQL ◾ 75
SSH Configuration
If you defined a Remote Host on the Specify Host Machine
page, you will be forwarded to the Host SSH Connection
page, which lets you use SSH for the connection to the
server instance. This facility enables you to establish a
secure connection to remotely manage and configure the
server instance. You must fill in the hostname and user
name of the account that will be utilized to log in to the
server for administration and configuration activities. If
you do not enter the optional SSH Key for use with the
server, then you will be demanded the password when the
connection is created by MySQL Workbench.
This connection is made to authorize remote adminis-
tration and configuration of the MySQL Server itself. It is
not similar to the connection used to connect to a server
for general database manipulation. You must need an SSH
connection type when administering a remote server if
you want to start or stop the server configuration process.
Apart from that, other administrative functions do not
require an SSH connection.
Windows Management
If a Windows server is utilized, then setting the Windows
configuration parameters is obligatory. Windows manage-
ment searches for a user account with the required privi-
leges to query the system status, and to control services.
78 ◾ Mastering MySqul for Web
Review Settings
The modified settings may be evaluated, which also
includes the default values. Choose the Change Parameters
check box if the MySQL Config File section will be edited
and then click Next to continue.
Specify Commands
Optionally prop the commands required to start, stop, and
examine the status of the running MySQL server instance.
Commands can be edited if needed, but the defaults are
satisfactory in most cases. The defaults depend on the cho-
sen options on the Operating System page of the wizard.
Click Next to continue.
Complete Setup
In this final step, you are required to name the MySQL
server instance. This name will be used throughout
MySQL Workbench as a reference to this MySQL connec-
tion. After setting an appropriate name, click Finish to save
the instance.
8 https://dev.mysql.com/doc/refman/8.0/en/connection-options.html, MySQL
84 ◾ Mastering MySqul for Web
Protocol Applicable
Value Transport Protocol Used Platforms
TCP TCP/IP transport to local or remote All
server
SOCKET Unix socket-file transport to local Unix
server
PIPE Named-pipe transport to local Windows
server
MEMORY Shared-memory transport to local Windows
server
• shared-memory-base-name=name: On Windows,
the shared-memory name is applied when you
need to move shared memory to a local server. The
default value is MYSQL. The shared-memory name
is case-sensitive. This option would work only if the
9 https://dev.mysql.com/doc/refman/8.0/en/connection-options.html, MySQL
86 ◾ Mastering MySqul for Web
10 https://dev.mysql.com/doc/refman/8.0/en/connection-options.html, MySQL
Getting Started with MySQL ◾ 87
11 https://dev.mysql.com/doc/refman/8.0/en/connection-options.html, MySQL
92 ◾ Mastering MySqul for Web
12 https://dev.mysql.com/doc/refman/8.0/en/connection-options.html, MySQL
Getting Started with MySQL ◾ 93
MySQL --tls-ciphersuites="suite1:suite2:
suite3"
mysql --tls-version="TLSv1.1,TLSv1.2"
The protocols that can be named for this option depend on the
SSL library applied to compile MySQL. Permitted protocols
should be selected such as not to leave free spaces in the list.
To illustrate, these values do not have any space in between:
• --tls-version=“TLSv1,TLSv1.1,TLSv1.2,TLSv1.3”
• --tls-version=“TLSv1.1,TLSv1.2,TLSv1.3”
• --tls-version=“TLSv1.2,TLSv1.3”
• --tls-version=“TLSv1.3”
• --tls-version=“TLSv1,TLSv1.2”
• --tls-version=“TLSv1.1,TLSv1.3”
13 https://dev.mysql.com/doc/refman/8.0/en/connection-options.html, MySQL
Chapter 3
Data Modeling
IN THIS CHAPTER
DOI: 10.1201/9781003229629-3 97
98 ◾ Mastering MySqul for Web
ssh example@your_server_ip
MySQL -u example -p
With that, you are now ready to go with the rest of this
chapter and begin learning about how to create and man-
age tables in MySQL.
Start by double-clicking the Add table icon in the
Physical Schemas section of the Model Overview tab adds
a table with the default name of table1. If a table with this
name already exists, the new table is named table2.
Creating a new table automatically opens the table
editor that is located at the bottom of the application.
Data Modeling ◾ 103
1 https://dev.mysql.com/doc/workbench/en/wb-tables-physical-schemata.
html, MySQL
104 ◾ Mastering MySqul for Web
Cut 'table_name'
Copy 'table_name'
Paste
Edit 'table_name'
Edit 'table_name' in New Tab…
Copy SQL to Clipboard
Copy Column Names to Clipboard
Copy Inserts to Clipboard
Copy Insert Template to Clipboard
Delete 'table_name'
Remove Figure 'table_name'
2 https://dev.mysql.com/doc/workbench/en/wb-using-table-tool.html, MySQL
106 ◾ Mastering MySqul for Web
Primary Keys
A requisite part of establishing relationships between two
entities or two parts of data is being able to certify the data
108 ◾ Mastering MySqul for Web
Foreign Keys
A Foreign Key enables us to link and associate a row in one
table to a row in another table. This is achieved by setting
a column in one table as a Foreign Key and having that
column reference another table’s Primary Key column.
Creating this relationship is possible through using the
REFERENCES keyword in this form: FOREIGN KEY (fk_
col_name) REFERENCES target_table_name (pk_col_
name); In general terms, you can think of this reference as
establishing a connection between rows in different tables.
It is best to try and look through an example.
Data Modeling ◾ 109
3 https://dev.mysql.com/doc/workbench/en/wb-connection-properties.html,
MySQL
Data Modeling ◾ 115
STORING ROUTINES
4 https://dev.mysql.com/doc/workbench/en/wb-routines.html, MySQL
120 ◾ Mastering MySqul for Web
• Rename
• Cut ‘routine_name’
• Copy ‘routine_name’
• Paste
• Edit Routine
• Edit in New Window
• Copy SQL to Clipboard
• Delete ‘routine_name’
• ALL
• CREATE
• DROP
• GRANT OPTION
124 ◾ Mastering MySqul for Web
• REFERENCES
• ALTER
• DELETE
• INDEX
• INSERT
• SELECT
• UPDATE
• TRIGGER
• Rename
• Cut ‘routine_group_name’
Data Modeling ◾ 125
• Copy ‘routine_group_name’
• Edit Routine
• Edit in New Window
• Copy SQL to Clipboard
• Delete ‘routine_group_name’
Here, the cut and paste items are useful for copying rou-
tine groups between different schemata. Deleting a routine
group from the MySQL Model page removes the group but
does not remove any routines holding in that group.
Any routine groups included in the Physical Schemata
also pop up in the Catalog platform on the right side of the
application. They may be attached to an EER diagram by
dragging and dropping them from this platform.
5 https://dev.mysql.com/doc/workbench/en/wb-routines.html#wb-routine-
editor, MySQL
128 ◾ Mastering MySqul for Web
SQL Development
IN THIS CHAPTER
• Tables
• Views
• Stored procedure
• User-defined functions
• User-defined data types
• DDL triggers
• XML schema collections
• Schemas
• Full-text catalogs
• Select objects
144 ◾ Mastering MySqul for Web
• Script to file
• Script to a new query window
• Script to clipboard
• Advanced configurations
[mysqld]
log-error=error.log
1 https://dev.mysql.com/doc/refman/8.0/en/mysqld-safe.html, MySQL
148 ◾ Mastering MySqul for Web
2 https://dev.mysql.com/doc/refman/8.0/en/mysqld-safe.html, MySQL
150 ◾ Mastering MySqul for Web
HYPHEN, hyphen
YY-MM-DD h:mm:ss format, as in mysqld_safe
for MySQL 5.6.
LEGACY, legacy
YYMMDD hh:mm:ss format, as in mysqld_safe
prior to MySQL 5.6.
• malloc-lib=[lib_name]: The name of the library that
is used for memory allocation instead of the system
malloc() library. The option value must be one of the
directories/usr/lib, /usr/lib64, /usr/lib/i386-linux-
gnu, or/usr/lib/x86_64-linux-gnu.
The --malloc-lib option works by modifying the
LD_PRELOAD environment value to impact link-
ing and enable the memory-allocation library. If the
option is given as --malloc-lib=/path/to/some/library,
that full path should be added to the beginning of the
LD_PRELOAD value. If the full path points to a non-
existent or unavailable file, mysqld_safe strikes with
an error. For cases where mysqld_safe includes a path-
name to LD_PRELOAD, it adds the path to the begin-
ning of any existing value the variable already has.
• mysqld=prog_name: It stands for the name of the
server program that you need to start. This option is
applied if you use the MySQL binary distribution but
hold the data directory outside of the binary distribu-
tion. If mysqld_safe cannot find the server, use the
-ledir option to look for the pathname to the direc-
tory where the server is placed. However, this option
can be accepted only on the command line, not in
SQL Development ◾ 151
cd mysql_installation_directory
bin/mysqld_safe &
• MySQL.server start
• MySQL.server stop
cp MySQL.server /etc/init.d/mysql
chmod +x /etc/init.d/mysql
[mysqld]
datadir=/usr/local/mysql/var
socket=/var/tmp/mysql.sock
port=3306
user=mysql
3 https://dev.mysql.com/doc/refman/8.0/en/mysql-server.html, MySQL
SQL Development ◾ 159
mysqld_multi [options]
{start|stop|reload|report} [GNR[,GNR] …]
mysqld_multi -example
[mysqld38]
mysqld = mysqld-debug
ledir = /opt/local/mysql/libexec
162 ◾ Mastering MySqul for Web
4 https://dev.mysql.com/doc/refman/8.0/en/mysqld-multi.html, MySQL
164 ◾ Mastering MySqul for Web
5 https://dev.mysql.com/doc/refman/8.0/en/mysqld-multi.html, MySQL
166 ◾ Mastering MySqul for Web
6 https://dev.mysql.com/doc/refman/8.0/en/mysql-command-options.html,
MySQL
168 ◾ Mastering MySqul for Web
MySQL db_name
Or:
MySQL --user=user_name --password db_name
EDITING DATA
Columns Tab
You can use the Columns subtab to present and edit all the
column data for a table. With this subtab, you can add, can-
cel, or change columns. You can also apply the Columns
subtab to alter column properties such as name, data type,
and default value. Just right-click a row under the Column
Name to review a pop-up menu with the following list:8
7 https://dev.mysql.com/doc/workbench/en/wb-table-editor-main-window.
html, MySQL
8 https://dev.mysql.com/doc/workbench/en/wb-table-editor-main-window.
html, MySQL
182 ◾ Mastering MySqul for Web
9 https://dev.mysql.com/doc/workbench/en/wb-table-editor-main-window.
html, MySQL
SQL Development ◾ 183
which you should click the Indexes tab, then in the Index
Columns panel, you have to set the proper order of the pri-
mary keys.
Indexes Tab
The Indexes subtab holds all of the index data for your table.
You can use this subtab to add, drop, and edit indexes. All
indexes for a table are displayed by index name. Simply
click an index name to see all the Index Columns section
with information about the selected index. In addition, in
the same section, you should be able to modify the stor-
age type, key block size, parser, and visibility of the index.
Index comments, when included, apply to the selected
index only. To add an index, click the last row in the index
list and insert a name for the index as well as the index
type from the list. You should also choose the column or
multiple columns that you need to index by checking the
column name in the Index Columns list. You can delete a
column from the index just by removing the checkmark
from the applicable column.
In order to specify the order of a column within an
index, pick ASC or DESC from the Order column. Next,
create an index prefix by defining a numeric value under
the Length column. You will not be able to enter a prefix
value for fields that have a data type that does not support
prefixing.
If you want to render a secondary index that is invis-
ible to the optimizer, just deselect the Visible option. By
default, all indexes are made visible, so this feature has to
be supported by the active server; otherwise, the Visible
SQL Development ◾ 185
• RESTRICT
• CASCADE
• SET NULL
• NO ACTION
10 https://dev.mysql.com/doc/workbench/en/wb-table-editor-foreign-keys-
tab.html, MySQL
186 ◾ Mastering MySqul for Web
Triggers Tab
The Triggers subtab creates a work platform that enables
you to generate new triggers or edit existing triggers. All
triggers are placed within a tree structure by section, such
as BEFORE INSERT and AFTER INSERT.
To add a new trigger, click the [+] icon next to the trig-
ger section. To delete a trigger, simply click the assigned [-]
icon. These icons become visible by layering over a trigger
or trigger section. When finished click Apply to set your
changes.
Partitioning Tab
To activate partitioning for your table, check the Enable
Partitioning check box. The Partition By pop-up menu
presents the types of partitions you can choose from:11
• HASH
• LINEAR HASH
• KEY
• LINEAR KEY
• RANGE
• LIST
11 https://dev.mysql.com/doc/workbench/en/wb-table-editor-partitioning-tab.
html, MySQL
SQL Development ◾ 187
• Partition
• Values
• Data Directory
• Index Directory
• Min Rows
• Max Rows
• Comment
• Options Tab
• General Options
• Row Options
12 https://dev.mysql.com/doc/workbench/en/wb-table-editor-partitioning-tab.
html, MySQL
13 https://dev.mysql.com/doc/workbench/en/wb-table-editor-partitioning-tab.
html, MySQL
188 ◾ Mastering MySqul for Web
• Storage Options
• Merge Table Options
Server
Administration
IN THIS CHAPTER
INSTANCES
The MySQL server, mysqld, has many command options
and system variables that can should be applied at startup
to modify its operation. To define the default command
option and system variable values used by the server, insert
the following command:
1 https://dev.mysql.com/doc/refman/8.0/en/multiple-servers.html, MySQL
Server Administration ◾ 203
--general_log_file=file_name
--log-bin[=file_name]
--slow_query_log_file=file_name
--log-error[=file_name]
--tmpdir=dir_name
[mysqld]
datadir = C:/mydata1
port = 4405
[mysqld]
datadir = C:/mydata2
port = 4406
2 https://dev.mysql.com/doc/refman/8.0/en/multiple-windows-command-
line-servers.html, MySQL
3 https://dev.mysql.com/doc/refman/8.0/en/multiple-windows-command-
line-servers.html, MySQL
Server Administration ◾ 207
4 https://dev.mysql.com/doc/refman/8.0/en/multiple-windows-services.html,
MySQL
Server Administration ◾ 209
enable-named-pipe
socket = mypipe1
# options for mysqld2 service
[mysqld2]
basedir = C:/mysql-8.0.25
port = 3308
enable-named-pipe
socket = mypipe2
In order to install the services as follows, you have
to use the full server path names to ensure that
Windows registers the correct executable program
for each service:5
C:\> C:\mysql-5.7.9\bin\mysqld
--install mysqld1
C:\> C:\mysql-8.0.25\bin\mysqld
--install mysqld2
5 https://dev.mysql.com/doc/refman/8.0/en/multiple-windows-services.html,
MySQL
210 ◾ Mastering MySqul for Web
[mysqld]
basedir = C:/mysql-5.7.9
port = 3307
enable-named-pipe
socket = mypipe1
[mysqld]
basedir = C:/mysql-8.0.25
port = 3308
enable-named-pipe
socket = mypipe2
shell> MYSQL_UNIX_PORT=/tmp/mysqld-new.sock
shell> MYSQL_TCP_PORT=3307
shell> export MYSQL_UNIX_PORT
MYSQL_TCP_PORT
shell> bin/mysqld --initialize --user=mysql
shell> mysqld_safe --datadir=/path/to/
datadir &
6 https://dev.mysql.com/doc/refman/8.0/en/multiple-windows-services.html,
MySQL
7 https://dev.mysql.com/doc/refman/8.0/en/multiple-windows-services.html,
MySQL
214 ◾ Mastering MySqul for Web
8 https://dev.mysql.com/doc/refman/8.0/en/roles.html, MySQL
Server Administration ◾ 217
defaults to ‘%’. The user and host parts can stay unquoted
unless they hold special characters such as - or %. However,
unlike account names, the user part of role names cannot
be left blank.
In order to assign privileges to the roles, you should
execute GRANT statement using the same syntax as for
assigning privileges to user accounts:9
9 https://dev.mysql.com/doc/refman/8.0/en/roles.html, MySQL
Server Administration ◾ 219
GRANT 'app_developer' TO
'dev1'@'localhost';
GRANT 'app_read' TO 'read_
user1'@'localhost',
'read_user2'@''localhost';
GRANT 'app_read', 'app_write' TO
'rw_user1'@'localhost';
10 https://dev.mysql.com/doc/refman/8.0/en/roles.html, MySQL
220 ◾ Mastering MySqul for Web
[mysqld]
mandatory_roles='role1,role2@
localhost,r3@%.example.com'
11 https://dev.mysql.com/doc/refman/8.0/en/roles.html, MySQL
Server Administration ◾ 221
12 https://dev.mysql.com/doc/refman/8.0/en/account-categories.html, MySQL
222 ◾ Mastering MySqul for Web
At the same time, the variable will show each granted role
without “expanding” it to the privileges the role represents.
To display role privileges as well, you need to include a
USING clause naming the granted roles for which to dis-
play privileges:
Activating Roles
Roles granted to a user account can be treated as active
or inactive within account sessions. If a granted role is
active within a session, its privileges are included as well;
otherwise, they are not. If might want to see for yourself
which roles are active within the current session, use the
CURRENT_ROLE() function for that purpose.
Server Administration ◾ 223
head -n 5 data-dump.sql
MySQL -u root -p
– Output
Query OK, 1 row affected (0.00 sec)
Once you see that, you can exit the MySQL shell by press-
ing CTRL+D. From the normal command line, you can
import the dump file with the following command:
Backing Up and
Restoring MySQL Data
IN THIS CHAPTER
1 https://dev.mysql.com/doc/mysql-backup-excerpt/8.0/en/backup-types.
html, MySQL
Backing Up and Restoring MySQL Data ◾ 237
Snapshot Backups
Some file system executions enable “snapshots” function
to be activated. These provide logical copies of the file
system at any point in time, without having to request a
physical copy of the entire file system. The backup execu-
tion may use the copy-on-write method so that only a sec-
tion of the file system modified after the snapshot time
needs to be copied. However, MySQL itself does not have
the capacity for taking file system snapshots. It is only
available through third-party solutions such as Veritas,
LVM, or ZFS.
that, the dump becomes lock-free and does not affect reads
and writes on the tables.
It was mentioned earlier that the tables to back up are
InnoDB tables, so --single-transaction uses a consistent
read and guarantees that data seen by mysqldump does not
change. But if the backup operation includes nontransac-
tional tables, consistency requires that they do not change
during the backup. For instance, for the MyISAM tables
in the MySQL database, there must be no administrative
modifications to MySQL accounts during the backup.
Even though full backups are necessary, it is not always
convenient to process them. They produce large backup
files and take a substantial amount of time to gener-
ate. They are not preferred in the sense that each set of
full backup includes all data, even that part that has not
changed since the previous full backup. It is therefore more
efficient to complete an initial full backup, and then to
make incremental backups. The incremental backups are
much smaller and take fewer resources to produce. But
at the same time, during the recovery period, you cannot
restore your data just by reloading the full backup. You
must also process the incremental backups to recover the
incremental changes.
In order to make incremental backups, we need to save
the incremental changes. In MySQL, these changes are
demonstrated in the binary log, so the MySQL server should
always be started with the --log-bin option to enable that
log. With binary logging enabled, the server copies each
data change into a file while it updates data. And each time
it restarts, the MySQL server creates a new binary log file
using the next number in the sequence. While the server
246 ◾ Mastering MySqul for Web
2 https://dev.mysql.com/doc/refman/5.6/en/backup-strategy-summary.html,
MySQL
Backing Up and Restoring MySQL Data ◾ 249
3 https://dev.mysql.com/doc/mysql-backup-excerpt/8.0/en/backup-types.
html, MySQL
250 ◾ Mastering MySqul for Web
The .txt files containing table data are scripted by the server,
so they belong to the system account used for running
the server. The server uses SELECT … INTO OUTFILE
to write the files, so you should have the FILE privilege to
complete this operation, and an error occurs if a given .txt
file already exists.
The server also sends the CREATE definitions for dumped
tables to mysqldump, which writes them to .sql files. These
files, therefore, belong to the user who executes mysqldump.
It is most suitable that --tab is used only for dumping a
local server. If you apply it with a remote server, the --tab
directory should exist on both the local and remote hosts,
and the .txt files are written by the server in the remote
directory (on the server host), while the .sql files are written
by mysqldump in the local directory (on the client host).
For mysqldump --tab, the server by default scripts table
data to .txt files one line per row with tabs between column
values, no quotation marks around column values, and
newline as the line terminator.
4 https://dev.mysql.com/doc/mysql-backup-excerpt/8.0/en/mysqldump-
delimited-text.html, MySQL
Backing Up and Restoring MySQL Data ◾ 253
--fields-enclosed-by=0x22
5 https://dev.mysql.com/doc/mysql-backup-excerpt/8.0/en/mysqldump-
delimited-text.html, MySQL
254 ◾ Mastering MySqul for Web
6 https://dev.mysql.com/doc/mysql-backup-excerpt/8.0/en/mysqldump-
delimited-text.html, MySQL
Backing Up and Restoring MySQL Data ◾ 255
And only after that specify the database name when you
load the dump file:
7 https://dev.mysql.com/doc/mysql-backup-excerpt/8.0/en/reloading-delim-
ited-text-dumps.html, MySQL
258 ◾ Mastering MySqul for Web
8 https://dev.mysql.com/doc/refman/5.6/en/point-in-time-recovery.html,
MySQL
Backing Up and Restoring MySQL Data ◾ 261
In case you have more than one binary log to use on the
MySQL server, the safest option would be to process them
all using a single connection to the server. To illustrate an
example of how the unsafe option looks like:
9 https://dev.mysql.com/doc/refman/5.6/en/point-in-time-recovery.html,
MySQL
Backing Up and Restoring MySQL Data ◾ 263
The next step would be to find the exact binary log event
position referring to the point in time up to which you need
to restore your database. In addition, if you know the rough
time where the table deletion took place, you can find the log
position by checking the log contents around that time using
the mysqlbinlog utility. Make sure to apply the --start-Date-
Time and --stop-datetime options to identify a short time
period around the event, and then look for it in the output.
After that, apply the events in binary log file to the server,
starting with the log position you found in step 1 (assume it
is 1006) and ending at the position you have found in step 2
that is before your point-in-time of interest (which is 1868):10
shell> mysqlbinlog --start-position=1006
--stop-position=1868 /var/lib/mysql/
bin.123456 \
| mysql -u root -p
10 https://dev.mysql.com/doc/refman/5.6/en/point-in-time-recovery.html,
MySQL
264 ◾ Mastering MySqul for Web
TABLE tableName
INTO OUTFILE 'path/outputFile.csv'
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
ESCAPED BY "
LINES TERMINATED BY 'n';
SELECT columnName, …
FROM tableName
WHERE columnName = 'value';
(SELECT 'columnHeading', …)
UNION
(SELECT column, …
FROM tableName
INTO OUTFILE 'path-to-file/
outputFile.csv''
FIELDS ENCLOSED BY '"'
TERMINATED BY ','
ESCAPED BY '"'
LINES TERMINATED BY 'n')
268 ◾ Mastering MySqul for Web
When you need to just copy the schema but not the data,
you can use –no-data option while creating the dump:
That is pretty much it. From the clients’ side, they usu-
ally emphasize which MySQL GUI to use. The lighter and
more user-friendly the GUI is, the faster and easier their
data management activities will be. Some of the most
popular MySQL GUIs are MySQL WorkBench, SequelPro,
DBVisualizer, and the Navicat DB Admin Tool. Some are
free, while some are commercial, some run exclusively for
macOS, and some are compatible with major operating
systems. You should be able to choose the GUI depending
on your needs and requirements.
To make it clear, MySQL is not the only relative database
on the market, but it is considered to be one of the most
popular ones scoring great points at critical parameters
like the number of downloads, mentions in search results,
and frequency of technical discussions on Internet forums.
In addition, the fact that many major tech giants rely on
Appraisal ◾ 277
A B
Active roles, 215 Backup, 234
Add Routine Group icon, 124 by copying table files, 241
ADO.NET interfaces, 19 delimited-text file backups,
Advanced Scripting 242
Options, 144 full, 239
MySQL.server, 155–159 incremental, 239
mysqld_multi, 159–164 by enabling binary log,
mysql_upgrade, 164–177 242
Advanced tab, 74 local, 238–239
Advantages of MySQL, 25–30 logical, 234–237
Already Downloaded with mysqldump, 241
option, 60 with MySQL Enterprise
ALTER FUNCTION statement, Edition, 240–241
128 offline, 237–238
ALTER PROCEDURE statement, online, 237–238
128 physical, 234–237
ALTER ROUTINE privilege, regular schedule of,
129–130 244–254
Application development of remote, 238–239
MySQL, 11 restoring from backup file,
Application expert, 196 255
Application servers, 191–192 point-in-time recovery
Availability of MySQL, 11 using binary log,
Axmark, David, 2 259–264
285
286 ◾ Index