我有一个安装MySQL的问题,
这是我的MySQL部分
--- - name: Install MySQL apt: name: "{{ item }}" with_items: - python-mysqldb - mysql-server - name: copy .my.cnf file with root password credentials template: src: templates/root/.my.cnf dest: ~/.my.cnf owner: root mode: 0600 - name: Start the MySQL service service: name: mysql state: started enabled: true # 'localhost' needs to be the last item for idempotency, see # http://ansible.cc/docs/modules.html#mysql-user - name: update mysql root password for all root accounts mysql_user: name: root host: "{{ item }}" password: "{{ mysql_root_password }}" priv: "*.*:ALL,GRANT" with_items: - "{{ ansible_hostname }}" - 127.0.0.1 - ::1 - localhost
我有这个错误
failed: [default] => (item=vagrant-ubuntu-trusty-64) => {"failed": true, "item": "vagrant-ubuntu-trusty-64"} msg: unable to connect to database, check login_user and login_password are correct or ~/.my.cnf has the credentials failed: [default] => (item=127.0.0.1) => {"failed": true, "item": "127.0.0.1"} msg: unable to connect to database, check login_user and login_password are correct or ~/.my.cnf has the credentials failed: [default] => (item=::1) => {"failed": true, "item": "::1"} msg: unable to connect to database, check login_user and login_password are correct or ~/.my.cnf has the credentials failed: [default] => (item=localhost) => {"failed": true, "item": "localhost"} msg: unable to connect to database, check login_user and login_password are correct or ~/.my.cnf has the credentials
我的.my.cnf是
[client] user=root password={{ mysql_root_password }}
并在服务器上复制时
[client] user=root password=root
我不明白为什么〜/ .my.cnf被创build
Github项目
谢谢
当mysql-server
被无端安装时,没有密码。 因此要使.my.cnf
工作,它应该有一个空白的密码行。 以下是我为.my.cnf
测试的.my.cnf
:
[client] user=root password=
把.my.cnf
放在你的vagrant
用户目录下也是有点奇怪的,因为它拥有root权限,只能以root身份可读。
确保.my.cnf
的密码为空白后,我可以在这四个上下文中正确设置root的密码。 请注意,之后运行失败,因为.my.cnf
需要更新,所以它不能通过幂等性测试。
有一个关于mysql_user模块页面的说明,建议编写密码, 然后写入.my.cnf
文件。 如果你这样做,你需要一个where
子句的mysql_user
行动(可能与之前的文件统计)。
更优雅的是使用check_implicit_admin
以及login_user
和login_password
。 这是非常幂等的。
作为第三种方式,也许check_implicit_admin
使得它更容易。
这是我成功的剧本,展示了上面的几个新服务器。 有点为此感到自豪。 注意.my.cnf
对于所有这些都是不必要的。
--- - hosts: mysql vars: mysql_root_password: fart tasks: - name: Install MySQL apt: name={{ item }} update_cache=yes cache_valid_time=3600 state=present sudo: yes with_items: - python-mysqldb - mysql-server #- name: copy cnf # copy: src=.my.cnf dest=~/.my.cnf owner=ubuntu mode=0644 # sudo: yes - name: Start the MySQL service sudo: yes service: name: mysql state: started enabled: true - name: update mysql root password for all root accounts sudo: yes mysql_user: name: root host: "{{ item }}" password: "{{ mysql_root_password }}" login_user: root login_password: "{{ mysql_root_password }}" check_implicit_admin: yes priv: "*.*:ALL,GRANT" with_items: - "{{ ansible_hostname }}" - 127.0.0.1 - ::1 - localhost
(编辑 – 删除my.cnf)
这里是我完整的MySQL工作,这可能会帮助你。
vars/main.yml
mysql_root_pass: mypassword #MySQL Root Password
tasks/main.yml
--- - name: Install the MySQL packages apt: name={{ item }} state=installed update_cache=yes with_items: - mysql-server-5.6 - mysql-client-5.6 - python-mysqldb - libmysqlclient-dev - name: Update MySQL root password for all root accounts mysql_user: name=root host={{ item }} password={{ mysql_root_pass }} state=present with_items: - "{{ ansible_hostname }}" - 127.0.0.1 - ::1 - localhost - name: Copy the root credentials as .my.cnf file template: src=root.cnf.j2 dest=~/.my.cnf mode=0600 - name: Ensure Anonymous user(s) are not in the database mysql_user: name='' host={{ item }} state=absent with_items: - localhost - "{{ ansible_hostname }}" - name: Remove the test database mysql_db: name=test state=absent notify: - Restart MySQL
templates/root.cnf.j2
[client] user=root password={{ mysql_root_pass }}
handlers/main.yml
--- - name: Restart MySQL service: name=mysql state=restarted
我的site.yml
是这样的:
--- - hosts: all become: yes gather_facts: yes roles: - mysql
如果您需要任何帮助,请检查此github 链接 。 谢谢