Open25

install mysql with ansible

R.C.LabR.C.Lab
vagrant@mochibell-local:~$ apt show mysql
N: Unable to locate package mysql
N: Unable to locate package mysql
E: No packages found
R.C.LabR.C.Lab

WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

akonadi-backend-mysql/jammy 4:21.12.3-0ubuntu3 all
asterisk-mysql/jammy 1:18.10.0~dfsg+~cs6.10.40431411-2 amd64
automysqlbackup/jammy 2.6+debian.4-4 all
cl-sql-mysql/jammy 6.7.1-3 amd64
courier-authlib-mysql/jammy 0.71.1-2build1 amd64
cvm-mysql/jammy 0.97-2 amd64
dbconfig-mysql/jammy 2.0.21 all
dbf2mysql/jammy 1.14a-7 amd64
default-libmysqlclient-dev/jammy 1.0.8 amd64
default-mysql-client/jammy 1.0.8 all
default-mysql-client-core/jammy 1.0.8 all
default-mysql-server/jammy 1.0.8 all
default-mysql-server-core/jammy 1.0.8 all
dovecot-mysql/jammy-updates 1:2.3.16+dfsg1-3ubuntu2.2 amd64
elpa-emacsql-mysql/jammy 3.0.0+ds-2 all
erlang-p1-mysql/jammy 1.0.19-2 amd64
freeradius-mysql/jammy-updates 3.0.26~dfsg~git20220223.1.00ed0241fa-0ubuntu3.2 amd64
gambas3-gb-db-mysql/jammy 3.16.3-3 amd64
gambas3-gb-mysql/jammy 3.16.3-3 amd64
gnokii-smsd-mysql/jammy 0.6.31+dfsg-2ubuntu10 amd64
golang-github-go-sql-driver-mysql-dev/jammy 1.5.0-1 all
golang-github-vividcortex-mysqlerr-dev/jammy 0.0~git20210426.69f897f-1 all
golang-github-ziutek-mymysql-dev/jammy 1.5.4+git20170206.23.0582bcf-2 all
groonga-normalizer-mysql/jammy 1.1.8-1 amd64
homer-api-mysql/jammy 5.0.6+dfsg2-3.2 amd64
icinga2-ido-mysql/jammy 2.13.2-1build2 amd64
kamailio-mysql-modules/jammy 5.5.4-1 amd64
kexi-mysql-driver/jammy 1:3.2.0-3 amd64
libapache2-mod-log-sql-mysql/jammy 1.100-16.3build2 amd64
libaprutil1-dbd-mysql/jammy-updates,jammy-security 1.6.1-5ubuntu4.22.04.2 amd64
libclass-dbi-mysql-perl/jammy 1.00-4.1 all
libcppdb-mysql0/jammy 0.3.1+dfsg-8ubuntu2 amd64
libcrypt-mysql-perl/jammy 0.04-6build7 amd64
libdatetime-format-mysql-perl/jammy 0.07.01-1 all
libdbd-mysql/jammy 0.9.0-10 amd64
libdbd-mysql-perl/jammy-updates,jammy-security 4.050-5ubuntu0.22.04.1 amd64
libgda-5.0-mysql/jammy 5.2.10-2 amd64
libkdb3-driver-mysql/jammy 3.2.0-5build1 amd64
libmysql++-dev/jammy 3.2.5-2.1 amd64
libmysql++-doc/jammy 3.2.5-2.1 all
libmysql++3v5/jammy 3.2.5-2.1 amd64
libmysql-diff-perl/jammy 0.60-1 all
libmysql-ocaml/jammy 1.2.4-1ubuntu3 amd64
libmysql-ocaml-dev/jammy 1.2.4-1ubuntu3 amd64
libmysqlclient-dev/jammy-updates,jammy-security 8.0.36-0ubuntu0.22.04.1 amd64
libmysqlclient21/jammy-updates,jammy-security 8.0.36-0ubuntu0.22.04.1 amd64
libmysqlcppconn-dev/jammy 1.1.12-4ubuntu2 amd64
libmysqlcppconn7v5/jammy 1.1.12-4ubuntu2 amd64
libodb-mysql-2.4/jammy 2.4.0-5ubuntu3 amd64
libodb-mysql-dev/jammy 2.4.0-5ubuntu3 amd64
libopendbx1-mysql/jammy 1.4.6-16 amd64
libpam-mysql/jammy 0.8.1-5build1 amd64
libpocodatamysql80/jammy 1.11.0-3 amd64
libpreludedb7-mysql/jammy 5.2.0-2build1 amd64
libqt5sql5-mysql/jammy-updates 5.15.3+dfsg-2ubuntu0.2 amd64
libqt6sql6-mysql/jammy-updates 6.2.4+dfsg-2ubuntu1.1 amd64
librdf-storage-mysql/jammy 1.0.17-1.1ubuntu3 amd64
libreoffice-mysql-connector/jammy-updates,jammy-security 1:7.3.7-0ubuntu0.22.04.4 amd64
libreoffice-sdbc-mysql/jammy-updates,jammy-security 1:7.3.7-0ubuntu0.22.04.4 amd64
librust-mysqlclient-sys-dev/jammy 0.2.4-1 amd64
  Auto-generated rust bindings for libmysqlclient - Rust source code
