Docker-compose installation db2 database operation

Docker-compose installation db2 database operation

It is troublesome to install the db2 database directly on the host machine, and it is inconvenient to involve users and permissions, so docker is used to install the db2 database. The advantage of this is that it facilitates database management and network isolation. Docker is generally run directly on the Internet, which is not convenient for later modification. We still use the docker-compose file to install the db2 database.

1. Write the docker-compose file to automatically download the database image

Just create a folder for this file.

version: "2.2"
services:
 db2:
 image: ibmcom/db2
 container_name: db211.5
 privileged: true
 environment:
 LICENSE: accept
 DB2INST1_PASSWORD: your password DBNAME: TESTDB
 volumes:
 - ./db2data:/database
 ports:
 - 50000:50000

2. Check the installation log and wait for DB2 initialization to complete, which may take several minutes

#Execute docker-compose to start the installation docker-compose up -d
#View the installation log docker-compose logs
#It will be installed almost when TESTDB is created

3. Enter the db2 database container to create your own database

#Enter the container docker exec -it db211.5 bash
#Switch to db2inst1 user su db2inst1
#Check if there is our TESTDB database db2 list db directory

4. Create a user and grant permissions

Other permissions can be found online. Here is a simple connection and add, delete, modify and check permissions.

#Create user group groupadd db2group
#Add the user to the group useradd -m -g db2group -d /home/test test
#Change the test password to passwd test
#Enter the password twice in succession #Switch to the db2inst1 user and grant connection permissions to test su db2inst1
#Connect to database db2 connect to testdb
#Grant connection permissions db2 grant connect on database to user test
#Give permissions for adding, deleting, modifying and checking db2 grant DATAACCESS on database to user test
#Close the connection db2 connect reset
#Other common commands#Create a database db2 create db TEST using codeset utf-8 territory CN
#View all current databases db2 list db directory
#View the table names in the library db2 list tables
#More commands omitted

5. DB2 export and import operation commands

#db2 export command (many files will appear, package them all)
db2move <your databases> export
#db2 import command (copy the packaged file to the server you want to import, and then execute the following command in the folder directory)
db2move <your databases> import
#If there is a permission problem, use the root user to grant writable permissions to the folder, because the import command will create an import.out file

At this point, the installation of docker for db2 is completed. The advantage of using docker containers is that they can be seamlessly migrated between servers, making it very convenient to migrate databases to other servers in the future.

Additional knowledge: Install db2 in docker and mount it locally

1. Find all db2 images

docker pull ibmcom/db2

2. Pull the image

I use the latest version of the image here. If you don't need the latest version, you can specify the version according to your own situation. Method: docker pull db2:11.5.4.0

docker pull ibmcom/db2

3. Start the container and mount

docker run -d -p 50001:50000 --name db2_50001 --privileged=true -e DB2INST1_PASSWORD=123456 -e DBNAME=testdb -e LICENSE=accept -v /data/tadopDataProject/db2/50001:/database ibmcom/db2

Parameter Description:

-d: means starting the container in the background;

-p 50001:50000: The 50000 port in the container is mapped to the 50000 port on the host;

--name db2_50001: Name the container db2_50001

--privileged=true: enables the root in the container to have real root permissions.

-e DB2INST1_PASSWORD=123456: Set the password of the built-in instance user db2inst1 to 123456

-e DBNAME=testdb: A database named testdb is automatically created when the container starts. If this parameter is not specified, no database is created.

-e LICENSE=accept: accept the agreement

-v /data/tadopDataProject/db2/50001:/database: mount directory, where /data/tadopDataProject/db2/50001 is the directory of the host machine

4. Check whether the startup is successful

docker ps

netstat -anp |grep 50001

5. Enter the container and execute the db2 command

docker exec -it db2_50001 bash

Execute the following command to switch to the instance user db2inst1:

Note: Be sure to write the middle horizontal bar (-).

su -db2inst1

Check the running status:

db2pd -

View the database and patch versions:

db2level

View the created database:

db2 list db directory

Execute the command to connect to the testdb database:

db2 connect to testdb

Create a table called TEST:

Note: The SQL statement following the db2 command needs to be enclosed in quotation marks, otherwise the error -bash: syntax error near unexpected token `(' will be reported.

db2 "create table TEST(ID BIGINT NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1,INCREMENT BY 1),USER_NAME VARCHAR(20),USER_AGES INT)"

View all user tables:

db2 list tables

We can also execute the following command to create a SAMPLE database (sample database):

db2sampl

Execute the db2 list dbdirectory command again to check whether the creation is successful.

Finally, execute exit to exit the container and return to the host machine.

Connect to the testdb database

The above docker-compose installation db2 database operation is all the content that the editor shares with you. I hope it can give you a reference, and I also hope that you will support 123WORDPRESS.COM.

You may also be interested in:
  • Detailed tutorial on installing harbor private warehouse using docker compose
  • Docker-compose tutorial installation and quick start
  • Detailed installation and use of docker-compose
  • Two simplest ways to install docker-compose
  • Detailed steps for installing and setting up Docker-compose
  • Teach you the detailed process of installing DOClever with Docker Compose

<<:  XHTML tutorial, a brief introduction to the basics of XHTML

>>:  js to create a carousel effect

Recommend

Detailed steps for implementing timeout status monitoring in Apache FlinkCEP

CEP - Complex Event Processing. The payment has n...

Using JS to implement a simple calculator

Use JS to complete a simple calculator for your r...

Pure CSS to achieve a single div regular polygon transformation

In the previous article, we introduced how to use...

Summary of various methods for Vue to achieve dynamic styles

Table of contents 1. Ternary operator judgment 2....

Specific usage of CSS compound selectors

Intersection Selector The intersection selector i...

Use of Linux passwd command

1. Command Introduction The passwd command is use...

37 Tips for a Good User Interface Design (with Pictures)

1. Try to use single column instead of multi-colum...

Example code for implementing 3D text hover effect using CSS3

This article introduces the sample code of CSS3 t...

MySQL index principle and usage example analysis

This article uses examples to illustrate the prin...

React's component collaborative use implementation

Table of contents Nesting Parent-child component ...

Using MySQL database with Python 3.4 under Windows 7

The detailed process of using MySQL database with...

Docker-compose installation yml file configuration method

Table of contents 1. Offline installation 2. Onli...

How to maintain a long connection when using nginx reverse proxy

· 【Scene description】 After HTTP1.1, the HTTP pro...

4 solutions to CSS browser compatibility issues

Front-end is a tough job, not only because techno...