Mysql为任何主机授予root权限

时间:2015-07-18 05:13:10

标签: mysql salt-stack

我正在尝试使用salt-stack在ec2实例上安装MySQL。我想为root用户添加grant。任何使用密码的主机都可以访问它。

这是我的州档案:

mysql-server:
  pkg:
    - installed
    - pkgs:
      - mysql-server
      - python-mysqldb
  service:
    - running
    - name: mysql
    - enable: True
    - require:
      - pkg: mysql-server
    - watch:
      - file: /etc/mysql/my.cnf
  mysql_user:
    - present
    - name: root
    - password: {{ pillar['mysql']['server']['root_password'] }}
    - require:
      - service: mysql

/srv/.my.cnf:
  file:
    - managed
    - source: salt://files/mysql/app-my.cnf
    - user: app
    - group: app
    - mode: 0600
    - template: jinja
    - require:
      - user: app

/root/.my.cnf:
  file:
    - managed
    - source: salt://files/mysql/root-my.cnf
    - user: root
    - group: root
    - mode: 0600
    - template: jinja

{{pillar.default_database.name}}:
  mysql_database.present

mysql-app-user:
  mysql_user.present:
    - name: app
    - host: '%'
    - password: {{ pillar['mysql']['server']['root_password'] }}
    - connection_user: root
    - connection_pass: {{ pillar['mysql']['server']['root_password'] }}
    - connection_charset: utf8
    - saltenv:
      - LC_ALL: "en_US.utf8"
    - require:
      - service: mysql

mysql-app-local-user:
  mysql_user.present:
    - name: app
    - host: localhost
    - password: {{ pillar['mysql']['server']['root_password'] }}
    - connection_user: root
    - connection_pass: {{ pillar['mysql']['server']['root_password'] }}
    - connection_charset: utf8
    - saltenv:
      - LC_ALL: "en_US.utf8"
    - require:
      - service: mysql

mysql_root_remote_grant:
   mysql_grants.present:
    - grant: all privileges
    - database: '*.*'
    - user: root

mysql_app_remote_grant:
   mysql_grants.present:
    - grant: all privileges
    - database: '*.*'
    - user: app

mysql_app_local_grant:
   mysql_grants.present:
    - grant: all privileges
    - database: '*.*'
    - user: app
    - host: localhost

这部分可以访问任何主机但没有密码。

mysql_root_remote_grant:
   mysql_grants.present:
    - grant: all privileges
    - database: '*.*'
    - user: root

这有效,但它允许从没有密码的任何主机访问root。我不知道我在这里做错了什么。如何使用带有salt-stack install的密码从任何主机访问root?

1 个答案:

答案 0 :(得分:0)

以下是我解决问题的方法,我只需为root(任何主机)添加另一个mysql_user.present - %,如下所示。

mysql-root-user-remote:
  mysql_user.present:
    - name: root
    - host: '%'
    - password: {{ pillar['mysql']['server']['root_password'] }}
    - connection_user: root
    - connection_pass: {{ pillar['mysql']['server']['root_password'] }}
    - connection_charset: utf8
    - saltenv:
      - LC_ALL: "en_US.utf8"
    - require:
      - service: mysql

因此,mysql_user.present用户的两个root声明用于localhost,另一个用于任何主机。在此mysql_grant.present之后。