Ansible: 使用/root/.my.cnf时mysql_user在2.7.1中损坏

创建于 2018-10-29  ·  29评论  ·  资料来源: ansible/ansible



概要

从2.7.0升级到2.7.1时,我们开始从mysql_user模块中看到此错误

unable to connect to database, check login_user and login_password are correct or /root/.my.cnf has the credentials. Exception message: (1698, \"Access denied for user 'root'@'localhost'\")

任务如下:

# Write password to root home as readable only by root so future mysql
# operations can function.
- name: Write root login credentials
  copy:
      dest: /root/.my.cnf
      owner: root
      group: root
      mode: 0600
      content: |
          # {{ ansible_note }}
          [client]
          user=root
          password={{ db_root_password }}

# By default the root user has no password. Set one.
- name: Set root user password
  mysql_user:
      host: "{{ item }}"
      name: root
      password: "{{ db_root_password }}"
      # Login without credentials
      check_implicit_admin: yes
      state: present
  with_items:
      - localhost
      - "{{ ansible_hostname }}"
      - 127.0.0.1
      - ::1

后者失败了。

Ansible 2.7.0正常工作。

问题类型
  • 错误报告
组件名称

mysql_user

可用版本


在下面粘贴
Ansible 2.7.1
配置文件=无
配置的模块搜索路径= ['/Users/esamatti/.ansible/plugins/modules','/ usr / share / ansible / plugins / modules']
ansible python模块位置= /usr/local/lib/python3.7/site-packages/ansible
可执行位置= / usr / local / bin / ansible
python版本= 3.7.0(默认,2018年10月2日,09:18:58)[Clang 10.0.0(clang-1000.11.45.2)]


##### CONFIGURATION
<!--- Paste verbatim output from "ansible-config dump --only-changed" between quotes -->
```paste below
ANSIBLE_PIPELINING(/Users/epeli/code/playbooks/ansible.cfg) = True
ANSIBLE_SSH_ARGS(/Users/epeli/code/playbooks/ansible.cfg) = -o ForwardAgent=yes
DEFAULT_FORKS(/Users/epeli/code/playbooks/ansible.cfg) = 100
DEFAULT_HOST_LIST(/Users/epeli/code/playbooks/ansible.cfg) = ['/Users/epeli/code/playbooks/tools/dynamic-inventory']
DEFAULT_REMOTE_USER(/Users/epeli/code/playbooks/ansible.cfg) = root
DEFAULT_SUDO_FLAGS(/Users/epeli/code/playbooks/ansible.cfg) = -HE
DEFAULT_VAULT_PASSWORD_FILE(env: ANSIBLE_VAULT_PASSWORD_FILE) = /Users/epeli/.ansible-vault-password
操作系统/环境

macOS Mojave版本10.14(18A391)

和Ubuntu 18.04服务器

重现步骤

见总结

预期成绩

实际结果


paste below TASK [mariadb : Set root user password] ******************************************************************************************************************************************************************************** failed: [1.2.3.4] (item=localhost) => {"changed": false, "item": "localhost", "msg": "unable to connect to database, check login_user and login_password are correct or /root/.my.cnf has the credentials. Exception message: (1698, \"Access denied for user 'root'@'localhost'\")"} failed: [1.2.3.4] (item=customer) => {"changed": false, "item": "customer", "msg": "unable to connect to database, check login_user and login_password are correct or /root/.my.cnf has the credentials. Exception message: (1698, \"Access denied for user 'root'@'localhost'\")"} failed: [1.2.3.4] (item=127.0.0.1) => {"changed": false, "item": "127.0.0.1", "msg": "unable to connect to database, check login_user and login_password are correct or /root/.my.cnf has the credentials. Exception message: (1698, \"Access denied for user 'root'@'localhost'\")"} failed: [1.2.3.4] (item=::1) => {"changed": false, "item": "::1", "msg": "unable to connect to database, check login_user and login_password are correct or /root/.my.cnf has the credentials. Exception message: (1698, \"Access denied for user 'root'@'localhost'\")"}

affects_2.7 bug collection community.general database has_pr module mysql needs_collection_redirect python3 community test

最有用的评论

好的,我找到了。 这是10月2日在#ansible-devel上进行的讨论,并且连接出现问题的用户(他首先怀疑是空字符串密码的处理是问题),最终发现他需要使用login_unix_socket (由于我上面提到的行为不同),它对他很有用。 不幸的是,他没有为此提出任何问题。

无论如何, @ epeli @anniemelen @ bchanan03 ,您可以尝试login_unix_socket选项,看看是否能为您解决问题?

所有29条评论

@epeli ,谢谢你提交这个问题!

单击此处以获取机器人帮助

说明中标识的文件:

如果这些文件不正确,请更新描述的component name部分或使用!component bot命令。

单击此处以获取机器人帮助

@epeli嗨,请问https://github.com/ansible/ansible/pull/47809为您解决了这个问题,您能否在PR上添加评论

@timorunge您知道,没有此补丁,可以添加其他mysql用户...
是的,我的问题与此PR不相关,但与#47736有关,所以我继续在那儿谈论。 而且我不能提供Dockerfile,抱歉。

您是否已经以不同的顺序尝试过相同的内容? 基本含义是:首先不使用任何密码登录,设置root密码,然后生成.my.cnf?

这些天您在使用哪个python DB连接器? MySQLdb还是PyMySQL?

大家好,
Ansible 2.7.5是否可能会遇到相同的问题? 与mysql_db

ansible 2.7.5
  config file = None
  configured module search path = [u'/home/vagrant/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/lib/python2.7/dist-packages/ansible
  executable location = /usr/local/bin/ansible
  python version = 2.7.12 (default, Nov 12 2018, 14:36:49) [GCC 5.4.0 20160609]

运行:
`''-名称:install_gerrit | 创建MySQL数据库(如果使用)
mysql_db:
名称:“ {{item.db}}”
编码:latin1
状态:存在
with_items:'{{gerrit_db_info}}'
时间:定义了item.type且item.type ==“ mysql”

Error:

失败:[192.168.202.15] {
“已更改”:错误,
“调用”:{
“ module_args”:{
“归类”:“”,
“ config_file”:“ /root/.my.cnf”,
“ connect_timeout”:30,
“ encoding”:“ latin1”,
“ ignore_tables”:[],
“ login_host”:“本地主机”,
“ login_password”:null,
“ login_port”:3306,
“ login_unix_socket”:null,
“ login_user”:null,
“ name”:“ reviewdb”,
“ quick”:是的,
“ single_transaction”:否,
“ ssl_ca”:null,
“ ssl_cert”:null,
“ ssl_key”:null,
“ state”:“现在”,
“目标”:空
}
},
“项目”:{
“ db”:“ reviewdb”,
“ host”:“ localhost”,
“通过”:“ xxxxxx”,
“ type”:“ mysql”,
“ user”:“ gerrit”
},
“ msg”:“无法连接到数据库,请检查login_user和login_password是否正确或/root/.my.cnf具有凭据。异常消息:(1698,u \”用户'root'@'localhost'的访问被拒绝“)”
}
```

mysql_db似乎存在相同问题-移回ansible v2.7.0解决了该问题。

我不知道那是哪里,但是一段时间(几周?几个月?)前,我遇到了类似的问题,原因是MySQLdb(基于libmysql)默认使用Unix套接字进行连接,而PyMySQL默认情况下通过TCP连接到localhost 。 根据对MySQL服务器的root访问权限的配置方式,这可能有助于您解决此问题。

就像我说的,我真的不记得我以前写过这篇文章的地方了,我也不是很确定细节,但是也许这给你们中的一个人提供了解决问题的提示:)

好的,我找到了。 这是10月2日在#ansible-devel上进行的讨论,并且连接出现问题的用户(他首先怀疑是空字符串密码的处理是问题),最终发现他需要使用login_unix_socket (由于我上面提到的行为不同),它对他很有用。 不幸的是,他没有为此提出任何问题。

无论如何, @ epeli @anniemelen @ bchanan03 ,您可以尝试login_unix_socket选项,看看是否能为您解决问题?

你好,
我在Centos7,ansible 2.7.6中遇到了同样的问题。
~/.my.cnf指定socket=/var/lib/mysql/mysql.sock ~/.my.cnf
或mysql_db任务中的login_unix_socket: /var/lib/mysql/mysql.sock选项
正在修复连接错误。

cc @Xyon @bmalynovytch @dagwieers @michaelcoburn @oneiroi @tolland
单击此处以获取机器人帮助

抱歉,到现在为止,我认为此错误仍未得到解决。

@nizarakbarm您是否尝试过我的建议,而@efflamlemaillet已尝试并验证过? 如果您可以尝试这样做,然后在此处确认它是否有效(不行!),那将是很好的。

@felixfontein ,在Ubuntu 18.04上, @ efflamlemaillet提供的解决方法解决了该问题
我认为以下几点:
https://stackoverflow.com/questions/41846000/mariadb-password-and-unix-socket-authentication-for-root
也有关系。

你好
@pouyana所说,以某种方式,以前在本地服务器上默认使用套接字连接的

@felixfontein可以,但是我不喜欢使用〜/ .my.cnf

@nizarakbarm login_unix_socket: /var/lib/mysql/mysql.sock是在Ansible中指定的内容。 你有尝试过吗?

@efflamlemaillet更精确地说,它不是Ansible模块本身,而是它使用的Python MySQL连接库。 旧的库MySQLdb(基于libmysql)更喜欢Unix套接字,而新的库PyMySQL更喜欢TCP连接。 如果您明确地在剧本/角色中指定了连接方式,那就没关系; 但是,如果不这样做,则取决于模块所使用的库。 该模块首先尝试PyMySQL,如果PyMySQL不可用,则回退到MySQLdb。

好的,谢谢您的回复。 @felixfontein从您的答案中,它可以帮助我理解问题的原因。

整个过程就是要知道是否应该将其视为错误?
如果是这样,当安装的驱动程序是MySQLdb或PyMySQL时,Ansible模块可能会覆盖PyMySQL的默认行为,以确保模块以相同的方式连接(如果未指定连接选项)。

抄送@ Alexander198961
单击此处以获取机器人帮助

就我而言,这种解决方法也有帮助
login_unix_socket: /var/run/mysqld/mysqld.sock

ansible 2.8.4
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/home/eldar/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/dist-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.15+ (default, Nov 27 2018, 23:36:35) [GCC 7.3.0]
- name: Create database
  mysql_db:
    name: dbname
    state: present
    login_unix_socket: /var/run/mysqld/mysqld.sock  

感谢@felixfontein指出这一点!
指定参数login_unix_socket: /var/run/mysqld/mysqld.sock解决了这个问题!

虽然必须更新文件tasks/databases.ymltasks/secure-installation.ymltasks/users.yml每个mysql_{user,db}模块任务

我在文档https://github.com/ansible/ansible/pull/66848中解释了这一点
它带有2.10

close_me

此页面是否有帮助?
0 / 5 - 0 等级