libsoci-mysql4.0/jammy 4.0.1-5ubuntu1 amd64
libtime-piece-mysql-perl/jammy 0.06-2.1 all
lighttpd-modules-mysql/jammy-updates,jammy-security 1.4.63-1ubuntu3.1 amd64
lua-dbi-mysql/jammy 0.7.2-2ubuntu1 amd64
lua-dbi-mysql-dev/jammy 0.7.2-2ubuntu1 amd64
lua-sql-mysql/jammy 2.6.0-1 amd64
lua-sql-mysql-dev/jammy 2.6.0-1 amd64
mha4mysql-manager/jammy 0.58-1.1 all
mha4mysql-node/jammy 0.58-1.1 all
mysql-client/jammy-updates,jammy-security 8.0.36-0ubuntu0.22.04.1 all
mysql-client-8.0/jammy-updates,jammy-security 8.0.36-0ubuntu0.22.04.1 amd64
mysql-client-core-8.0/jammy-updates,jammy-security 8.0.36-0ubuntu0.22.04.1 amd64
mysql-common/jammy 5.8+1.0.8 all
  MySQL database common files, e.g. /etc/mysql/my.cnf
mysql-router/jammy-updates,jammy-security 8.0.36-0ubuntu0.22.04.1 amd64
mysql-sandbox/jammy 3.2.05-1 all
mysql-server/jammy-updates,jammy-security 8.0.36-0ubuntu0.22.04.1 all
mysql-server-8.0/jammy-updates,jammy-security 8.0.36-0ubuntu0.22.04.1 amd64
mysql-server-core-8.0/jammy-updates,jammy-security 8.0.36-0ubuntu0.22.04.1 amd64
mysql-source-8.0/jammy-updates,jammy-security 8.0.36-0ubuntu0.22.04.1 amd64
mysql-testsuite/jammy-updates,jammy-security 8.0.36-0ubuntu0.22.04.1 all
mysql-testsuite-8.0/jammy-updates,jammy-security 8.0.36-0ubuntu0.22.04.1 amd64
mysqltcl/jammy 3.052-3ubuntu1 amd64
mysqltuner/jammy 1.7.17-1 all
node-mysql/jammy 2.18.1+~2.3.2-1 all
oar-server-mysql/jammy 2.5.9-1 amd64
oar-user-mysql/jammy 2.5.9-1 amd64
opendnssec-enforcer-mysql/jammy 1:2.1.10-1build1 amd64
orthanc-mysql/jammy 4.3-3build1 amd64
parser3-mysql/jammy 10.8-3 amd64
pdns-backend-mysql/jammy 4.5.3-1 amd64
perdition-mysql/jammy 2.2-3.1build3 amd64
php-mariadb-mysql-kbs/jammy 1.2.12-1 all
php-mdb2-driver-mysql/jammy 1.5.0b4-2 all
  mysql MDB2 driver
