centos7 shell脚本一键安装mysql8与修改密码

rpm包可以自行下载,或者使用yum安装后的缓存文件

rpm -qa|grep mariadb >/dev/null
    if [ $? -ne 1 ]; then
				#mysql安装
				#卸载centos自带mariadb
				rpm -e --nodeps mariadb-libs
				rpm -ivh mysql-community-common-8.0.20-1.el7.x86_64.rpm    
				rpm -ivh mysql-community-libs-8.0.20-1.el7.x86_64.rpm
				rpm -ivh mysql-community-libs-compat-8.0.20-1.el7.x86_64.rpm
				rpm -ivh mysql-community-client-8.0.20-1.el7.x86_64.rpm
				rpm -ivh mysql-community-server-8.0.20-1.el7.x86_64.rpm
				systemctl restart  mysqld
				#数据库修改密码
				mypassw=`grep "temporary password" /var/log/mysqld.log|awk -F' ' "{print $NF}"|awk '{print $NF}'`
				/usr/bin/mysqladmin -uroot -p${mypassw} password "Waner123456!"
				#数据库准备指令
				/usr/bin/mysql -uroot -pWaner123456! << EOF
				    create database stwk;
					use mysql;
					update user set host = '%' where user ='root';
				    /*设置编码*/
				    set character_set_client = utf8;
				    set character_set_server = utf8;
				    set character_set_connection = utf8;
				    set character_set_database = utf8;
				    set character_set_results = utf8;
				    set collation_connection = utf8_general_ci;
				    set collation_database = utf8_general_ci;
				    set collation_server = utf8_general_ci;
				    /*创建用户*/
				    CREATE USER 'test'@'%' IDENTIFIED BY 'Waner123456!';
				    GRANT ALL ON *.* TO 'test'@'%';
				    ALTER USER 'test'@'%' IDENTIFIED BY 'Waner123456!' PASSWORD EXPIRE NEVER; 
				    ALTER USER 'test'@'%' IDENTIFIED WITH mysql_native_password BY 'Waner123456!';
				    FLUSH PRIVILEGES;
/*注意:EOF要靠边并删除以上注释*/
EOF
                #数据库导入,需要在上一步创建数据库
                mysql -uroot -pWaner123456! stwk<stwk.sql
    fi

你可能感兴趣的:(mysql,centos)