简体   繁体   中英

How to create testing database under docker?

In Kubuntu 18 I create docker for laravel 6 app with mysql defined :

  mysql:
    container_name: "vanilla-crm-db"
    image: mysql:5.7
    environment:
      MYSQL_ROOT_PASSWORD: "MYSQL_ROOT_PASSWORD"
      MYSQL_DATABASE: "vanilla-crm-dev"
      MYSQL_USER: "MYSQL_USER"
      MYSQL_PASSWORD: "MYSQL_PASSWORD"
    ports:
      - "3330:3306"
    volumes:
      - "./docker/mysql/data:/var/lib/mysql"

and it works for me. I use MySql Workbench 6.3 for db access. Text I make http tests and I need to create new database for this and load dump of my database in it. Name of this database is wriiten in config/database.php under 'mysql_testing' block

I open Workbench and try to create new database : https://prnt.sc/unjie8 But I do not find “Create database” option, But I see “Create new schema” option and in sql-statement preview I see command

CREATE SCHEMA `vanilla-crm-testing` ;

I expected

Create database ...

command. Is it the same?

and error next:

Operation failed: There was an error while applying the SQL script to the database.
Executing:
CREATE SCHEMA `vanilla-crm-testing` ;

ERROR 1044: Access denied for user 'vanilla-crm-usr'@'%' to database 'vanilla-crm-testing'
SQL Statement:
CREATE SCHEMA `vanilla-crm-testing`

Which is valid way to create testing database?

UPDATED : I tried to create new database in mysql console, like:

mysql
CREATE DATABASE  vanilla-crm-testing;

but I got error in docker command line:

$ docker-compose exec app bash
root@09649d3a2b81:/app# mysql
bash: mysql: command not found

My docker app has in Dockerfile :

FROM php:7.3-apache
...

    RUN apt-get update && apt-get install --no-install-recommends -y \
      apt-utils ghostscript jq libicu-dev libmagick++-dev libpq-dev libfreetype6-dev libjpeg62-turbo-dev zlib1g-dev libzip-dev git zip && \
      docker-php-ext-install intl && \
      docker-php-ext-install opcache && \
      docker-php-ext-install pdo_mysql && \

and no more mysql commands. Are there some more packages I need to install in Dockerfile to have mysql console under docker?

UPDATED # 2: I enter the bash with command :

docker-compose exec mysql bash

root@f216ef80c104:/# uname -a 
Linux f216ef80c104 4.15.0-118-generic #119-Ubuntu SMP Tue Sep 8 12:30:01 UTC 2020 x86_64 GNU/Linux

Where mysql is container_name in docker-compose.yml

Usually I enter mysql console with command:

mysql -u root -h localhost -p

But which must be format of this command in the docker console? I tried several ways and failed...

UPDATED # 3: I installed DBeaver Version 7.2.1.202009201907 and logged into my database and tried to create new database for testing. I got error: https://prnt.sc/uol0z7

How to fix it ? Have I to add some more right my mysql container definitions?

Thanks!

To answer your question seems your main problem is you are trying to create a new test database but you are login with a non-root user. Non-root user has very little permission to do some operations. That's why you got that error message and can't create a new database. To solve this, try to log-in with the root user and make sure your Dbeaver config is correct:

host: 127.0.0.1
port: <Exposed MySQL port based on your docker-compose, e.g: 3330>
username: root
password: <MYSQL_ROOT_PASSWORD from your docker-compose>

With the root user, you should be able to create any new database. Another thing, if you wanted to connect via mysql cli command, make sure you also provide the correct port to the docker container. The command should be like this:

mysql -u root -h 127.0.0.1 -P <Exposed MySQL port based on your docker-compose, e.g: 3330> -p

Hope it helps and solved your problem. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM