mysql基础主从复制
1. master端
master端:已经安装了mysql
[root@server1 data]# scp -r mysql/ root@172.25.16.2:/usr/local ##复制mysql二进制程序到另外一台主机中用于设置mysql从属
[root@server1 data]# cd ..
[root@server1 mysql]# cd /etc/
[root@server1 etc]# scp my.cnf server2:/etc/
[root@server1 etc]# scp /etc/init.d/mysqld server2:/etc/init.d/
[root@server1 etc]# /etc/init.d/mysqld start
Starting MySQL SUCCESS!
[root@server1 etc]# vim /etc/my.cnf
[mysqld]
datadir=/usr/local/mysql/data
socket=/usr/local/mysql/data/mysql.sock
symbolic-links=0
server-id=1 ##数据同步
log-bin=mysql-bin
[root@server1 etc]# /etc/init.d/mysqld restart ##重启使之生效
[root@server1 data]# mysql -pwestos
mysql> grant REPLICATION SLAVE ON *.* TO 'repl'@'%' IDENTIFIED BY 'westos';
mysql> show master status;
为replication创建一个用户:
GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%' IDENTIFIED BY 'westos'; %mysql7版本可以通过一个语句完成,mysql8中必须需要如下所示两个语句:
mysql> CREATE USER 'repl'@'%.example.com' IDENTIFIED BY 'password';
mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%.example.com';
mysql-bin.000001会记录对数据库的所有变更,mysql-bin.index是数据库二进制索引文件,它记录了数据库所有的二进制日志:
2. slave端
[root@server2 ~]# cd /usr/local/
[root@server2 local]# du -sh mysql/
2.0G mysql/
[root@server2 ~]# vim .bash_profile
PATH=$PATH:$HOME/bin:/usr/local/mysql/bin ##添加环境变量
[root@server2 ~]# source .bash_profile
[root@server2 ~]# cd /usr/local/mysql/data/
[root@server2 data]# rm -fr * ##删除原来的mysql配置
[root@server2 ~]# useradd -M -d /usr/local/mysql/data/ -s /sbin/nologin mysql ##添加mysql用户
[root@server2 ~]# mysqld --initialize --user=mysql ##mysql初始化
[root@server2 data]# /etc/init.d/mysqld start ##启动数据库
[root@server2 data]# mysql_secure_installation ##安全安装
Securing the MySQL server deployment.
Enter password for user root:
The existing password for the user account root has expired. Please set a new password.
New password:
Re-enter new password:
VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?
Press y|Y for Yes, any other key for No:
Using existing password for root.
Change the password for root ? ((Press y|Y for Yes, any other key for No) :
... skipping.
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.
Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.
Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
Success.
By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
- Dropping test database...
Success.
- Removing privileges on test database...
Success.
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.
All done!
[root@server2 data]# mysql -pwestos
[root@server2 data]# vim /etc/my.cnf
[root@server2 data]# cat /etc/my.cnf
[mysqld]
datadir=/usr/local/mysql/data
socket=/usr/local/mysql/data/mysql.sock
symbolic-links=0
server-id=2
[root@server2 data]# /etc/init.d/mysqld restart ##重启使之生效
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!
[root@server2 data]# mysql -pwestos
mysql> CHANGE MASTER TO MASTER_HOST='172.25.0.1',MASTER_USER='repl',MASTER_PASSWORD='westos',MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=437; ##备份数据库
mysql> start slave; ##启动slave
mysql> show slave status\G;
IO线程:负责复制replication的日志
SQL线程:负责重做
3. 测试
在server1上创建数据库westos,server2数据库会同步更新