Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- config=".my.cnf.$$"
- command=".mysql.$$"
- rootpass=""
- echo_n=
- echo_c=
- set_echo_compat() {
- case `echo "testing\c"`,`echo -n testing` in
- *c*,-n*) echo_n= echo_c= ;;
- *c*,*) echo_n=-n echo_c= ;;
- *) echo_n= echo_c='\c' ;;
- esac
- }
- prepare() {
- touch $config $command
- chmod 600 $config $command
- }
- do_query() {
- echo $1 >$command
- mysql --defaults-file=$config <$command
- return $?
- }
- make_config() {
- echo "# mysql_secure_installation config file" >$config
- echo "[mysql]" >>$config
- echo "user=root" >>$config
- echo "password=$rootpass" >>$config
- }
- set_root_password() {
- password1=${mysqlserver_password}
- do_query "UPDATE mysql.user SET Password=PASSWORD('$password1') WHERE User='root';"
- if [ $? -eq 0 ]; then
- echo "Password updated successfully!"
- echo "Reloading privilege tables.."
- if ! reload_privilege_tables; then
- exit 1
- fi
- echo
- rootpass=$password1
- make_config
- else
- echo "Password update failed!"
- exit 1
- fi
- return 0
- }
- remove_anonymous_users() {
- do_query "DELETE FROM mysql.user WHERE User='';"
- if [ $? -eq 0 ]; then
- echo " ... Success!"
- else
- echo " ... Failed!"
- exit 1
- fi
- return 0
- }
- remove_remote_root() {
- do_query "DELETE FROM mysql.user WHERE User='root' AND Host!='localhost';"
- if [ $? -eq 0 ]; then
- echo " ... Success!"
- else
- echo " ... Failed!"
- fi
- }
- remove_test_database() {
- echo " - Dropping test database..."
- do_query "DROP DATABASE test;"
- if [ $? -eq 0 ]; then
- echo " ... Success!"
- else
- echo " ... Failed! Not critical, keep moving..."
- fi
- echo " - Removing privileges on test database..."
- do_query "DELETE FROM mysql.db WHERE Db='test' OR Db='test\\_%'"
- if [ $? -eq 0 ]; then
- echo " ... Success!"
- else
- echo " ... Failed! Not critical, keep moving..."
- fi
- return 0
- }
- reload_privilege_tables() {
- do_query "FLUSH PRIVILEGES;"
- if [ $? -eq 0 ]; then
- echo " ... Success!"
- return 0
- else
- echo " ... Failed!"
- return 1
- fi
- }
- interrupt() {
- echo
- echo "Aborting!"
- echo
- cleanup
- exit 1
- }
- cleanup() {
- echo "Cleaning up..."
- rm -f $config $command
- }
- # The actual script starts here
- DEBIAN_FRONTEND=noninteractive apt-get -y -qq install mysql-server
- mysql_install_db
- prepare
- set_echo_compat
- password='${mysqlserver_password}'
- set_root_password
- remove_anonymous_users
- remove_remote_root
- remove_test_database
- reload_privilege_tables
- cleanup
Add Comment
Please, Sign In to add comment