Showing posts with label MySQL Workbench. Show all posts
Showing posts with label MySQL Workbench. Show all posts

Monday, 17 January 2022

How to run MySQL Docker container

Docker hub contains MySQL images. To run the container with the latest version of MySQL on the local machine:
 
$ docker run \
--name my-test-mysql \
-p 3306:3306 \
-e MYSQL_ROOT_PASSWORD=root \
-d mysql:latest

02179364ced0e65912ab0d23dd8451e42d9e4ca679507abe711a86d0ac564219

We are here naming this container my-test-mysql, opening default MySQL port 3306 on container (so we can connect to DB from outside the container), set the password (e.g. root) for the default MySQL user (root) and making a choice which MySQL version to run (the latest).
 
To check that this container is running:
 
$ docker container ls
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                  PORTS                                            NAMES
...
02179364ced0        mysql:latest        "docker-entrypoint.s…"   3 days ago          Up 3 days               0.0.0.0:3306->3306/tcp, 33060/tcp                my-test-mysql
...


---

If for some reason container won't run (it starts and then stops), the reason can be found in docker logs for that container:
 
$ docker container logs my-test-mysql
 
NOTE: make sure that container is preserved after docker run command - don't run it with --rm option.
---
 
 
We can test connectivity to this DB via MySQL workbench:
 
 
 
 

Setting MySQL configuration when creating the container

 
The default configuration for MySQL can be found in /etc/mysql/my.cnf, which may !includedir additional directories such as /etc/mysql/conf.d or /etc/mysql/mysql.conf.d.[source]

 
It is possible to set a variable in MySQL configuration file when starting the container. To see the full list of such variables we need to run:
 
$ docker run -it --rm mysql:latest --verbose --help
mysqld  Ver 8.0.27 for Linux on x86_64 (MySQL Community Server - GPL)
Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Starts the MySQL database server.

Usage: mysqld [OPTIONS]

Default options are read from the following files in the given order:
/etc/my.cnf /etc/mysql/my.cnf ~/.my.cnf
The following groups are read: mysqld server mysqld-8.0
The following options may be given as the first argument:
--print-defaults        Print the program argument list and exit.
--no-defaults           Don't read default options from any option file,
                        except for login file.
...

Variables (--variable-name=value)
and boolean options {FALSE|TRUE}                             Value (after reading options)
------------------------------------------------------------ -------------
abort-slave-event-count                                      0
activate-all-roles-on-login                                  FALSE
...
sql-mode                                                     ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
...
windowing-use-high-precision                                 TRUE

To see what values a running MySQL server is using, type
'mysqladmin variables' instead of 'mysqld --verbose --help'.



Let's say we want to set sql_mode in order to suppress error invalid default value when setting '0000-00-00 00:00:00' as default value for column with datetime type. By default NO_ZERO_IN_DATE,NO_ZERO_DATE are included in sql_mode value in my.cnf but we want to exlude them:

sql_mode = "ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION"
 
To set this variable when running the container:

$ docker run \
--name wp-admin-mysql \
-p 3306:3306 \
-e MYSQL_ROOT_PASSWORD=root \
-d mysql:latest \
--sql-mode="ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION"

To verify that the value is correctly set we can connect to DB and execute the following command:

SELECT @@GLOBAL.sql_mode;



 

DB Config and Data Persistence



Each time we run the MySQL container it sets up DB from scratch. Once container is stopped all configurations, schemas, tables and data are lost forever. To persist this data between MySQL container sessions we can use:
  • Docker volume (preferred way)
  • directory on the host

Data Persistence with named Docker volume


Named Docker volume is like a blob that can be attached to a container and where container persists its data. 
 
We first need to create it:

$ docker volume create mysql-volume
mysql-volume

 
After it's created it will be listed among other Docker volumes:
 
$ docker volume ls
DRIVER              VOLUME NAME
local               mysql-volume



We can now mount it to /var/lib/mysql inside the container, where MySQL by default will write its data files: -v data-volume:/var/lib/mysql


