使用SaltStack创建Mysql数据库

时间:2015-05-18 15:04:06

标签: mysql vagrant salt-stack devops

我正在使用SaltStack(与vagrant)/ Ubuntu 12.04。

我的目标是安装Mysql,创建数据库和用户并授予所有权限。

此SLS文件有什么问题?

mysql_setup:
  debconf.set:
    - name: mysql-server
    - data:
        'mysql-server/root_password': {'type': 'string', 'value': '{{ pillar['database']['password'] }}'}
        'mysql-server/root_password_again': {'type': 'string', 'value': '{{ pillar['database']['password'] }}'}

mysql-server:
  pkg:
    - installed
    - require:
      - debconf: mysql_setup

mysql-base:
  mysql_database.present:
    - name: owncloud
  mysql_user.present:
    - name: owncloud
    - password: pass
  mysql_grants.present:
    - database: owncloud.*
    - grant: ALL PRIVILEGES
    - user: owncloud

我遇到了这个错误:

[INFO    ] All specified answers are already set
[INFO    ] Completed state [mysql-server] at time 14:48:13.611204
[INFO    ] Running state [mysql-server] at time 14:48:13.644547
[INFO    ] Executing state pkg.installed for mysql-server
[INFO    ] Package mysql-server is already installed.
[INFO    ] Completed state [mysql-server] at time 14:48:13.695092
[DEBUG   ] LazyLoaded mysql_database.mod_init
[INFO    ] Running state [owncloud] at time 14:48:13.702359
[DEBUG   ] LazyLoaded mysql_database.present
[ERROR   ] State 'mysql_database.present' was not found in SLS 'mysql'

[DEBUG   ] LazyLoaded mysql_user.mod_init
[INFO    ] Running state [owncloud] at time 14:48:13.710744
[DEBUG   ] LazyLoaded mysql_user.present
[ERROR   ] State 'mysql_user.present' was not found in SLS 'mysql'

[DEBUG   ] LazyLoaded mysql_grants.mod_init
[INFO    ] Running state [mysql-base] at time 14:48:13.733003
[DEBUG   ] LazyLoaded mysql_grants.present
[ERROR   ] State 'mysql_grants.present' was not found in SLS 'mysql'

3 个答案:

答案 0 :(得分:4)

除了@Utah_Dave建议的内容之外,您还可能需要python-mysqldb APT包以及mysql pip包,并且您需要连接信息,可以在您的minion配置中提供,也可以提供给国家,可能是这样的:

foo_db:
  mysql_database.present:
    - name: {{ pillar['SQL_DATABASE'] }}
    - connection_host: {{ pillar['SQL_HOST'] }}
    - connection_user: {{ pillar['SQL_ROOT_USER'] }}
    - connection_pass: {{ pillar['SQL_ROOT_PASSWORD'] }}
    - require:
      - pip: mysql

未来状态可以使用use:重用连接信息,如下所示:

sql_app_user:
  mysql_user.present:
    - name: {{ pillar['SQL_APP_USER'] }}
    - password: {{ pillar['SQL_APP_PASSWORD'] }}
    - host: '%'
    - use:
      - mysql_database: foo_db

salt.modules.mysql记录了这一点。要安装其他依赖项:

server_pkgs:
  pkg:
    - installed
    - pkgs:
      - python-dev
    - refresh: True

mysql_python_pkgs:
  pkg.installed:
    - pkgs:
      - libmysqlclient-dev
      - mysql-client
      - python-mysqldb
    - require:
      - pkg: server_pkgs

python-pip:
  pkg:
    - installed
    - refresh: False

mysql:
  pip.installed:
    - require:
      - pkg: python-pip
      - pkg: mysql_python_pkgs

对于特定项目,这是我自己状态的精简版本,因此它可能不是您需要的最小的东西,但它确实有效。

答案 1 :(得分:0)

第二次运行状态时这是否有效?或者在重新启动salt-minion守护进程后?如果是这样,那是因为mysql_ *状态并不知道已经安装了mysql服务器。

您可以在此处看到mysql_grants状态要求已在Salt中加载mysql.grant_exists函数。 https://github.com/saltstack/salt/blob/1fb606ef6beffecc3e34f18b9ce189db4fe4292f/salt/states/mysql_grants.py#L52

尝试将- reload_modules: true添加到您的mysql-server状态,如下所示:

mysql-server:
  pkg:
    - installed
    - require:
      - debconf: mysql_setup
    - reload_modules: True

答案 2 :(得分:0)

我知道这是一个老问题,但是当我搜索错误时,它不断弹出:

  

无法LazyLoad mysql.version:'mysql'虚拟返回False:无法加载mysql执行模块:MySQLdb和PyMySQL都不可用。

我使用的是Ubuntu 18.04,为我解决的问题是安装软件包

  

python3-mysqldb

很显然,同时安装了python 2.7和3,salt使用了3,软件包python-mysqldb仅用于2.7。

我希望这会帮助其他遇到相同错误的人。