lamp架构四、mysql主从复制

主从复制:

官方文档

slave复制主二进制文件到slave上执行,做复制之前:主从的base需要相同

lEnkCHUv.2-t

master端:已经安装了mysql,

master创建复制用户:
为replication创建一个用户:
GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%' IDENTIFIED BY 'WESTOS'; 
 %mysql7版本可以通过一个语句完成,
 mysql8中必须需要如下所示两个语句
mysql>CREATE USER 'repl'@'172.25.10.%' IDENTIFIED BY 'WESTOS';
mysql>GRANT REPLICATION SLAVE ON *.* TO 'repl'@'172.25.10.%';
默认使用caching_sha2_password
mysql> show master status
    -> ;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |      437 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

mysql> create database westos;
Query OK, 1 row affected (0.00 sec)

##在slave端完成配置后创建数据库测试是否同步成功
mysql> create database haha;
Query OK, 1 row affected (0.01 sec)

master配置server-id
[root@server1 data]# cat /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
  • /etc/init.d/mysqld restart

slave端

安装mysql

直接copy之前安装过主机所需文件

  301  scp -r /usr/local/mysql/ server2:/usr/local/
  303  scp /etc/my.cnf server2:/etc/
  307  scp /etc/init.d/mysqld  server2:/etc/init.d/
添加变量:
[root@sever2 ~]# vim .bash_profile 
PATH=$PATH:$HOME/bin:/usr/local/mysql/bin
[root@sever2 ~]# source  .bash_profile 
新建用户

需要删除/usr/local/mysql/data数据

[root@sever2 ~]# useradd -M -d /usr/local/mysql/data/ -s /sbin/nologin mysql

[root@sever2 mysql]# ls
bin   docs     lib      man         README       share
data  include  LICENSE  mysql-test  README-test  support-files
[root@sever2 mysql]# cd data/
[root@sever2 data]# ls
auto.cnf         client-key.pem  ib_logfile1      performance_schema  server1.pid
ca-key.pem       ib_buffer_pool  ibtmp1           private_key.pem     server-cert.pem
ca.pem           ibdata1         mysql            public_key.pem      server-key.pem
client-cert.pem  ib_logfile0     mysql.sock.lock  server1.err         sys
[root@sever2 data]# rm -fr *
[root@sever2 data]# ls
修改配置
[root@sever2 data]# vim /etc/my.cnf
[root@sever2 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@sever2 data]# mysqld --initialize --user=mysql
2021-04-04T09:35:13.666229Z 1 [Note] A temporary password is generated for root@localhost: lEnkCHUv.2-t

[root@sever2 data]# /etc/init.d/mysqld start
Starting MySQL.Logging to ‘/usr/local/mysql/data/sever2.err’.
SUCCESS!

[root@sever2 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@sever2 data]# /etc/init.d/mysqld restart

mysql -pwestos
mysql> CHANGE MASTER TO  MASTER_HOST='172.25.10.1',MASTER_USER='repl',MASTER_PASSWORD='WESTOS',MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=437;
Query OK, 0 rows affected, 2 warnings (0.01 sec)
##//在不使用安全连接并且用户帐户已使用caching_sha2_password
插件进行身份验证(MySQL 8.0的默认设置),
则必须指定 MASTER_PUBLIC_KEY_PATH或 
GET_MASTER_PUBLIC_KEY选项在该 CHANGE MASTER TO语句
中启用基于RSA密钥对的密码交换

mysql> start slave;
Query OK, 0 rows affected (0.00 sec)

mysql> show slave status\G:
*************************** 1. row **************************
#查看下面两个值的状态IO 复制,sql: 存
     Slave_IO_Running: Yes
     Slave_SQL_Running: Yes
mysql> show databases;   #在master端创建haha数据库,可以看见slave端创建的haha数据库
+--------------------+
| Database           |
+--------------------+
| information_schema |
| haha               |
| mysql              |
| performance_schema |
| sys                |
| westos             |
+--------------------+
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值