# ps -ef|grep __salt
root 15510 15501 0 16:40 ? 00:00:00 [__salt.tmp.cZvT] <defunct>
mysql-community-server:
pkg:
- installed
mysql_conf:
file.managed:
- name: /etc/my.cnf
- source: ftp://10.0.0.4/config_file/salt/mysql/my.cnf
- source_hash: ftp://10.0.0.4/config_file/salt/mysql/my.cnf.sha512
- template: jinja
- defaults:
hostname: {{ grains['host'] }}
- require:
- pkg: mysql-community-server
/root/.bashrc:
file.append:
- text:
- alias my3306='mysql -uroot -pDsuBbT6u4MYcCGm0% -S/tmp/mysql.sock --prompt="\\u@\\h:\\d \\r:\\m:\\s>"'
mysql_init:
cmd.script:
- source: ftp://10.0.0.4/config_file/salt/mysql/init_mysql.sh
- source_hash: ftp://10.0.0.4/config_file/salt/mysql/init_mysql.sh.sha512
- cwd: /tmp
- require:
- file: mysql_conf
# cat /tmp/__salt.tmp.cZvT6R.sh
#!/bin/bash
sql_user="alter user 'root'@'localhost' identified by 'my password'"
mysqld --initialize-insecure --user=mysql --datadir=/data/mysql
if [ $? = 0 ];then
service mysqld start
echo "${sql_user}" | mysql -uroot -S/tmp/mysql.sock
else
echo init failed && exit 1
fi
exit 0
#this command stall #salt&#39; mysqltest1&#39; state.sls mysql
答案 0 :(得分:0)
避免在cmd.run或cmd.script中使用交互式程序。 mysql命令行客户端提供mysql --batch
参数,以避免阻止交互式提示。尽可能避免使用管道来简化(易错)shell命令执行环境。
为获得最佳效果,请使用mysql执行模块(状态模块,如果可用),并确保salt-minion可以使用相应的mysql python模块。请考虑使用https://github.com/saltstack-formulas/mysql-formula
等社区公式如果必须使用cmd.run或cmd.script: 不要在脚本中运行多个步骤。没有办法说出出了什么问题。将其分解为多个盐状态并提供必要条件以确保它们在正确的条件下以正确的顺序运行。
而不是shell脚本,您可以使用状态sl看起来像这样:
mysqld initialized insecure:
cmd.run:
- name: mysqld --initialize-insecure --user=mysql --datadir=/data/mysql
- creates: /data/mysql/ibdata1
- watch:
- pkg: mysql-community-server
mysqld service running:
service.running:
- name: mysqld
- require:
- mysqld initialize insecure
{% set my_password = salt['pillar.get']('mysql:user:root:password') %}
mysqld local root password:
cmd.run:
- name: mysql -uroot -S/tmp/mysql.sock --batch --execute "alter user 'root'@'localhost' identified by '{{ my_password }}'"
- prereq:
- mysqld initialized insecure
- require:
- mysqld service running