postgres和mysql基于docker搭建以及phppgadmin和phpmyadmin的应用(个人备份用非教程)

Posted _less is more

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了postgres和mysql基于docker搭建以及phppgadmin和phpmyadmin的应用(个人备份用非教程)相关的知识,希望对你有一定的参考价值。

postgres

1、docker-compose文件

# Specify the version for docker-compose.yml
version: "3.8"

# add the serivces needed (postgres,phppgadmin)
services:
  postgres:
    container_name: postgres
    image: postgres:latest
    environment:
      POSTGRES_USER: user
      POSTGRES_PASSWORD: mypassword
      # Optional: Give a name to the database, otherwise
      # use the default value POSTGRES_USER as a database name (user in this case.)
      # POSTGRES_DB: testdb
    ports:
      - "5432:5432"
    volumes:
      - dbdata:/var/lib/postgresql/data

  phppgadmin:
    container_name: phppgadmin
    image: neimheadh/phppgadmin:latest
    ports:
      - "8080:80" # web browsers block the port 100080. Use 8080 instead.
    environment:
      - POSTGRES_HOST=postgres
      - POSTGRES_PORT=5432
      - POSTGRES_USER=user
      - POSTGRES_PASS=mypassword

volumes:
  dbdata:

2、Running the containers

docker-compose up -d

3、Enter iterative mode

docker exec -it postgres psql -U user

4、Some useful commands

# show tables from current schema
\\dt
# list tables from all schemas
\\dt *.*
# show table
\\d TABLE_NAME
# show databases
\\l

5、Create table and insert content

CREATE TABLE COMPANY (ID INT PRIMARY KEY NOT NULL, NAME text);
INSERT INTO company(id,name) values (1,'test');
SELECT * from company;

6、Go to phpPgAdmin

http://localhost:8080/

7、Go back to terminal. Simple SQL queries

# NOTE: when doing queries, uncheck the paginate results
# create a new db
create database employee_database;
# create table
create table employee (
 last_name varchar(150),
 department_id int,
 primary key (last_name)
);
# insert content
insert into employee (last_name, department_id) values
('Rafferty', 31),
('Jones', 33),
('Steinberg', 33),
('Robinson', 34),
('Smith', 34);
# creat another table
create table department (
 department_id int,
 department_name varchar(150),
 primary key ( department_name )
);
# insert content
insert into department (department_id, department_name) values
(31, 'Sales'),
(33, 'Engineering'),
(34, 'Clerical'),
(35, 'Marketing');
# visualizing tables
select *
from employee;
select *
from department;
# single relation query
select department_name
from department dept
where dept.department_id = 33;
# join, (must specify ON in postgres)
# select * from employee emp join department dept
select * from employee emp join department dept ON true
# join with filtering
select *
from employee emp join department dept
on emp.department_id=dept.department_id
# delete (rows)
delete from employee
where last_name='Jones';
# update
update employee
set department_id=37
where last_name = 'Robinson';
# alter table command, add column, drop column
alter table employee
add salary real null;
update employee
set salary = 5000
where department_id = 33;
update employee
set salary = 6000
where department_id = 31;
update employee
set salary = 7000
where department_id = 34;
# Useful arithmetic functions
select avg(salary)
from employee;
# drop column
alter table employee
drop column department_id;

8、Shutdown

docker-compose down

mysql

1、docker-compose文件

version: "3.8"

services: # adds 2 services: mysql and phpmyadmin to connect with
  db:
    image: mysql:latest # use latest version of mysql
    container_name: db # add a name for the container
    command: --default-authentication-plugin=mysql_native_password
    restart: unless-stopped
    environment: # add default values, see docs for more info.
      MYSQL_USER: user
      MYSQL_ROOT_PASSWORD: mypassword
      MYSQL_PASSWORD: mypassword
      MYSQL_DATABASE: testdb # create this database on startup
    volumes:
      - my-db:/var/lib/mysql
    ports:
      - '3306:3306'
  
  phpmyadmin:
    container_name: phpmyadmin
    image: phpmyadmin/phpmyadmin:latest
    ports:
     - "8082:80"
    environment:
      MYSQL_ROOT_PASSWORD:  #(Required) set the password for the root superuser account.
      PMA_HOST: db   # define the address/hostname of the mysql server eg mysql container name.
      PMA_USER: root # this is the root user to login on startup
      PMA_PASSWORD: mypassword # use the root password to login on startup.

volumes: # add persistent data even if container is removed.
  my-db:

2、Go to phpMyAdmin

http://localhost:8082/

3、Create database and the schema

CREATE DATABASE freedbtech_language

CREATE TABLE programming_languages
(
 id INT(11) NOT NULL auto_increment ,
 name VARCHAR(255) NOT NULL ,
 released_year INT NULL ,
 githut_rank INT NULL ,
 pypl_rank INT NULL ,
 tiobe_rank INT NULL ,
 created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ,
 updated_at DATETIME on UPDATE CURRENT_TIMESTAMP NOT NULL DEFAULT 
CURRENT_TIMESTAMP ,
 PRIMARY KEY (id),
 UNIQUE idx_name_unique (name(255))
)
engine = innodb charset=utf8mb4 COLLATE utf8mb4_general_ci;

4、Insert some data into the table

INSERT INTO 
programming_languages(id,name,released_year,githut_rank,pypl_rank,tiobe_rank) 
VALUES 
(1,'javascript',1995,1,3,7),
(2,'Python',1991,2,1,3),
(3,'Java',1995,3,2,2),
(4,'TypeScript',2012,7,10,42),
(5,'C#',2000,9,4,5),
(6,'PHP',1995,8,6,8),
(7,'C++',1985,5,5,4),
(8,'C',1972,10,5,1),
(9,'Ruby',1995,6,15,15),
(10,'R',1993,33,7,9),
(11,'Objective-C',1984,18,8,18),
(12,'Swift',2015,16,9,13),
(13,'Kotlin',2011,15,12,40),
(14,'Go',2009,4,13,14),
(15,'Rust',2010,14,16,26),
(16,'Scala',2004,11,17,34);

以上是关于postgres和mysql基于docker搭建以及phppgadmin和phpmyadmin的应用(个人备份用非教程)的主要内容,如果未能解决你的问题,请参考以下文章

基于Docker快速搭建wordpress博客

基于Postgres xl的分布式数据库实验环境搭建

基于Postgres xl的分布式数据库实验环境搭建

基于Postgres xl的分布式数据库实验环境搭建

如何 在Docker窗口中部署PHP开发环境

基于Docker的Mysql主从复制搭建