create_user: automatically fetch user/password and database from master link in slave

This commit is contained in:
Sameer Naik
2015-09-11 11:01:02 +05:30
parent f1d23c68d0
commit e677f04740
3 changed files with 30 additions and 11 deletions

View File

@@ -252,7 +252,7 @@ docker run --name mariadb-slave --link mariadb-master:master -e MARIADB_SERVER_I
In this command we are configuring the container as a slave using the `MARIADB_REPLICATION_MODE=slave` parameter. Before the replication slave is started, the `MARIADB_MASTER_HOST`, `MARIADB_MASTER_USER` and `MARIADB_MASTER_PASSWORD` parameters are used by the slave container to connect to the master and take a dump of the existing data in the database identified by the `MARIADB_DATABASE` paramater. The `MARIADB_REPLICATION_USER` and `MARIADB_REPLICATION_PASSWORD` credentials are used to read the binary replication logs from the master. In this command we are configuring the container as a slave using the `MARIADB_REPLICATION_MODE=slave` parameter. Before the replication slave is started, the `MARIADB_MASTER_HOST`, `MARIADB_MASTER_USER` and `MARIADB_MASTER_PASSWORD` parameters are used by the slave container to connect to the master and take a dump of the existing data in the database identified by the `MARIADB_DATABASE` paramater. The `MARIADB_REPLICATION_USER` and `MARIADB_REPLICATION_PASSWORD` credentials are used to read the binary replication logs from the master.
Using the `master` docker link, the Bitnami MariaDB Docker image supports automatic discovery of the replication paramaters from the master container, namely: When not specified, using the `master` docker link alias, the Bitnami MariaDB Docker image automatically fetches the replication paramaters from the master container, namely:
- `MARIADB_REPLICATION_MODE` - `MARIADB_REPLICATION_MODE`
- `MARIADB_REPLICATION_USER` - `MARIADB_REPLICATION_USER`
@@ -261,18 +261,27 @@ Using the `master` docker link, the Bitnami MariaDB Docker image supports automa
- `MARIADB_MASTER_USER` - `MARIADB_MASTER_USER`
- `MARIADB_MASTER_PASSWORD` - `MARIADB_MASTER_PASSWORD`
As a result you can drop these parameters from the slave. Additionally since `MARIADB_SERVER_ID` is assigned a random identifier we can drop it as well: Additionally, the following parameters are also fetched in the slave container:
- `MARIADB_USER`
- `MARIADB_PASSWORD`
- `MARIADB_DATABASE`
As a result you can drop all of these parameters from the slave. Since `MARIADB_SERVER_ID` is assigned a random identifier we can drop it as well:
```bash ```bash
docker run --name mariadb-slave --link mariadb-master:master \ docker run --name mariadb-slave --link mariadb-master:master \
-e MARIADB_USER=my_user -e MARIADB_PASSWORD=my_password -e MARIADB_DATABASE=my_database \
-e MARIADB_REPLICATION_MODE=slave \ -e MARIADB_REPLICATION_MODE=slave \
bitnami/mariadb bitnami/mariadb
``` ```
You can also add more slaves to the cluster without any downtime allowing you to scale the cluster horizontally as required. With these two commands you now have a two node MariaDB master-slave replication cluster up and running. When required you can add more slaves to the cluster without any downtime allowing you to scale the cluster horizontally.
Using Docker Compose the master-slave replication can be setup with: > **Note**:
>
> The cluster only replicates the database specified in the `MARIADB_DATABASE` parameter.
With Docker Compose the master-slave replication can be setup using:
```yaml ```yaml
master: master:
@@ -290,19 +299,20 @@ slave:
links: links:
- master:master - master:master
environment: environment:
- MARIADB_USER=my_user
- MARIADB_PASSWORD=my_password
- MARIADB_DATABASE=my_database
- MARIADB_REPLICATION_MODE=slave - MARIADB_REPLICATION_MODE=slave
- MARIADB_MASTER_HOST=master - MARIADB_MASTER_HOST=master
``` ```
In Docker Compose you can scale the number of slaves using: Scale the number of slaves using:
```bash ```bash
docker-compose scale master=1 slave=3 docker-compose scale master=1 slave=3
``` ```
The above command scales up the number of slaves to `3`. You can scale down in the same way.
> **Note**: You should not scale the number of master nodes to anything more or less than `1`.
## Command-line options ## Command-line options
The simplest way to configure your MariaDB server is to pass custom command-line options when The simplest way to configure your MariaDB server is to pass custom command-line options when

View File

@@ -26,6 +26,15 @@ create_custom_database() {
} }
create_mysql_user() { create_mysql_user() {
if [ "$MARIADB_REPLICATION_MODE" == "slave" ]; then
if [ ! "$MARIADB_USER" ] || [ ! "${MARIADB_PASSWORD}" ] || [ ! "$MARIADB_DATABASE" ]; then
echo "==> Trying to fetch MariaDB user/password from the master link..."
MARIADB_USER=${MARIADB_USER:-$MASTER_ENV_MARIADB_USER}
MARIADB_PASSWORD=${MARIADB_PASSWORD:-$MASTER_ENV_MARIADB_PASSWORD}
MARIADB_DATABASE=${MARIADB_DATABASE:-$MASTER_ENV_MARIADB_DATABASE}
fi
fi
if [ ! "$MARIADB_USER" ]; then if [ ! "$MARIADB_USER" ]; then
MARIADB_USER=root MARIADB_USER=root
fi fi
@@ -70,7 +79,7 @@ configure_replication() {
slave) slave)
echo "==> Setting up MariaDB slave..." echo "==> Setting up MariaDB slave..."
echo "==> Trying to fetch MariaDB master connection parameters from the master link..." echo "==> Trying to fetch MariaDB replication parameters from the master link..."
MARIADB_MASTER_HOST=${MARIADB_MASTER_HOST:-$MASTER_PORT_3306_TCP_ADDR} MARIADB_MASTER_HOST=${MARIADB_MASTER_HOST:-$MASTER_PORT_3306_TCP_ADDR}
MARIADB_MASTER_USER=${MARIADB_MASTER_USER:-$MASTER_ENV_MARIADB_USER} MARIADB_MASTER_USER=${MARIADB_MASTER_USER:-$MASTER_ENV_MARIADB_USER}
MARIADB_MASTER_PASSWORD=${MARIADB_MASTER_PASSWORD:-$MASTER_ENV_MARIADB_PASSWORD} MARIADB_MASTER_PASSWORD=${MARIADB_MASTER_PASSWORD:-$MASTER_ENV_MARIADB_PASSWORD}

View File

@@ -272,7 +272,7 @@ create_full_container_mounted(){
-e MARIADB_REPLICATION_PASSWORD=$MARIADB_REPLICATION_PASSWORD -e MARIADB_REPLICATION_PASSWORD=$MARIADB_REPLICATION_PASSWORD
run create_container --name $CONTAINER_NAME-slave \ run create_container --name $CONTAINER_NAME-slave \
--link $CONTAINER_NAME-master:master \ --link $CONTAINER_NAME-master:mariadb-master \
-e MARIADB_MASTER_HOST=$CONTAINER_NAME-master \ -e MARIADB_MASTER_HOST=$CONTAINER_NAME-master \
-e MARIADB_MASTER_USER=$MARIADB_USER \ -e MARIADB_MASTER_USER=$MARIADB_USER \
-e MARIADB_MASTER_PASSWORD=$MARIADB_PASSWORD \ -e MARIADB_MASTER_PASSWORD=$MARIADB_PASSWORD \