Environment: centos7 + mariadb5.5.64.
Let me show the installation info on screen when to run mysql_secure_installation
.
# mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none):
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.
Set root password? [Y/n] y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
... Success!
By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.
Remove anonymous users? [Y/n] y
... Success!
Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] y
... Success!
By default, MariaDB comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n] y
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n] y
... Success!
Cleaning up...
All done! If you've completed all of the above steps, your MariaDB
installation should now be secure.
Thanks for using MariaDB!
I write an automation expect script to install mariadb.
vim secure.exp
set timeout 60
spawn mysql_secure_installation
expect {
"Enter current password for root (enter for none): " {send "r";exp_continue}
"Set root password? [Y/n] " {send "yr";exp_continue}
"New password:" {send "123456r";exp_continue}
"Re-enter new password:" {send "123456r";exp_continue}
"Remove anonymous users? [Y/n]" {send "yr";exp_continue}
"Disallow root login remotely? [Y/n]" {send "yr";exp_continue}
"Remove test database and access to it? [Y/n]" {send "yr";exp_continue}
"Reload privilege tables now? [Y/n]" {send "yr";exp_continue}
}
To execute /usr/bin/expect secure.exp
, i come across the error:
spawn mysql_secure_installation
invalid command name "Y/n"
while executing
"Y/n"
invoked from within
"expect {
"Enter current password for root (enter for none): " {send "r";exp_continue}
"Set root password? [Y/n] " {send "yr";exp..."
(file "secure.exp" line 3)
It is no use to write as below:
set timeout 60
spawn mysql_secure_installation
expect {
"Enter current password for root (enter for none): " {send "r";exp_continue}
"Set root password? \[Y/n] " {send "yr";exp_continue}
"New password:" {send "123456r";exp_continue}
"Re-enter new password:" {send "123456r";exp_continue}
"Remove anonymous users? \[Y/n]" {send "yr";exp_continue}
"Disallow root login remotely? \[Y/n]" {send "yr";exp_continue}
"Remove test database and access to it? \[Y/n]" {send "yr";exp_continue}
"Reload privilege tables now? \[Y/n]" {send "yr";exp_continue}
}
Same error:
invalid command name "Y/n"
while executing
"Y/n"
invoked from within
"expect {
"Enter current password for root (enter for none): " {send "r";exp_continue}
"Set root password? \[Y/n] " {send "yr";exp_conti..."
(file "secure.exp" line 3)
How to fix my exp script then?
2
Answers
Not only are square brackets used for command substitution, but they are also special for glob patterns.
You can either use the
-exact
switch while escaping the square brackets in quotes:Or use braces instead of quotes:
FYI you can have the expect script generated for you by using
autoexpect
:This will generate an expect script called
script.exp
in your present working directory.These scripts wait to receive optional output (
timeout -1
means “no timeout”) and they can tell apart different responses, as it is required byyum install
andmysql_secure_installation
. With#!/bin/expect -f
as shebang, the scripts can be executed, when they were set tochmod +x
.A) To begin with,
mariadb_yum.exp
(requiressu
orsudo
):B) And then
mariadb_sec.exp
(doesn’t requiresudo
):For debugging purposes – or to validate the answer, one can run
expect
with log-levelstrace 4
. This is probably as reputable as a source can get, when it comes to writingexpect
scripts, as it nicely displays what is going on and most importantly, in which order the things happen:Instruction
set exp_internal 1
can be used to get output for the regex-matching.A possible source of confusion might be, where one spawns the processes – as one can spawn several process on various hosts, eg.
ssh
locally and thenyum
andmysql_secure_installation
remotely. Added$spawn_id
to the script; the bottom oneclose
call might be redundant, since it is alreadyEOF
(just to show how tospawn
&close
processes):Conclusion: The
mariadb_sec.exp
script probably could be improved further, eg. when at first sending no password and seeing what happens – then sending the password onERROR 1045
(when a password had already been set previously). It may be save to assume, that one has to set the password when the server just has been installed (except thatyum reinstall
delivers the same result). Just had no blank CentOS container to test all the cases. Unless running in aroot
shell, passing both kinds of passwords into one script would be required to automate this from installation until post-installation.Probably worth noting is that on GCE,
sudo
would not ask for a password; there are indeed minor differences based upon the environment, as these CentOS container images behave differently. In such case (since there is nosu
or container-image detection in place), themariadb_yum.exp
script might get stuck for30
seconds and then continue.The most reputable sources I can offer are the
expect
manual, written by Don Libes @ NIST and the TCL/TK manual forexpect
, along with it’s SourceForge project coincidentally calledexpect
.