采用脚本自动填写具有交互式命令的方法

  采用脚本自动填写具有交互式命令的方法

  在我们安装完数据库mariadb之后,启动数据库,然后需要手动执行mysql_secure_installation进行安全设置,比如设置数据库的密码等等,设置完成之后我们才可以使用数据库。然而在我们采用脚本执行数据安装过程中,如何在数据库安全设置的命令执行后,自动输入我们要设置的数据库密码。这里可以提供一个可以对交互式命令自动填写的方法,那就是expect。

1、安装mariadb

  yum install mariadb mariadb-server python2-PyMySQL -y

2、数据库配置

  按如下方法配置数据的配置文件:

  vim /etc/my.cnf.d/openstack.cnf

[mysqld]
bind-address = $CONTROLLER_IP
default-storage-engine = innodb
innodb_file_per_table = on
max_connections = 4096
collation-server = utf8_general_ci
character-set-server = utf8

  启动数据库服务

  systemctl restart mariadb.service

  systemctl status mariadb.service

3、利用脚本执行数据库安全设置

  首先安装expect软件:yum install expect -y

  编写执行脚本,如/root/install/mysqlinstall.sh,脚本内容如下:

#!/usr/bin/expect
spawn mysql_secure_installation
expect "Enter current password for root (enter for none):"
send "\r"
expect "Set root password? "
send "Y\r"
expect "New password: "
send "root\r"
expect "Re-enter new password: "
send "root\r"
expect "Remove anonymous users?"
send "Y\r"
expect "Disallow root login remotely?"
send "n\r"
expect "Remove test database and access to it?"
send "Y\r"
expect "Reload privilege tables now?"
send "Y\r"
interact

  备注:

  Spawn后边填写我们需要执行的命令

  expect "xxx "其中xxx表示提示我们输入字符的关键字

  \r表示不输入任何内容直接回车

  Y\r表示输入Y然后回车

  root\r表示输入root然后回车,这里我设置密码为root

  注意:执行脚本不能用bash执行,用./root/install/mysqlinstall.sh执行脚本

 

转载于:https://www.cnblogs.com/chenli90/p/10539375.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值