User:Tsu2/MySQL and Mariadb

Jump to: navigation, search

The HereDoc

A simple real world, easy to understand use is described next.
More extensive information and references about the HereDoc follows afterwards

MySQL/Mariadb Secure Setup

Today's MySQL and Mariadb come with a utility that walks the User through a variety of steps to set up how it's secured called "mysql_secure_installation" but answering those questions the same way all the time can be tedious.

The following which can be run in a console or as an executable script invokes and runs MySQL secure installation answering the questions quickly and automatically in about a second. BTW - this is a very simple way to auto-answer any interactive console app.

mysql_secure_installation <<EOF


If you were to run the above without the automatic answers, the interactive questions(abbreviated) would be

Enter current password for root (enter for none): 
Set root password? [Y/n] 
New password:
Re-enter new password:
Remove anonymous users? [Y/n] 
Disallow root login remotely? [Y/n] 
Remove test database and access to it? [Y/n] 
Reload privilege tables now? [Y/n]

NOTE: The blank first line in the automatic answers and the first question... which is to enter null if there is no current passowrd.

If you ever wanted to reset these security settings, for instance to grant the ability to remotely administer this MySQL/Mariadb instance, you only have to re-run this script with the modified setting(Don't forget to provide the existing MySQL/Mariadb root password in the first line which is blank by default).

More Info

The HereDoc is a powerful way to pass a block of string data to a command.
The brief, but probably insufficient description provided by Wikipedia

The very good extensive description with multiple examples for a variety of uses by The Linux Documentation Project

A good number of examples in the above TLDP reference illustrate the power of directing a text stream which might even be code to a variable instead of command, which can then be operated on within another command or even another HereDoc stream.

The above reference (TLDP) describes the following uses
- Broadcast a message to everyone logged on the LAN using wall
- The very commonly used "cat" script to add multi-line text to an existing document
- Various options to suppress whitespace and use replaceable parameters
- Disabling replaceable parameters
- Upload a "file pair" to a remote location
- Script generation (script that generates another script)
- Inserting documentation
- Automating a sequence of commands or scripts like connecting to, and uploading and downloading files

A Thank You to jetchisel for identifying this technique as a HereDoc which then led to opening to vastly more ways to use than the simple use illustrated by the MySQL Security script.