php-mysql/jammy 2:8.1+92ubuntu1 all
php8.1-mysql/jammy-updates,jammy-security 8.1.2-1ubuntu2.14 amd64
postfix-mysql/jammy-updates,jammy-security 3.6.4-1ubuntu1.3 amd64
postgresql-14-mysql-fdw/jammy 2.7.0-2 amd64
proftpd-mod-mysql/jammy 1.3.7c+dfsg-1build1 amd64
prometheus-mysqld-exporter/jammy-updates,jammy-security 0.13.0-1ubuntu0.2 amd64
puppet-module-puppetlabs-mysql/jammy 8.1.0-5ubuntu1 all
pure-ftpd-mysql/jammy-updates 1.0.50-2.1ubuntu0.22.04.1 amd64
python-aiomysql-doc/jammy 0.0.20-3 all
python-pymysql-doc/jammy 1.0.2-1ubuntu1 all
python3-aiomysql/jammy 0.0.20-3 all
python3-mysql.connector/jammy 8.0.15-2build1 all
python3-mysqldb/jammy 1.4.6-1build1 amd64
python3-pymysql/jammy 1.0.2-1ubuntu1 all
python3-testing.mysqld/jammy 1.4.0-4 all
r-cran-rmysql/jammy 0.10.23-1 amd64
roundcube-mysql/jammy 1.5.0+dfsg.1-2 all
rsyslog-mysql/jammy-updates,jammy-security 8.2112.0-2ubuntu2.2 amd64
rt4-db-mysql/jammy-updates,jammy-security 4.4.4+dfsg-2ubuntu1.22.04.1 all
rt5-db-mysql/jammy 5.0.1+dfsg-1ubuntu1 all
ruby-dataobjects-mysql/jammy 0.10.17-1ubuntu3 amd64
ruby-mysql2/jammy 0.5.3-3ubuntu4 amd64
tcl8.6-tdbc-mysql/jammy 1.1.3-2ubuntu1 amd64
tntdb-mysql4v5/jammy 1.3-4ubuntu1 amd64
ulogd2-mysql/jammy 2.0.7-1build1 amd64
voms-mysql-plugin/jammy 3.1.7-2ubuntu2 amd64
w1retap-mysql/jammy 1.4.6-1ubuntu1 amd64
zabbix-proxy-mysql/jammy 1:5.0.17+dfsg-1 amd64
zabbix-server-mysql/jammy 1:5.0.17+dfsg-1 amd6
R.C.LabR.C.Lab
~$ apt show mysql-server
Package: mysql-server
Version: 8.0.36-0ubuntu0.22.04.1
Priority: optional
Section: database
Source: mysql-8.0
Origin: Ubuntu
Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
Original-Maintainer: Debian MySQL Maintainers <pkg-mysql-maint@lists.alioth.debian.org>
Bugs: https://bugs.launchpad.net/ubuntu/+filebug
Installed-Size: 35.8 kB
Depends: mysql-server-8.0
Homepage: http://dev.mysql.com/
Task: lamp-server
Download-Size: 9,460 B
APT-Sources: http://us.archive.ubuntu.com/ubuntu jammy-updates/main amd64 Packages
Description: MySQL database server (metapackage depending on the latest version)
 This is an empty package that depends on the current "best" version of
 mysql-server (currently mysql-server-8.0), as determined by the MySQL
 maintainers. Install this package if in doubt about which MySQL
 version you need. That will install the version recommended by the
 package maintainers.
 .
 MySQL is a fast, stable and true multi-user, multi-threaded SQL database
 server. SQL (Structured Query Language) is the most popular database query
 language in the world. The main goals of MySQL are speed, robustness and
 ease of use.

