Introduction
I’m using Docker for learning. I’ve configured the docker-cn mirror.
Setting Up a MySQL Cluster
Use the following command to pull the MySQL image:
docker pull mysql
Use the following commands to set up one master and one slave:
docker run --name mysql-master -P -d -e MYSQL_ROOT_PASSWORD=ln mysql
docker run --name mysql-slave1 -P -d -e MYSQL_ROOT_PASSWORD=ln mysql
Where -d configures it as a daemon process, -P exposes all ports, and the root password is set to “ln”.
Use the following commands to enter the two Docker containers:
docker exec -it mysql-master bash
docker exec -it mysql-slave1 bash
Check the network IP. First, we need to install net-tools, which requires an update, and before updating, we need to change the source:
sed -i 's/deb.debian.org/mirrors.ustc.edu.cn/g' /etc/apt/sources.list
apt update
apt install net-tools
Enter ifconfig to check the IP address. The master is 172.17.0.2, and slave1 is 172.17.0.3.
Set login permissions In the MySQL master, follow these steps. First login, then set the root user to be accessible without a password from anywhere.
mysql -uroot -p
mysql> grant all privileges on *.* to root@'%' identified by '';
Query OK, 0 rows affected (0.02 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.02 sec)
In slave1, use the master’s database with an empty password:
mysql -uroot -p -h 172.17.0.2
Create a database in the master:
create database test_docker;
Check if the database exists in the slave:
show databases;
Miscellaneous
SQL is case-insensitive; SELECT and select are the same. It’s better to add a semicolon at the end of SQL statements. Use “use” to select a database. Use “set” to set values. The method to add users is with “create”. Then grant permissions to users for databases and tables.
mysql -u root -p
Enter password:**
mysql> use mysql;
mysql> create user 'linanwx'@'%' identified by 'ln';
mysql> grant all on *.* to 'linanwx'@'%';
SHOW DATABASES is used to display the current databases. SHOW TABLES is used to display the current tables.