Centos7 通过shell来一键操作mysql,实现修改密码及创建wordpress数据库操作

Centos7 通过shell来一键操作mysql,实现修改密码及创建wordpress数据库操作

平台:centos7 + mysql5.7

注:mysql以“;”标识结束,所有命令结束后必须加“;”

一.获取msyql初始密码,两种方式:

MYSQLPASSWD=`grep "temporary password" /var/log/mysqld.log|awk -F' ' "{print $NF}"|awk '{print $NF}'`
echo "MYSQL初始密码:  ${MYSQLPASSWD}"
PASSWD=$(grep 'temporary password' /var/log/mysqld.log )
#mysql默认密码为12位,从右向左0-12位
MYSQLPASSWD=${PASSWD:0-12}
echo "MYSQL初始密码:  ${MYSQLPASSWD}"

二.通过获取到的初始密码,修改mysql密码

#使用mysqladmin工具进行密码修改(默认有密码复杂度:大小写、数字、特殊符号)
mysqladmin -uroot -p${MYSQLPASSWD} password "Aa123456789#"

三.修改密码安全规则,方便后期修改密码

#全局设置 密码安全等级为低
set global validate_password_policy=LOW;
#全局设置 密码长度,最少为4位
set global validate_password_length=4;

四.创建数据库名“wordpress”,并允许root远程

create database wordpress;
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;

六.创建用户名:test,使用户只可远程操作wordpress数据库

CREATE USER 'test'@'%' IDENTIFIED BY '123456';
GRANT ALL ON wordpress.* TO 'test'@'%';
ALTER USER 'test'@'%' IDENTIFIED BY '123456' PASSWORD EXPIRE NEVER; 
ALTER USER 'test'@'%' IDENTIFIED WITH mysql_native_password BY '123456';

七.完成所有操作,必须刷新mysql权限

flush privilege;

将所有命令合并,实现一键shell

#!/bin/bash
PASSWD=$(grep 'temporary password' /var/log/mysqld.log )
MYSQLPASSWD=${PASSWD:0-12}
echo MYSQLPASSWD
mysqladmin -uroot -p${MYSQLPASSWD} password "Aa123456789#"
mysql -uroot -pAa123456789# << EOF
	/*密码规则*/
	set global validate_password_policy=LOW;
	set global validate_password_length=4;
	/*创建数据库*/
	create database wordpress;
	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 '123456';
	GRANT ALL ON wordpress.* TO 'test'@'%';
	ALTER USER 'test'@'%' IDENTIFIED BY '123456' PASSWORD EXPIRE NEVER; 
	ALTER USER 'test'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
/*注意:EOF要靠边*/
EOF
echo "默认账号root,密码Aa123456789#"
echo "数据库:wordpress,账号:test,密码:123456"

good good study, day day up!
# #
首页      Linux      Centos7 通过shell来一键操作mysql,实现修改密码及创建wordpress数据库操作

发表评论

textsms
account_circle
email

Centos7 通过shell来一键操作mysql,实现修改密码及创建wordpress数据库操作
平台:centos7 + mysql5.7 注:mysql以“;”标识结束,所有命令结束后必须加“;” 一.获取msyql初始密码,两种方式: MYSQLPASSWD=`grep "temporary password" /var/l…
扫描二维码继续阅读
2021-03-05