N: There is 1 additional record. Please use the '-a' switch to see it```
R.C.LabR.C.Lab
---

- name: Install MySQL
  apt:
    name: mysql-server
    state: present
    update_cache: yes
R.C.LabR.C.Lab
vagrant@mochibell-local:~$ mysql --version
mysql  Ver 8.0.36-0ubuntu0.22.04.1 for Linux on x86_64 ((Ubuntu))
R.C.LabR.C.Lab

state: present # name で指定されているユーザーを存在させるか / すでに存在している場合スキップするのか?

R.C.LabR.C.Lab
TASK [mysql : Set password of root user] *****************************************************
fatal: [mochibell-local]: FAILED! => {"changed": false, "msg": "A MySQL module is required: for Python 2.7 either PyMySQL, or MySQL-python, or for Python 3.X mysqlclient or PyMySQL. Consider setting ansible_python_interpreter to use the intended Python version."}
R.C.LabR.C.Lab
vagrant@mochibell-local:~$ apt show python3-mysqldb
Package: python3-mysqldb
Version: 1.4.6-1build1
Priority: optional
Section: python
Source: python-mysqldb
Origin: Ubuntu
Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
Original-Maintainer: Debian Python Team <team+python@tracker.debian.org>
Bugs: https://bugs.launchpad.net/ubuntu/+filebug
Installed-Size: 181 kB
Provides: python3.10-mysqldb
Depends: python3 (<< 3.11), python3 (>= 3.10~), python3:any, libc6 (>= 2.4), libmysqlclient21 (>= 8.0.11)
Suggests: default-mysql-server | virtual-mysql-server
Homepage: https://github.com/PyMySQL/mysqlclient
Download-Size: 48.8 kB
APT-Sources: http://us.archive.ubuntu.com/ubuntu jammy/main amd64 Packages
Description: Python interface to MySQL
 Mysqlclient is an interface to the popular MySQL database server for Python.
 .
 This is a fork of MySQLdb. It add Python 3.3 support and merges some pull
 requests.
 .
 This package contains modules for all Python 3.x versions supported in Debian.
R.C.LabR.C.Lab
vagrant@mochibell-local:~$ apt show python3-pymysql
Package: python3-pymysql
Version: 1.0.2-1ubuntu1
Priority: optional
Section: python
Source: python-pymysql
Origin: Ubuntu
Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
Original-Maintainer: Debian OpenStack <team+openstack@tracker.debian.org>
Bugs: https://bugs.launchpad.net/ubuntu/+filebug
Installed-Size: 172 kB
Depends: python3:any
Recommends: python3-cryptography
Suggests: python-pymysql-doc
Homepage: https://github.com/PyMySQL/PyMySQL/
Download-Size: 38.3 kB
APT-Sources: http://us.archive.ubuntu.com/ubuntu jammy/main amd64 Packages
Description: Pure-Python MySQL Driver - Python 3.x
 This package contains a pure-Python MySQL client library. The goal of PyMySQL
 is to be a drop-in replacement for MySQLdb and work on CPython, PyPy,
 IronPython and Jython.
 .
 This package contains the Python 3.x module.
R.C.LabR.C.Lab
vagrant@mochibell-local:~$ apt list --installed | grep mysql

初期はなんもないが、 mysql-server を入れると色々入る

vagrant@mochibell-local:~$ apt list --installed | grep mysql

WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

mysql-client-8.0/jammy-updates,jammy-security,now 8.0.36-0ubuntu0.22.04.1 amd64 [installed,automatic]
mysql-client-core-8.0/jammy-updates,jammy-security,now 8.0.36-0ubuntu0.22.04.1 amd64 [installed,automatic]
mysql-common/jammy,now 5.8+1.0.8 all [installed,automatic]
mysql-server-8.0/jammy-updates,jammy-security,now 8.0.36-0ubuntu0.22.04.1 amd64 [installed,automatic]
mysql-server-core-8.0/jammy-updates,jammy-security,now 8.0.36-0ubuntu0.22.04.1 amd64 [installed,automatic]
mysql-server/jammy-updates,jammy-security,now 8.0.36-0ubuntu0.22.04.1 all [installed]
python3-pymysql/jammy,now 1.0.2-1ubuntu1 all [installed]
R.C.LabR.C.Lab
Install MySQL
  apt:
    name: [mysql-server, python3-pymysql]
    state: absent
    autoremove: true
    purge: true
    #update_cache: true

初期化?

R.C.LabR.C.Lab
vagrant@mochibell-local:~$ apt list --installed | grep mysql

WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

できた

R.C.LabR.C.Lab
- name: Set password of root user
  mysql_user:
    name: root # the user name in order to change settings.
    password: "{{ root_user_password | default('def@uIt&rOot&p@s5w0rd') }}" # change password to this valuable.

    # check if mysql allows login as root/nopassword before trying supplied (login_user / login_password) credentials.
    # if this option is `true` and succeed check, ansible will ignore login_user / login_password valuables.
    # インストール以外の作業をしていない状態の MySQL には root ユーザーには password は設定されていない
    check_implicit_admin: true
    login_user: root
    login_password: "{{ root_user_password | default('def@uIt&rOot&p@s5w0rd') }}"
    host: localhost # default: localhost

    state: present # name で指定されているユーザーを存在させるか / すでに存在している場合スキップするのか?
TASK [mysql : Set password of root user] *****************************************************
fatal: [mochibell-local]: FAILED! => {"changed": false, "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'\")"}
R.C.LabR.C.Lab

なんかよくわからんが、
https://www.claudiokuenzler.com/blog/1236/ansible-reset-mysql-root-password-unable-connect-check-login-user
にあるように

- name: Set password of root user
  mysql_user:
    name: root # the user name in order to change settings.
    password: "{{ root_user_password | default('def@uIt&rOot&p@s5w0rd') }}" # change password to this valuable.

    # check if mysql allows login as root/nopassword before trying supplied (login_user / login_password) credentials.
    # if this option is `true` and succeed check, ansible will ignore login_user / login_password valuables.
    # インストール以外の作業をしていない状態の MySQL には root ユーザーには password は設定されていない
    check_implicit_admin: true
    login_user: root
    login_password: "{{ root_user_password | default('def@uIt&rOot&p@s5w0rd') }}"
    host: localhost # default: localhost

    login_unix_socket: /var/run/mysqld/mysqld.sock
    state: present # name で指定されているユーザーを存在させるか / すでに存在している場合スキップするのか?

こうすると通る

R.C.LabR.C.Lab

ubuntu は mysql root user の tcp コネクションを許可していない?

R.C.LabR.C.Lab

嘘だな。そもそも

vagrant@mochibell-local:~$ mysql -uroot -p
Enter password:

これって tcp なんか?

R.C.LabR.C.Lab
localhost の解釈
トランスポートプロトコルを明示的に指定しない場合、localhost は次のように解釈されます:

Unix および Unix に似たシステムでは、localhost に接続するとソケットファイル接続が行われます。

それ以外の場合、localhost に接続すると、127.0.0.1 への TCP/IP 接続になります。

トランスポートプロトコルが明示的に指定されている場合、localhost はそのプロトコルに関して解釈されます。 たとえば、--protocol=TCP では、localhost に接続すると、すべてのプラットフォームの 127.0.0.1 に TCP/IP 接続されます。

https://dev.mysql.com/doc/refman/8.0/ja/transport-protocols.html

R.C.LabR.C.Lab
#
# The MySQL database server configuration file.
#
# One can use all long options that the program supports.
# Run program with --help to get a list of available options and with
# --print-defaults to see which it would actually understand and use.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html

# Here is entries for some specific programs
# The following values assume you have at least 32M ram

[mysqld]
#
# * Basic Settings
#
user            = mysql
# pid-file      = /var/run/mysqld/mysqld.pid
# socket        = /var/run/mysqld/mysqld.sock
# port          = 3306
# datadir       = /var/lib/mysql

# If MySQL is running as a replication slave, this should be
# changed. Ref https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_tmpdir
# tmpdir                = /tmp
#
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
bind-address            = 127.0.0.1
mysqlx-bind-address     = 127.0.0.1
#
# * Fine Tuning
#
key_buffer_size         = 16M
# max_allowed_packet    = 64M
# thread_stack          = 256K

# thread_cache_size       = -1

# This replaces the startup script and checks MyISAM tables if needed
# the first time they are touched
myisam-recover-options  = BACKUP

# max_connections        = 151

# table_open_cache       = 4000

#
# * Logging and Replication
#
# Both location gets rotated by the cronjob.
#
# Log all queries
# Be aware that this log type is a performance killer.
# general_log_file        = /var/log/mysql/query.log
# general_log             = 1
#
# Error log - should be very few entries.
#
log_error = /var/log/mysql/error.log
#
# Here you can see queries with especially long duration
# slow_query_log                = 1
# slow_query_log_file   = /var/log/mysql/mysql-slow.log
# long_query_time = 2
# log-queries-not-using-indexes
#
# The following can be used as easy to replay backup logs or for replication.
# note: if you are setting up a replication slave, see README.Debian about
#       other settings you may need to change.
# server-id             = 1
# log_bin                       = /var/log/mysql/mysql-bin.log
# binlog_expire_logs_seconds    = 2592000
max_binlog_size   = 100M
# binlog_do_db          = include_database_name
# binlog_ignore_db      = include_database_name
R.C.LabR.C.Lab

apt install mysql-server の時点で systyemctl is-enable (自動起動) になってる
そして、起動している