问题描述
我在无业游民的ubuntu上安装ansible 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
项目Github
谢谢
最佳答案
无头安装mysql-server
时,没有密码。因此,要使.my.cnf
正常工作,它应该有一个空白的密码行。这是我为.my.cnf
测试的内容:
[client]
user=root
password=
将.my.cnf
放置在您的vagrant
用户目录中(由root拥有,并且只能由root读取)也有些奇怪。
确保在.my.cnf
中密码为空后,我能够在这四个上下文中为root正确设置密码。请注意,此后它无法运行,因为需要更新.my.cnf
,因此它无法通过幂等性测试。
在空白的mysql_user模块页面上有一条注释,建议您写入密码,然后再写入.my.cnf
文件。如果这样做,则需要mysql_user
操作的where
子句(可能在此之前带有文件统计信息)。
更优雅的是将check_implicit_admin
以及login_user
和login_password
一起使用。这是幂等的。
第三种方式,也许check_implicit_admin
使它变得更加容易。
这是我成功的剧本,上面显示了上述内容,并通过一些新服务器进行了测试。 Kinda为此感到自豪。注意.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
问/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
模板/root.cnf.j2
[client]
user=root
password={{ mysql_root_pass }}
处理程序/main.yml
---
- name: Restart MySQL
service: name=mysql state=restarted
site.yml
---
- hosts: all
become: yes
gather_facts: yes
roles:
- mysql
如果您需要任何帮助,请检查此github link。谢谢