3

Summary:

  • mysql-server 8.0.16 is running inside a Docker container.
  • php 7 + Apache2 + myslnd 5.0.12 + mysqld 5.6.22 are running outside Docker.

In PHP, mysqli fails thus:

mysqli_connect('127.0.0.1', 'test', 'test', 'test', 8016)

ERR: 2054 : The server requested authentication method unknown to the client

Meanwhile mysql can connect thus from outside Docker:

mysql  -P 8016 -h 127.0.0.1 -u test -ptest test

Question: How can I get mysqli_connect working?

Details:

Here are some of the relevant GRANT entries.

mysql> select host, user, db, select_priv, grant_priv
                  from mysql.db where user='test';
+------------+------+------+-------------+------------+
| host       | user | db   | select_priv | grant_priv |
+------------+------+------+-------------+------------+
| 172.17.0.1 | test | test | Y           | N          |

mysql> select host, user, plugin, select_priv, grant_priv
                    from mysql.user where user='test';
+------------+------+-----------------------+-------------+------------+
| host       | user | plugin                | select_priv | grant_priv |
+------------+------+-----------------------+-------------+------------+
| 172.17.0.1 | test | mysql_native_password | N           | N          |

I found the /etc/my.cnf inside the docker image and enabled

default-authentication-plugin=mysql_native_password

That allowed even an old (5.6) mysql to connect thus:

mysql  -P 8016 -h 127.0.0.1 -u test -ptest test

And, yes, it came back with a heading including

Server version: 8.0.16 MySQL Community Server - GPL

It seems that the commandline mysql knows about mysql_native_password, but mysqlnd does not.

More version info:

PHP Version 7.0.33-0ubuntu0.16.04.6
Apache/2.4.18 (Ubuntu) 

phpinfo says this about mysqlnd:

Version:         mysqlnd 5.0.12-dev - 20150407 - $Id: b5c5906d452ec590732a93b051f3827e02749b83 $
Loaded plugins:  mysqlnd,debug_trace,auth_plugin_mysql_native_password,auth_plugin_mysql_clear_password,auth_plugin_sha256_password
API Extensions:  mysqli,pdo_mysql 

#docker ps (shows port mapping):

CONTAINER ID        IMAGE                       COMMAND                  CREATED             STATUS                  PORTS                               NAMES
99193bedb36c        mysql/mysql-server:8.0.16   "/entrypoint.sh my..."   8 weeks ago         Up 12 hours (healthy)   33060/tcp, 0.0.0.0:8016->3306/tcp   o8016

PHP-CLI also fails to connect.

(Yes, I ran FLUSH PRIVILEGES; at the appropriate time.)

Dharman
  • 30,962
  • 25
  • 85
  • 135
Rick James
  • 135,179
  • 13
  • 127
  • 222
  • Possible duplicate of [PHP with MySQL 8.0+ error: The server requested authentication method unknown to the client](https://stackoverflow.com/questions/52364415/php-with-mysql-8-0-error-the-server-requested-authentication-method-unknown-to) – LinPy Aug 20 '19 at 06:08
  • @LinPy - Thanks, but I don't think it is the same. Note that I already have `mysql_native_password` in the Grant table and my.cnf. Also, mysqlnd _seems_ to be able to handle sha2 (see phpinfo). – Rick James Aug 20 '19 at 06:12
  • you need to rebuild the container with `default-authentication-plugin` – LinPy Aug 20 '19 at 06:22
  • @LinPy - It is not a dup. The `ALTER` (Answers 1 and 3 of dup) has been done. The `[mysqld]` setting (Ans 2) has been done, – Rick James Aug 20 '19 at 23:57

1 Answers1

5

You've tried setting default-authentication-plugin in my.cnf - perhaps try to do it as a command arg instead? I had similar problems trying to auth, but this works for me in my docker-compose.yml - I never tried setting it in my.cnf

mysql:
    container_name: myapp-db
    image: mysql:8.0
    command: --default-authentication-plugin=mysql_native_password
    environment:
     (snip)

(command taken from the Docker docs page for MySQL).

vacri
  • 1,128
  • 7
  • 10
  • You are saying that the setting in the GRANTs and my.cnf are not sufficient? That sounds like a bug. – Rick James Aug 20 '19 at 22:09
  • I'm just saying what worked for me. The official mysql docker image has a launch script rather than running mysql directly - I haven't dug into it to see if there's some mechanism where it doesn't read the my.cnf file, sorry. The first thing I tried worked, so I moved on to my next problem... – vacri Aug 21 '19 at 07:48
  • I don't see it on https://hub.docker.com/r/mysql/mysql-server ; what page are you looking at? – Rick James Aug 21 '19 at 18:59
  • https://docs.docker.com/samples/library/mysql/ - pretty sure this was the page. The 'command' method is in there as a sample config item, but my hazy memory has the page content a little different (it was definitely on the docs.docker.com site, though) – vacri Aug 22 '19 at 00:19
  • It's irritating that installing the package from docker would not work as-is. I created a compose script similar to yours and got it to work. Thanks. – Rick James Aug 22 '19 at 02:47