問題描述
我在流浪的 ubuntu 上安裝帶有 ansible 的 MySQL 時遇到問題,
I have a problem installing MySQL with ansible on a vagrant ubuntu,
這是我的 MySQL 部分
This is my MySQL part
---
- 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 是
my .my.cnf is
[client]
user=root
password={{ mysql_root_password }}
當復制到服務(wù)器上時
[client]
user=root
password=root
我不明白為什么,創(chuàng)建了 ~/.my.cnf
I don't understand why, ~/.my.cnf is created
項目 Github
謝謝
推薦答案
當 mysql-server
無頭安裝時,沒有密碼.因此,要使 .my.cnf
工作,它應(yīng)該有一個空白的密碼行.這是我為 .my.cnf
測試的內(nèi)容:
When mysql-server
is installed headlessly, there's no password. Therefore to make .my.cnf
work, it should have a blank password line. Here's what I tested with for a .my.cnf
:
[client]
user=root
password=
將 .my.cnf
放在您的 vagrant
用戶目錄中也有點奇怪,因為它由 root 擁有并且只能作為 root 讀取.
It's also slightly strange to put .my.cnf
in your vagrant
user directory as owned by root and only readable as root.
確保 .my.cnf
中的密碼為空后,我能夠在這四個上下文中正確設(shè)置 root 的密碼.請注意,此后它無法運行,因為需要更新 .my.cnf
,因此它無法通過冪等性測試.
After ensuring the password was blank in .my.cnf
, I was able to properly set the password for root in those four contexts. Note that it fails to run after that, since .my.cnf
would need to be updated, so it fails the idempotency test.
ansible mysql_user 模塊頁面上有一條注釋,建議先寫密碼,然后然后寫.my.cnf
文件.如果你這樣做,你需要一個 where
子句到 mysql_user
操作(可能在此之前有一個文件統(tǒng)計信息).
There's a note on the ansible mysql_user module page that suggests writing the password and then writing the .my.cnf
file. If you do that, you need a where
clause to the mysql_user
action (probably with a file stat before that).
更優(yōu)雅的是將 check_implicit_admin
與 login_user
和 login_password
一起使用.這真是冪等的.
Even more elegant is to use check_implicit_admin
along with login_user
and login_password
. That's beautifully idempotent.
作為第三種方式,也許 check_implicit_admin
會更容易.
As a third way, perhaps check_implicit_admin
makes it even easier.
這是我成功的劇本,展示了上述內(nèi)容,并在幾臺新服務(wù)器上進行了測試.有點為此感到自豪.注意所有這些都不需要 .my.cnf
.
Here's my successful playbook showing the above, tested with a few fresh servers. Kinda proud of this. Note .my.cnf
is unnecessary for all of this.
---
- 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)
(edit- removed my.cnf)
這篇關(guān)于在 ubuntu 上使用 ansible 安裝 MySQL的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!