Заработало
parent
1b2833d38b
commit
e727e7e7e7
12
Makefile
12
Makefile
|
@ -1,14 +1,18 @@
|
||||||
cdir = $(shell pwd)
|
cdir = $(shell pwd)
|
||||||
|
|
||||||
up:
|
up:
|
||||||
sudo -S docker-compose -f ./cicd/docker-compose.yml up -d --build
|
docker-compose -f ./cicd/docker-compose.yml up -d --build
|
||||||
|
./cicd/init.sh
|
||||||
down: shutdown clean
|
down: shutdown clean
|
||||||
|
|
||||||
shutdown:
|
shutdown:
|
||||||
sudo -S docker-compose -f ./cicd/docker-compose.yml down
|
docker-compose -f ./cicd/docker-compose.yml down
|
||||||
|
sudo docker rmi $$(sudo docker images -a | grep '<none>' | awk '{print $$3}')
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
sudo docker rmi $(sudo docker images | grep '<none>' | awk '{print $3}')
|
rm -rf /opt/mysql_master/* ; \
|
||||||
|
rm -rf /opt/mysql_slave1/* ; \
|
||||||
|
rm -rf /opt/mysql_slave2/* ; \
|
||||||
|
sudo docker rmi $$(sudo docker images -a | grep '<none>' | awk '{print $$3}')
|
||||||
|
|
||||||
.PHONY: up down
|
.PHONY: up down
|
|
@ -1,2 +0,0 @@
|
||||||
CREATE DATABASE IF NOT EXISTS `app` CHARACTER SET utf8 COLLATE utf8_general_ci;
|
|
||||||
GRANT ALL ON `app`.* TO 'app'@'%' identified by 'app';
|
|
|
@ -8,38 +8,41 @@ services:
|
||||||
restart: always
|
restart: always
|
||||||
environment:
|
environment:
|
||||||
MYSQL_ROOT_PASSWORD: root
|
MYSQL_ROOT_PASSWORD: root
|
||||||
MYSQL_USER: app
|
|
||||||
MYSQL_PASSWORD: app
|
|
||||||
MYSQL_DATABASE: app
|
|
||||||
MYSQL_PORT: 3306
|
MYSQL_PORT: 3306
|
||||||
MYSQL_LOWER_CASE_TABLE_NAMES: 0
|
MYSQL_LOWER_CASE_TABLE_NAMES: 0
|
||||||
ports:
|
|
||||||
- 13306:3306
|
|
||||||
volumes:
|
volumes:
|
||||||
- ./mysql/mysql_master.conf:/etc/mysql/conf.d/mysql.conf.cnf
|
- ./mysql/mysql_master.conf:/etc/mysql/conf.d/mysql.conf.cnf
|
||||||
- /opt/mysql_master:/var/lib/mysql
|
- /opt/mysql_master:/var/lib/mysql
|
||||||
- ./mysql/master_init:/docker-entrypoint-initdb.d
|
|
||||||
|
|
||||||
mysql_slave:
|
mysql_slave1:
|
||||||
image: mysql:5.7
|
image: mysql:5.7
|
||||||
hostname: "mysql_slave"
|
hostname: "mysql_slave1"
|
||||||
container_name: mysql_slave
|
container_name: mysql_slave1
|
||||||
restart: always
|
restart: always
|
||||||
environment:
|
environment:
|
||||||
MYSQL_ROOT_PASSWORD: root
|
MYSQL_ROOT_PASSWORD: root
|
||||||
MYSQL_USER: app
|
|
||||||
MYSQL_PASSWORD: app
|
|
||||||
MYSQL_DATABASE: app
|
|
||||||
MYSQL_PORT: 3306
|
MYSQL_PORT: 3306
|
||||||
MYSQL_LOWER_CASE_TABLE_NAMES: 0
|
MYSQL_LOWER_CASE_TABLE_NAMES: 0
|
||||||
ports:
|
|
||||||
- 23306:3306
|
|
||||||
depends_on:
|
depends_on:
|
||||||
- mysql_master
|
- mysql_master
|
||||||
volumes:
|
volumes:
|
||||||
- ./mysql/mysql_slave.conf:/etc/mysql/conf.d/mysql.conf.cnf
|
- ./mysql/mysql_slave1.conf:/etc/mysql/conf.d/mysql.conf.cnf
|
||||||
- /opt/mysql_slave:/var/lib/mysql
|
- /opt/mysql_slave1:/var/lib/mysql
|
||||||
- ./mysql/slave_init:/docker-entrypoint-initdb.d
|
|
||||||
|
mysql_slave2:
|
||||||
|
image: mysql:5.7
|
||||||
|
hostname: "mysql_slave2"
|
||||||
|
container_name: mysql_slave2
|
||||||
|
restart: always
|
||||||
|
environment:
|
||||||
|
MYSQL_ROOT_PASSWORD: root
|
||||||
|
MYSQL_PORT: 3306
|
||||||
|
MYSQL_LOWER_CASE_TABLE_NAMES: 0
|
||||||
|
depends_on:
|
||||||
|
- mysql_master
|
||||||
|
volumes:
|
||||||
|
- ./mysql/mysql_slave2.conf:/etc/mysql/conf.d/mysql.conf.cnf
|
||||||
|
- /opt/mysql_slave2:/var/lib/mysql
|
||||||
|
|
||||||
app:
|
app:
|
||||||
build:
|
build:
|
||||||
|
@ -50,7 +53,7 @@ services:
|
||||||
APP_SERVER_ADDRESS: 0.0.0.0
|
APP_SERVER_ADDRESS: 0.0.0.0
|
||||||
APP_SERVER_PORT: 8080
|
APP_SERVER_PORT: 8080
|
||||||
APP_DSN_HOST: mysql_master
|
APP_DSN_HOST: mysql_master
|
||||||
APP_DSN_PORT: 13306
|
APP_DSN_PORT: 3306
|
||||||
APP_DSN_USER: app
|
APP_DSN_USER: app
|
||||||
APP_DSN_PASS: app
|
APP_DSN_PASS: app
|
||||||
APP_DSN_BASE: app
|
APP_DSN_BASE: app
|
||||||
|
|
|
@ -1,59 +1,45 @@
|
||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
mysql:
|
until docker exec mysql_master sh -c 'export MYSQL_PWD=root; mysql -u root -e ";"'
|
||||||
image: mysql:5.6
|
|
||||||
hostname: "mysql"
|
|
||||||
container_name: mysql
|
|
||||||
command: --default-authentication-plugin=mysql_native_password
|
|
||||||
restart: always
|
|
||||||
environment:
|
|
||||||
MYSQL_ROOT_PASSWORD: root
|
|
||||||
MYSQL_DATABASE: app
|
|
||||||
MYSQL_USER: app
|
|
||||||
MYSQL_PASSWORD: app
|
|
||||||
volumes:
|
|
||||||
- /opt/mysql:/var/lib/mysql
|
|
||||||
- ./dbinit:/docker-entrypoint-initdb.d
|
|
||||||
ports:
|
|
||||||
- "3306:3306"
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
docker-compose down
|
|
||||||
rm -rf ./master/data/*
|
|
||||||
rm -rf ./slave/data/*
|
|
||||||
docker-compose build
|
|
||||||
docker-compose up -d
|
|
||||||
|
|
||||||
until docker exec mysql_master sh -c 'export MYSQL_PWD=111; mysql -u root -e ";"'
|
|
||||||
do
|
do
|
||||||
echo "Waiting for mysql_master database connection..."
|
echo "Waiting for mysql_master database connection..."
|
||||||
sleep 4
|
sleep 4
|
||||||
done
|
done
|
||||||
|
|
||||||
priv_stmt='GRANT REPLICATION SLAVE ON *.* TO "mydb_slave_user"@"%" IDENTIFIED BY "mydb_slave_pwd"; FLUSH PRIVILEGES;'
|
priv_stmt='CREATE DATABASE IF NOT EXISTS app CHARACTER SET utf8 COLLATE utf8_general_ci; GRANT ALL ON app.* TO "app"@"%" IDENTIFIED BY "app"; GRANT REPLICATION SLAVE ON *.* TO "mydb_slave_user"@"%" IDENTIFIED BY "mydb_slave_pwd"; FLUSH PRIVILEGES;'
|
||||||
docker exec mysql_master sh -c "export MYSQL_PWD=111; mysql -u root -e '$priv_stmt'"
|
docker exec mysql_master sh -c "export MYSQL_PWD=root; mysql -u root -e '$priv_stmt'"
|
||||||
|
|
||||||
until docker-compose exec mysql_slave sh -c 'export MYSQL_PWD=111; mysql -u root -e ";"'
|
until docker exec mysql_slave1 sh -c 'export MYSQL_PWD=root; mysql -u root -e ";"'
|
||||||
do
|
do
|
||||||
echo "Waiting for mysql_slave database connection..."
|
echo "Waiting for mysql_slave1 database connection..."
|
||||||
sleep 4
|
sleep 4
|
||||||
done
|
done
|
||||||
|
|
||||||
|
until docker exec mysql_slave2 sh -c 'export MYSQL_PWD=root; mysql -u root -e ";"'
|
||||||
|
do
|
||||||
|
echo "Waiting for mysql_slave2 database connection..."
|
||||||
|
sleep 4
|
||||||
|
done
|
||||||
|
|
||||||
|
priv_stmt='CREATE DATABASE IF NOT EXISTS app CHARACTER SET utf8 COLLATE utf8_general_ci; GRANT ALL ON app.* TO "app"@"%" IDENTIFIED BY "app"; FLUSH PRIVILEGES;'
|
||||||
|
docker exec mysql_slave1 sh -c "export MYSQL_PWD=root; mysql -u root -e '$priv_stmt'"
|
||||||
|
docker exec mysql_slave2 sh -c "export MYSQL_PWD=root; mysql -u root -e '$priv_stmt'"
|
||||||
|
|
||||||
docker-ip() {
|
docker-ip() {
|
||||||
docker inspect --format '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' "$@"
|
docker inspect --format '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' "$@"
|
||||||
}
|
}
|
||||||
|
|
||||||
MS_STATUS=`docker exec mysql_master sh -c 'export MYSQL_PWD=111; mysql -u root -e "SHOW MASTER STATUS"'`
|
MS_STATUS=`docker exec mysql_master sh -c 'export MYSQL_PWD=root; mysql -u root -e "SHOW MASTER STATUS"' | grep mysq`
|
||||||
CURRENT_LOG=`echo $MS_STATUS | awk '{print $6}'`
|
CURRENT_LOG=`echo $MS_STATUS | awk '{print $1}'`
|
||||||
CURRENT_POS=`echo $MS_STATUS | awk '{print $7}'`
|
CURRENT_POS=`echo $MS_STATUS | awk '{print $2}'`
|
||||||
|
|
||||||
start_slave_stmt="CHANGE MASTER TO MASTER_HOST='$(docker-ip mysql_master)',MASTER_USER='mydb_slave_user',MASTER_PASSWORD='mydb_slave_pwd',MASTER_LOG_FILE='$CURRENT_LOG',MASTER_LOG_POS=$CURRENT_POS; START SLAVE;"
|
start_slave_stmt="CHANGE MASTER TO MASTER_HOST='$(docker-ip mysql_master)',MASTER_USER='mydb_slave_user',MASTER_PASSWORD='mydb_slave_pwd',MASTER_LOG_FILE='$CURRENT_LOG',MASTER_LOG_POS=$CURRENT_POS; START SLAVE;"
|
||||||
start_slave_cmd='export MYSQL_PWD=111; mysql -u root -e "'
|
start_slave_cmd='export MYSQL_PWD=root; mysql -u root -e "'
|
||||||
start_slave_cmd+="$start_slave_stmt"
|
start_slave_cmd+="$start_slave_stmt"
|
||||||
start_slave_cmd+='"'
|
start_slave_cmd+='"'
|
||||||
docker exec mysql_slave sh -c "$start_slave_cmd"
|
|
||||||
|
|
||||||
docker exec mysql_slave sh -c "export MYSQL_PWD=111; mysql -u root -e 'SHOW SLAVE STATUS \G'"
|
docker exec mysql_slave1 sh -c "$start_slave_cmd"
|
||||||
|
docker exec mysql_slave1 sh -c "export MYSQL_PWD=root; mysql -u root -e 'SHOW SLAVE STATUS \G' | grep Slave_"
|
||||||
|
|
||||||
|
docker exec mysql_slave2 sh -c "$start_slave_cmd"
|
||||||
|
docker exec mysql_slave2 sh -c "export MYSQL_PWD=root; mysql -u root -e 'SHOW SLAVE STATUS \G' | grep Slave_"
|
|
@ -1,3 +0,0 @@
|
||||||
CREATE DATABASE IF NOT EXISTS `app` CHARACTER SET utf8 COLLATE utf8_general_ci;
|
|
||||||
GRANT ALL ON `app`.* TO 'app'@'%' identified by 'app';
|
|
||||||
GRANT REPLICATION SLAVE ON *.* TO 'app'@'%' identified by 'app';
|
|
|
@ -5,5 +5,5 @@ skip-name-resolve
|
||||||
|
|
||||||
server-id = 1
|
server-id = 1
|
||||||
log_bin = /var/log/mysql/mysql-bin.log
|
log_bin = /var/log/mysql/mysql-bin.log
|
||||||
binlog_format = ROW
|
# binlog_format = ROW
|
||||||
binlog_do_db = mydb
|
binlog_do_db = app
|
|
@ -3,7 +3,7 @@
|
||||||
skip-host-cache
|
skip-host-cache
|
||||||
skip-name-resolve
|
skip-name-resolve
|
||||||
|
|
||||||
server-id=2
|
server-id = 2
|
||||||
relay-log = /var/log/mysql/mysql-relay-bin.log
|
|
||||||
log_bin = /var/log/mysql/mysql-bin.log
|
log_bin = /var/log/mysql/mysql-bin.log
|
||||||
binlog_do_db = mydb
|
relay-log = /var/log/mysql/mysql-relay-bin.log
|
||||||
|
binlog_do_db = app
|
|
@ -0,0 +1,9 @@
|
||||||
|
[mysqld]
|
||||||
|
|
||||||
|
skip-host-cache
|
||||||
|
skip-name-resolve
|
||||||
|
|
||||||
|
server-id = 3
|
||||||
|
log_bin = /var/log/mysql/mysql-bin.log
|
||||||
|
relay-log = /var/log/mysql/mysql-relay-bin.log
|
||||||
|
binlog_do_db = app
|
|
@ -1,2 +0,0 @@
|
||||||
CREATE DATABASE IF NOT EXISTS `app` CHARACTER SET utf8 COLLATE utf8_general_ci;
|
|
||||||
GRANT ALL ON `app`.* TO 'app'@'%' identified by 'app';
|
|
|
@ -19,7 +19,7 @@ func New(configFile, envPrefix string) (App, error) {
|
||||||
return App{}, fmt.Errorf("can't apply config: %w\n", err)
|
return App{}, fmt.Errorf("can't apply config: %w\n", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
db, err := sql.Open("mysql", conf.DSN.User+":"+conf.DSN.Pass+"@tcp("+conf.DSN.Host+":"+conf.DSN.Port+")/"+conf.DSN.Base)
|
db, err := sql.Open("mysql", conf.DSN.User+":"+conf.DSN.Pass+"@tcp("+conf.DSN.Host+":"+conf.DSN.Port+")/"+conf.DSN.Base+"?charset=utf8&collation=utf8_unicode_ci")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return App{}, err
|
return App{}, err
|
||||||
}
|
}
|
||||||
|
@ -60,7 +60,7 @@ func dbInit(db *sql.DB) error {
|
||||||
City varchar(255) DEFAULT NULL,
|
City varchar(255) DEFAULT NULL,
|
||||||
Interests varchar(255) DEFAULT NULL,
|
Interests varchar(255) DEFAULT NULL,
|
||||||
PRIMARY KEY (Id)
|
PRIMARY KEY (Id)
|
||||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8`); err != nil {
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE utf8_unicode_ci`); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if _, err := db.Exec(`CREATE TABLE IF NOT EXISTS relations (
|
if _, err := db.Exec(`CREATE TABLE IF NOT EXISTS relations (
|
||||||
|
|
|
@ -30,9 +30,13 @@ func NewPerson() (p Person) {
|
||||||
p.FirstName = womanNames[rand.Intn(len(womanNames))]
|
p.FirstName = womanNames[rand.Intn(len(womanNames))]
|
||||||
p.SecondName = secondNames[rand.Intn(len(secondNames))] + "а"
|
p.SecondName = secondNames[rand.Intn(len(secondNames))] + "а"
|
||||||
}
|
}
|
||||||
t := make([]byte, 16)
|
charSet := "abcdedfghijklmnopqrstABCDEFGHIJKLMNOP0123456789"
|
||||||
rand.Read(t)
|
var output strings.Builder
|
||||||
p.Password = string(t)
|
for i := 0; i < 16; i++ {
|
||||||
|
random := rand.Intn(len(charSet))
|
||||||
|
output.WriteString(string(charSet[random]))
|
||||||
|
}
|
||||||
|
p.Password = output.String()
|
||||||
p.City = cities[rand.Intn(len(cities))]
|
p.City = cities[rand.Intn(len(cities))]
|
||||||
for i := 0; i < (rand.Intn(4) + 3); i++ {
|
for i := 0; i < (rand.Intn(4) + 3); i++ {
|
||||||
p.Interests = append(p.Interests, interests[rand.Intn(len(interests))])
|
p.Interests = append(p.Interests, interests[rand.Intn(len(interests))])
|
||||||
|
|
Loading…
Reference in New Issue