我在使用ansible在流氓ubuntu上安装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
由于
答案 0 :(得分:36)
无头安装mysql-server
时,没有密码。因此,要使.my.cnf
正常工作,它应该有一个空白的密码行。这是我为.my.cnf
测试的内容:
[client]
user=root
password=
将.my.cnf
放在您拥有的vagrant
用户目录中并且只能以root身份读取时,这也有点奇怪。
确保.my.cnf
中的密码为空后,我能够在这四种情况下正确设置root密码。请注意,之后无法运行,因为.my.cnf
需要更新,因此无法进行幂等性测试。
在ansible 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)
答案 1 :(得分:11)
这是我完整的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。感谢
答案 2 :(得分:0)
在ubuntu 20.04上,它现在需要login_unix_socket:
- name: setting default root password
mysql_user:
name: root
password: "{{ mysql_root_password }}"
check_implicit_admin: true
login_unix_socket: /var/run/mysqld/mysqld.sock
欢呼