$ docker run \
--name my-test-mysql \
--rm \
-p 3306:3306 \
-e MYSQL_ROOT_PASSWORD=root \
-v mysql-volume:/var/lib/mysql \ 
-d mysql:latest \
--sql-mode="ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION"

Any change on the database from now will be persisted inside the volume and each subsequent run of MySQL database (from another MySQL container) will pick it up.

Stopping the container


$ docker container stop my-test-mysql
my-test-mysql

References:

Connect to MySQL running in Docker container from a local machine | by Md Kamaruzzaman | Towards Data Science

Friday, 12 March 2021

How to install MySQL Workbench on Ubuntu

Ubuntu 20.04 in my case.

We first need to download the deb package from the Oracle website:


We can choose package which does not contain debugging information files (dbgsym).

Read How to check file GPG signature on Linux | My Public Notepad and don't skip that step! Now we can try to install the package:

$ sudo dpkg -i mysql-workbench-community_8.0.23-1ubuntu20.04_amd64.deb 
Selecting previously unselected package mysql-workbench-community.
(Reading database ... 304721 files and directories currently installed.)
Preparing to unpack mysql-workbench-community_8.0.23-1ubuntu20.04_amd64.deb ...
Unpacking mysql-workbench-community (8.0.23-1ubuntu20.04) ...
dpkg: dependency problems prevent configuration of mysql-workbench-community:
 mysql-workbench-community depends on libzip5 (>= 0.10); however:
  Package libzip5 is not installed.

dpkg: error processing package mysql-workbench-community (--install):
 dependency problems - leaving unconfigured
Processing triggers for gnome-menus (3.36.0-1ubuntu1) ...
Processing triggers for desktop-file-utils (0.24-1ubuntu3) ...
Processing triggers for mime-support (3.64ubuntu1) ...
Processing triggers for hicolor-icon-theme (0.17-2) ...
Processing triggers for shared-mime-info (1.15-1) ...
Errors were encountered while processing:
 mysql-workbench-community


To fix the dependencies:

sudo apt --fix-broken install
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Correcting dependencies... Done
The following additional packages will be installed:
  libzip5
The following NEW packages will be installed
  libzip5
0 to upgrade, 1 to newly install, 0 to remove and 38 not to upgrade.
1 not fully installed or removed.
Need to get 46.7 kB of archives.
After this operation, 134 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://gb.archive.ubuntu.com/ubuntu focal/universe amd64 libzip5 amd64 1.5.1-0ubuntu1 [46.7 kB]
Fetched 46.7 kB in 1s (75.4 kB/s)
Selecting previously unselected package libzip5:amd64.
(Reading database ... 306005 files and directories currently installed.)
Preparing to unpack .../libzip5_1.5.1-0ubuntu1_amd64.deb ...
Unpacking libzip5:amd64 (1.5.1-0ubuntu1) ...
Setting up libzip5:amd64 (1.5.1-0ubuntu1) ...
Setting up mysql-workbench-community (8.0.23-1ubuntu20.04) ...
Processing triggers for libc-bin (2.31-0ubuntu9.2) ...

Installation is now successful:

$ sudo dpkg -i mysql-workbench-community_8.0.23-1ubuntu20.04_amd64.deb 
(Reading database ... 306011 files and directories currently installed.)
Preparing to unpack mysql-workbench-community_8.0.23-1ubuntu20.04_amd64.deb ...
Unpacking mysql-workbench-community (8.0.23-1ubuntu20.04) over (8.0.23-1ubuntu20.04) ...
Setting up mysql-workbench-community (8.0.23-1ubuntu20.04) ...
Processing triggers for gnome-menus (3.36.0-1ubuntu1) ...
Processing triggers for desktop-file-utils (0.24-1ubuntu3) ...
Processing triggers for mime-support (3.64ubuntu1) ...
Processing triggers for hicolor-icon-theme (0.17-2) ...
Processing triggers for shared-mime-info (1.15-1) ...

