【问题标题】:How to install MySQL 5.7 using Ansible and Vagrant?如何使用 Ansible 和 Vagrant 安装 MySQL 5.7?
【发布时间】:2016-05-30 21:07:51
【问题描述】:

我正在尝试使用 Vagrant 在 Ubuntu Trusty64 上自动安装 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

这会在 Vagrant 实例上安装 MySQL 5.7,但是当我尝试使用 mysql -p -u root 登录时,它根本不起作用;好像没有空密码了,我试着检查一下输出。

有什么方法可以获取密码或可以在安装过程中设置密码吗?

【问题讨论】:

标签: mysql vagrant ansible


【解决方案1】:

由于 MySQL 5.7 版本不仅安装了 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 避免在我的剧本再次运行时停止它 - 设置密码后此命令将不起作用。

【讨论】:

  • 我听说你可以使用 ansible debconf 模块来做到这一点,但它似乎不适用于 Ubuntu 14.04 和 MySQL 5.7 和 ansible 2.3.0.0-1
【解决方案2】:

这是我在vagrant中安装后配置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

我不记得我是如何做到这一点的,已经有一段时间了。可能是一些链接和实验。试一试。

【讨论】:

    【解决方案3】:

    我发现有趣的是,用户 vagrant mysql -p -u root 或简单的 mysql -u root 不起作用....但是如果你和 root 一样,你没有密码,你可以开始做你的事情。 ...

    干杯!

    【讨论】:

    • 在根用户的主目录下检查.my.cnf 文件,看看为什么会出现这种情况。您可能希望编辑此答案以更彻底地解释其工作原理以及如何使用该信息继续您的 Ansible 角色/扮演来设置 root 用户的凭据或创建更多用户等。
    猜你喜欢
    • 2016-11-26
    • 2017-07-05
    • 1970-01-01
    • 2020-08-15
    • 1970-01-01
    • 1970-01-01
    • 2016-12-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多