Because recently in learning MySQL, see a lot of places have "sub-database sub-table", "Read and write separation" word, want to try it!
First of all, because of my limited financial capacity, can not afford so many servers, so I borrowed Docker to build a virtual machine to serve as two servers.
First, prepare the master-slave server
1. Create a master server
$docker run-it--name=mysql_master Ubuntu:14.04/bin/bash
2. Create a Slave server
$docker run-it--name=mysql_slave Ubuntu:14.04/bin/bash
At this point, the two servers are ready with the following parameters:
Operating System (OS): Ubuntu 14.04
Primary server ip:172.17.0.2
From server ip:172.17.0.3
Second, install MySQL on the master and slave server
Because of the version issue, you need to update the software source (if not updated, the subsequent installation of MySQL will be prompted to install the package)
$ apt-get Update
I just need a step to install MySQL here.
Install Mysql-server
A confirmation stop will appear in the middle, press "Y".
Finally, you will get the login password of the root user who will enter MySQL, which is used later.
Confirm the password again.
At this point, the master-slave server MySQL installation completed
Third, build MySQL master-slave replication
Here is the principle of master-slave replication, is based on the MySQL log files, the main server's MySQL operation log files sent to the slave server, let the server execute log files, to achieve the purpose of master-slave replication.
1, configure the primary server (the following operations in the master server)
Locate the MySQL configuration file (my.cnf) on the primary server and modify the configuration file. (I've been learning the basics of Linux recently, little Show)
$ find -name mf.cnf
Represents the configuration file in this directory
Then modify the file
$ VI /ETC/MYSQL/MY.CNF
And then we go into this interface.
And then we found a couple of lines, and this is where we're going to configure it.
Remove the Server-id and Log_bin in front of the "#" comment.
Server-id: Used to identify the server MySQL, in the future as long as there is a MySQL cluster, Server-id in the cluster must be unique.
Log_bin: For storing binary log files, other MySQL is copied according to this file.
Others can be used without a tube.
Save to exit. The MySQL service needs to be restarted later (service MySQL restart)
Then authorize it in MySQL, and why authorize it? MySQL for other servers to get the log files always have some permission.
Log in to MySQL
This is more embarrassing, the reason why the MySQL service didn't open up.
A red line is turned on successfully, and then log in to MySQL.
Then it is authorized, because it is the master-slave copy, so the authorization command is a bit special, normal words can look at my previous write an article.
In the future, the user name and password, which is "usr" and "123", where "flush privileges" is the command to update permissions.
Then it is the information about the log files that are related to the master server.
Here's the file "mysql-bin.000001" and "48481" we'll use.
Then quit MySQL and check out our log file production No. The directory generated by the log file is also available in the configuration above.
With this file, the configuration is already in effect.
2. Configure the slave server
Enter from the server, also, to modify the MySQL configuration file (my.cnf), Server-id must not be the same as the primary server
This can both
Save exit, restart MySQL service (service MySQL restart)
Then go to MySQL and receive the log file of the master server.
Do you see a lot of familiar information here?
Master_host: The host that sent the log file
Master_user: Authorized User
Master_password: User Password
Master_log_file: Log file name
Master_log_pos: Log file location
Finally, look at the status from the server.
The two lines in the red box are yes, that's the success!!!
If not, check the above steps for more checks.
Finally, check it out.
1. Create a database on the primary server
2. Creating a database from the server
The final result shows that the master-slave replication is only one-way replication, and how to do two-way replication, this is the primary master replication.
The effect of primary master replication is also very simple, at the time of configuration, will be done in the master and slave server to do once again.
That is, at the time of authorization from the server, the primary server MySQL configuration is good
。
Docker implements MySQL master-slave replication (primary master replication)