In the process of MySQL database deployment, after installing the MySQL software and configuring the MY.CNF configuration file, you need to create a MySQL instance.
MySQL 5.5 Instance creation is complete, default will have the login user name and password is empty user, generally need to modify the root user password, and delete invalid user, then can provide to the app to use.
This process can be done automatically through the shell script instance creation, start, automatically generate passwords, modify the root user password, delete invalid users.
There are several ways to write shell scripts, and the shell scripts I write are as follows:
#!/bin/bash# this scripts used to initial the mysql instance and modify password# created by zhaofx# mysql software directory.readonly symlink= '/usr/local/mysql ' echo "MySQL software installation directory for ${symlink}" # mysql database portreadonly mysql_port=3306echo "MYSQL instance port is ${mysql_port}" # mysql database Directory.readonly mysql_data_dir= "/data/mysql_${mysql_port}" echo "MYSQL instance data file directory is ${mysql_data_dir}" echo "Ready to create MySQL instance" cd $SYMLINK./scripts/mysql_install_db --user=mysql --basedir=/usr/local/ mysql --datadir=${mysql_data_dir}/dataecho "Start the DB instance, modify the root user password, delete the invalid user" echo "" cp /etc/ my.cnf /data/mysql_${mysql_port}/my.cnfecho "Startup mysql service:localhost_" ${MYSQL_PORT }/usr/local/mysql/bin/mysqld_safe --defaults-file=/data/mysql_${mysql_port}/my.cnf &echo "" # mysql root password# randomly generates eight-bit passwords as MySQL password for i in {1..10}doa= ' head -c 500 /dev/urandom | tr -dc a-za-z | tr [a-z] [a-z]|head -c 1 ' B= ' head -c 500 /dev/urandom | tr -dc a-z0-9a-z | head -c 6 ' C= ' echo $ Random|cut -c 2 ' echo $A $b$c > /home/mysql/.password_mysqldonereadonly mysql_ root_pass= $A $b$cecho "The root user password for MySQL instance has been generated" su - mysql <<eof/usr/local/mysql/bin/ mysql -uroot -s /data/mysql_${mysql_port}/tmp/mysql.sock -e "Select user,host from mysql.user; " /usr/local/mysql/bin/mysqladmin -uroot -s /data/mysql_${mysql_port}/tmp/mysql.sock password "${mysql_root_pass}" eofecho "MYSQL ROOT user password has been modified" echo "" /usr/ local/mysql/bin/mysql -uroot -p${mysql_root_pass} -s /data/mysql_${mysql_port}/tmp/ mysql.sock -e "delete from mysql.user where user=";d elete from mysql.user where user= ' root ' and host!= ' localhost '; flush privileges; " /usr/local/mysql/bin/mysql -uroot -p${mysql_root_pass} -s /data/mysql_${mysql_port}/tmp/ mysql.sock -e "delete from mysql.db where db= ' Test ' or db= ' test\\_% '; FLUSH PRIVILEGES; " echo "MySQL instance started successfully, initial user environment modified" echo ""/usr/local/mysql/bin/mysql -uroot -p${mysql_root_pass} -S /data/mysql_${MYSQL_PORT}/tmp/mysql.sock -e " show databases;" /usr/local/mysql/bin/mysql -uroot -p${mysql_root_pass} -s /data/mysql_${mysql_port}/tmp/ mysql.sock -e " select user,host from mysql.user;"
Everyone can refer to!
This article is from the "Yumushui column" blog, be sure to keep this source http://yumushui.blog.51cto.com/6300893/1688580
MySQL database automatically generates and modifies random root password scripts