Let's try to run it:

$ mysql-workbench --version
Workbench can't find libproj.so, some options may be unavailable.
MySQL Workbench CE (GPL) 8.0.23 CE build 365764

For this warning, see here: MySQL Workbench not opening on ubuntu - Stack Overflow

That library is not present on my machine:

$ sudo find / -name libproj.so

But as one comment said, it is optional so the app worked fine:


Updating Workbench to the latest version


I had version 8.0.23 and check for updates returned 8.0.27. I downloaded the deb file and procedure was the same as for first installation:

$ sudo dpkg -i mysql-workbench-community_8.0.27-1ubuntu20.04_amd64.deb
(Reading database ... 304933 files and directories currently installed.)
Preparing to unpack mysql-workbench-community_8.0.27-1ubuntu20.04_amd64.deb ...
Unpacking mysql-workbench-community (8.0.27-1ubuntu20.04) over (8.0.23-1ubuntu20.04) ...
dpkg: dependency problems prevent configuration of mysql-workbench-community:
 mysql-workbench-community depends on libproj15 (>= 6.3.0); however:
  Package libproj15 is not installed.

dpkg: error processing package mysql-workbench-community (--install):
 dependency problems - leaving unconfigured

Processing triggers for gnome-menus (3.36.0-1ubuntu1) ...
Processing triggers for desktop-file-utils (0.24-1ubuntu3) ...
Processing triggers for mime-support (3.64ubuntu1) ...
Processing triggers for hicolor-icon-theme (0.17-2) ...
Processing triggers for shared-mime-info (1.15-1) ...
Errors were encountered while processing:
 mysql-workbench-community

 
$ sudo apt --fix-broken install
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Correcting dependencies... Done
The following additional packages will be installed:
  libproj15 proj-data
Suggested packages:
  proj-bin
The following NEW packages will be installed
  libproj15 proj-data
0 to upgrade, 2 to newly install, 0 to remove and 0 not to upgrade.
1 not fully installed or removed.
Need to get 8,572 kB of archives.
After this operation, 27.5 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://gb.archive.ubuntu.com/ubuntu focal/universe amd64 proj-data all 6.3.1-1 [7,647 kB]
Get:2 http://gb.archive.ubuntu.com/ubuntu focal/universe amd64 libproj15 amd64 6.3.1-1 [925 kB]
Fetched 8,572 kB in 4s (2,323 kB/s)
Selecting previously unselected package proj-data.
(Reading database ... 304934 files and directories currently installed.)
Preparing to unpack .../proj-data_6.3.1-1_all.deb ...
Unpacking proj-data (6.3.1-1) ...
Selecting previously unselected package libproj15:amd64.
Preparing to unpack .../libproj15_6.3.1-1_amd64.deb ...
Unpacking libproj15:amd64 (6.3.1-1) ...
Setting up proj-data (6.3.1-1) ...
Setting up libproj15:amd64 (6.3.1-1) ...
Setting up mysql-workbench-community (8.0.27-1ubuntu20.04) ...
Processing triggers for libc-bin (2.31-0ubuntu9.2) ...
 
$ sudo dpkg -i mysql-workbench-community_8.0.27-1ubuntu20.04_amd64.deb
(Reading database ... 304985 files and directories currently installed.)
Preparing to unpack mysql-workbench-community_8.0.27-1ubuntu20.04_amd64.deb ...
Unpacking mysql-workbench-community (8.0.27-1ubuntu20.04) over (8.0.27-1ubuntu20.04) ...
Setting up mysql-workbench-community (8.0.27-1ubuntu20.04) ...
Processing triggers for gnome-menus (3.36.0-1ubuntu1) ...
Processing triggers for desktop-file-utils (0.24-1ubuntu3) ...
Processing triggers for mime-support (3.64ubuntu1) ...
Processing triggers for hicolor-icon-theme (0.17-2) ...
Processing triggers for shared-mime-info (1.15-1) ...

 

Checking for the updates now shows that no new updates are available: