# MySQL - Running multiple version of mysql using Docker

In case to run multiple versions of MySQL, we can do in several ways. In this article, I'm going to run MySQL multiple versions using Docker & using MariaDB instead of mysql.

## 1\. Install Docker

since we're going to use docker, of course we need to install docker. Please to follow [official documentation by Docker to install](https://s.id/1FGuh) on your machine.

## 2\. Run The mysql Version

You can choose between MySQL, MariaDB, or other version. in this case I'm going to use MariaDB, so I need to run this:

```bash
docker run -d --restart=always --name mariadb-10.4.20 --env MARIADB_ROOT_PASSWORD=root -p 1042:3306 mariadb:10.4.20
```

Descriptions:

* `--detach, -d` Run container in background and print container ID
    
* `--restart` Restart policy to apply when a container exits
    
* `--name` Assign a name to the container
    
* `--env, -e` Set environment variables
    
* `--expose, -p` Expose a port or a range of ports
    
* `mariadb:10.4.20` Package-Name:version
    

## 3\. FAQ:

3.1. How to ByPass password?

try to ssh on the running database by running:

```bash
docker exec -it mariadb-10.4.20 bash
```

then try to move to `HOME` directory by running

```bash
cd
```

and then add new file called `.my.cnf`, and the content are:

```bash
[client]
user=root
password=root
host=localhost

[mysqldump]
host=localhost
user=root
password=root
```

### 3.2. How to Run Execute?

```bash
docker exec -it mariadb-10.2.44 mysql
```

Once you configured 3.1. you don't need to type the password.

### 3.3. How to Import Database

```bash
docker exec -i mariadb-10.2.44 mysql -u user -p database_name < database.sql
```

Or if you want to have the progress bar whilst importing database, you can use this command: (don't forget to [install pv](https://s.id/1FGAU) if you don't have this package)

```bash
pv databse.sql | docker exec -i mariadb-10.2.44 sh -c 'mysql database_name'
```

### 3.4. How to export Database

```bash
docker exec -i mariadb-10.4.20 mysqldump database_name > database.sql
```

If you want to directly compress the file for the output, you can run this: (don't forget to [install gzip](https://s.id/1FGEu) if you don't have it.)

```bash
docker exec -i mariadb-10.4.20 mysqldump database_name | sed -e 's/\sDEFINER=`[^`]*`@`[^`]*`//g' | gzip -c > database.sql.gz
```

### 3.5. Remove DEFINER?

```bash
sed -e 's/\sDEFINER=`[^`]*`@`[^`]*`//g' -i database.sql
```

---

### Preferences:

* [https://docs.docker.com/engine/install/](https://s.id/1FGuh)
    
* [https://hub.docker.com/\_/mysql](https://s.id/1FGEM)
    
* [https://hub.docker.com/\_/mariadb](https://s.id/1FGEP)
    
* [https://docs.docker.com/engine/reference/commandline/run/#options](https://s.id/1FGEY)
    
* [https://howtoinstall.co/en/pv](https://s.id/1FGAU)
    
* [https://howtoinstall.co/en/gzip](https://s.id/1FGEu)
