2016-05-30 76 views
0

我想在使用Vagrant的Ubuntu Trusty64上自動安裝MySQL 5.7。這是我怎麼安裝的樣子:如何使用Ansible和Vagrant安裝MySQL 5.7?

- name: Install new APT package repository 
    apt: deb=mysql-apt-config_0.7.2-1_all.deb 
    sudo: yes 
- name: Updating the cache 
    apt: update_cache=yes 
    sudo: yes 
- name: Install mysql server 
    sudo: yes 
    apt: pkg={{ item }} state=latest 
    with_items: 
    - mysql-server 
    - mysql-client 
    - python-mysqldb 

這將安裝的MySQL 5.7在流浪實例,但是當我嘗試登錄使用mysql -p -u root它根本不起作用;它似乎沒有更多的空密碼,我試圖檢查輸出。

是否有任何方法獲取密碼或在安裝過程中設置它?

+1

'mysql_user'任務可以在這裏幫忙嗎? http://docs.ansible.com/ansible/mysql_user_module.html – halfer

回答

0

這是我如何在顛沛流離的安裝後配置MySQL:

- name: Configure mysql step 1 
    shell: echo mysql-server mysql-server/root_password password vagrant | debconf-set-selections 
    become: true 

- name: Configure mysql step 2 
    shell: echo mysql-server mysql-server/root_password_again password vagrant | debconf-set-selections 
    become: true 

- name: Create mysql client configuration file 
    blockinfile: 
    dest: ~/.my.cnf 
    create: yes 
    block: | 
     [client] 
     user=root 
     password=vagrant 

我不記得我是如何達到這一點,它已經相當長一段時間。可能是一些鏈接和實驗。試一試。

0

我發現有趣的是,用戶vagrant mysql -p -u root或者簡單的mysql -u root不起作用....但是如果你像root一樣做,你就沒有密碼,你可以開始做你的事情...

乾杯!

+1

在根用戶的主目錄下檢查一個'.my.cnf'文件,看看這是爲什麼。你可能想要編輯這個答案,以更全面地解釋爲什麼這會起作用,以及如何使用這些信息繼續使用這些信息來設置root用戶的憑據或創建更多用戶等等。 – ydaetskcoR

1

從版本5.7開始,MySQL不僅安裝了root用戶的空密碼,還安裝了用於root帳戶的身份驗證插件auth_socket,因此您可以以root用戶身份啓動mysql,而不需要密碼,但同一命令不會讓您進入它由另一個用戶開始。這很好,但我們很多人都很驚訝。爲了實現舊式行爲,我們來你需要改變身份驗證插件,並在一次使用的命令是這樣設置的密碼:

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'test'; 

我發現它很好地說明如下:Change user password in MySQL 5.7 with 「plugin: auth_socket」(和學分去那裏)

這裏是我的Ansible一塊做:

- name: Set MySQL root password 
    command: /usr/bin/mysql -e "alter user 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'yourrootpassword';" 
    ignore_errors: yes 
    become: yes 

我以前ignore_errors避免停止我的劇本,當它再次運行 - 此命令將不設置密碼後工作。

+0

我讀過你可以做到這一點帶有「debconf」模塊,但它在Ubuntu 14.04和MySQL 5.7以及2.3.0.0-1中似乎不起作用 – SomethingOn