zoukankan      html  css  js  c++  java
  • 采用脚本自动填写具有交互式命令的方法

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

      在我们安装完数据库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 "
    "
    expect "Set root password? "
    send "Y
    "
    expect "New password: "
    send "root
    "
    expect "Re-enter new password: "
    send "root
    "
    expect "Remove anonymous users?"
    send "Y
    "
    expect "Disallow root login remotely?"
    send "n
    "
    expect "Remove test database and access to it?"
    send "Y
    "
    expect "Reload privilege tables now?"
    send "Y
    "
    interact

      备注:

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

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

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

      Y 表示输入Y然后回车

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

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

  • 相关阅读:
    8bit数据 转换为 16bit数据的四种方法
    可变长度的结构体定义
    【转】typedef和#define的用法与区别
    编程事项
    FreeRTOS不允许在中断服务程序和临界段中执行不确定的性的操作
    低优先级任务在执行过程中高优先级任务在干什么
    使用FreeRTOS在SD卡驱动使用非系统延时导致上电重启不工作的情况
    PMOS 与 NMOS
    keil优化等级设置
    #define用法之一
  • 原文地址:https://www.cnblogs.com/chenli90/p/10539375.html
Copyright © 2011-2022 走看看