Automate mysql_secure_installation with echo command via a shell script

后端 未结 11 1030
无人共我
无人共我 2020-12-12 12:46

I am trying to automate mysql_secure_installation script with automated response. My code is as follows :

echo \"& y y abc abc y y y y\" | ./usr/bin/mysq         


        
相关标签:
11条回答
  • 2020-12-12 13:19
    1. In Windows OS just search for 'mysql_secure_installation' application usually found in Drive:\MySQL_Install-DIR\bin\
    2. By pressing WindowKey + R just run the command 'Drive:\MySQL_Install-DIR\bin\mysql_secure_installation'

    When you run this command a window will pop up that will walk you through the process of securing your MySQL installation. That's it!

    0 讨论(0)
  • 2020-12-12 13:20

    Since mysql_secure_installation is just a Bash script, just check out the raw source code as shown here. Look for the lines that read, do_query (note that extra space I placed after do_query; need to find queries versus the function) and then you can find these commands.

    UPDATE mysql.user SET Password=PASSWORD('root') WHERE User='root';
    DELETE FROM mysql.user WHERE User='';
    DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1');
    DROP DATABASE IF EXISTS test;
    DELETE FROM mysql.db WHERE Db='test' OR Db='test\\_%';
    FLUSH PRIVILEGES;
    

    Note that for this example, I have the password being set to root but feel free to change that to match your setup needs. Anyway, take that simply pile of MySQL commands and save it in a file named mysql_secure_installation.sql.

    With that done, just run the following command via script to secure the MySQL install:

    mysql -sfu root < "mysql_secure_installation.sql"
    

    The s silences errors and the f forces the commands to continue even if one chokes. The u relates to the username that immediately follows it which—in this case—is clearly root.

    Run that in a deployment script where MySQL is installed initially without a password and you are all set to lock it down without any keyboard interaction.

    PS: This script was put together to secure a MySQL installation on Ubuntu 14.04 which was installed with the export DEBIAN_FRONTEND=noninteractive set and the actual install command being set to sudo -E aptitude install -y --assume-yes -q mysql-server mysql-client. Doing that will cleanly install MySQL on Ubuntu without a password; which is nice for deployment scripts. This mysql -sfu root < "mysql_secure_installation.sql" just locks it all down in seconds after that install happens.

    0 讨论(0)
  • 2020-12-12 13:22

    You could try this:

    echo -e "\ny\ny\nabc\nabc\ny\ny\ny\ny" | ./usr/bin/mysql_secure_installation
    
    0 讨论(0)
  • 2020-12-12 13:26

    I am using simple command to change root password after MySql installation ,But getting the Above error (signal 9 kill)

    (FATAL: Chef::Exceptions::ChildConvergeError: Chef run process terminated by signal 9 (KILL)) Though the command works and password is changed the error is confusing.
    script "change password" do
    interpreter "bash"
    user "root"
    cwd "/tmp"
    code <<-EOH
    #MYSQL
    root_temp_pass=$(grep 'A temporary password' /mysql/log/mysqld.log |tail -1 |awk '{split($0,a,": "); print a[2]}')
    
    #Login as root change password
    mysql -uroot -p"$root_temp_pass" -Be "ALTER USER 'root'@'localhost' IDENTIFIED BY 'Czt732ck#';" --connect-expired-password
    EOH
    end
    
    0 讨论(0)
  • 2020-12-12 13:29

    I just did this on CentOS 6.7 with the following:

    mysql_secure_installation <<EOF
    
    y
    secret
    secret
    y
    y
    y
    y
    EOF
    
    0 讨论(0)
提交回复
热议问题