新建执行脚本:mysql_install.sh,并添加执行权限
#!/bin/bash
#zhouyihua V0.1 2021.07.08
#For centos 7.0 &8.0
#v 0.2
# add DNS
echo "---------- Add DNS --------"
echo "nameserver 8.8.8.8" >>/etc/resolv.conf
#stop firewall
systemctl stop firewalld
if [ $? -ne 0 ]then
echo "Firewall stop failed"
else
echo "Firewall stop success"
fi
#download mysql yum source
wget https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
#install mysql yum source
rpm -ivh mysql80-community-release-el7-3.noarch.rpm
#enable 5.7
#sed -i '21s/enabled=0/enabled=1/g' /etc/yum.repos.d/mysql-community.repo
#sed -i '21s/enabled=0/enabled=1/g' /etc/yum.repos.d/mysql-community.repo
#disable 8.0
#sed -i '28s/enabled=1/enabled=0/g' /etc/yum.repos.d/mysql-community.repo
#install mysql
yum -y module disable mysql
yum -y remove mariadb-libs
yum install -y mysql-community-server
#config mysql
echo "----------- Config my.cnf ----------"
sed -i '/\[mysqld\]/a\lower_case_table_names=1' /etc/my.cnf
sed -i '/\[mysqld\]/a\skip-grant-tables' /etc/my.cnf
#start mysql
echo "---------- Starting mysql service ----------"
systemctl start mysqld.service
mysql -uroot -p123456 <<EOF
use mysql
update user set authentication_string='' where user='root'
flush privileges
ALTER user 'root'@'localhost' IDENTIFIED BY '?CZJh8JWxvH';
EOF
echo "---------- Annotation skip grant tables in my.cnf ----------"
sed -i 's/skip-grant-tables/\#skip-grant-tables/g' /etc/my.cnf
echo "---------- Restart mysql service ----------"
systemctl restart mysqld.service
echo "---------- Grant all privileges ----------"
mysql -uroot -p'Oracle1234!@#$' --connect-expired-password -e "alter user 'root'@'localhost' identified by 'Oracle1234!@#$'"
mysql -u root -p'Oracle1234!@#$' <<EOF
use mysql
CREATE USER 'maxkey'@'%' IDENTIFIED BY 'Oracle1234!@#$'
GRANT ALL ON *.* TO 'maxkey'@'%'
EOF
参考:
先设置grant_skip_tables
1. use mysql
2. update user set authentication_string='' where user='root' 如果这个字段有值,先置为空
3. flush privileges 刷新权限表
4.ALTER user 'root'@'localhost' IDENTIFIED BY 'Tianya1234' 修改root 密码
参考:https://blog.csdn.net/qq_27820551/article/details/101488430
在Shell中执行mysql的脚本,这里介绍比较容易使用的一种方法首先写好sql的脚本,后缀为.sql,比如
sql_file.sql:内容如下
#这是SQL的脚本
create table if not exists test_sql(id int(10),name varchar(20))
insert into test_sql values(1,'正餐')
select * from test_sql
很简单的创建、插入、查询
之后shell的脚本,内容如下
#!/bin/bash #这是执行SQL的脚本,传入数据库表,和SQL文件名即可
name=$1
file_name=$2
cd /opt/lampp/bin ./mysql -uroot -p123456 <<EOF
use ${name}
source ${file_name}
EOF
./mysql -uroot -p123456是进入mysql的命令,前面的路径可以改成自己的绝对路径
<<EOF EOF中间可以写出任意的mysql脚本
其中,如果要执行某个.sql的命令,直接用 source 脚本.sql 即可执行该脚本上面的命令,这里使用的是source sql_file.sql
此外,如果在mysql中需要执行shell脚本
可以在EOF中 ./mysql -uroot -p123456<<EOF \! sh shell.sh EOF
前面加上 !\ +linux中执行的shell命令即可
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)