mysql简单主从复制(一)

时间:2023-03-09 00:11:05
mysql简单主从复制(一)

MYSQL简单主从复制

master:172.25.44.1

slave:172.25.44.2

mysql5.7安装

master和slave均操作

准备rpm包:mysql-5.7.17-1.el6.x86_64.rpm-bundle.tar

解压:tar xvf mysql-5.7.17-1.el6.x86_64.rpm-bundle.tar

[root@server1 software]# ls
mysql-5.7.17-1.el6.x86_64.rpm-bundle.tar
[root@server1 software]# tar xvf mysql-5.7.17-1.el6.x86_64.rpm-bundle.tar
mysql-community-test-5.7.17-1.el6.x86_64.rpm
mysql-community-embedded-5.7.17-1.el6.x86_64.rpm
mysql-community-embedded-devel-5.7.17-1.el6.x86_64.rpm
mysql-community-server-5.7.17-1.el6.x86_64.rpm
mysql-community-libs-compat-5.7.17-1.el6.x86_64.rpm
mysql-community-devel-5.7.17-1.el6.x86_64.rpm
mysql-community-client-5.7.17-1.el6.x86_64.rpm
mysql-community-libs-5.7.17-1.el6.x86_64.rpm
mysql-community-common-5.7.17-1.el6.x86_64.rpm
[root@server1 software]# ls
mysql-5.7.17-1.el6.x86_64.rpm-bundle.tar mysql-community-embedded-devel-5.7.17-1.el6.x86_64.rpm
mysql-community-client-5.7.17-1.el6.x86_64.rpm mysql-community-libs-5.7.17-1.el6.x86_64.rpm
mysql-community-common-5.7.17-1.el6.x86_64.rpm mysql-community-libs-compat-5.7.17-1.el6.x86_64.rpm
mysql-community-devel-5.7.17-1.el6.x86_64.rpm mysql-community-server-5.7.17-1.el6.x86_64.rpm
mysql-community-embedded-5.7.17-1.el6.x86_64.rpm mysql-community-test-5.7.17-1.el6.x86_64.rpm

查看之前是否安装过mysql,如果有要卸载掉

[root@server1 software]# rpm -qa |grep mysql
mysql-libs-5.1.71-1.el6.x86_64
[root@server1 software]# rpm -e mysql-libs-5.1.71-1.el6.x86_64
error: Failed dependencies:
libmysqlclient.so.16()(64bit) is needed by (installed) postfix-2:2.6.6-2.2.el6_1.x86_64
libmysqlclient.so.16(libmysqlclient_16)(64bit) is needed by (installed) postfix-2:2.6.6-2.2.el6_1.x86_64
mysql-libs is needed by (installed) postfix-2:2.6.6-2.2.el6_1.x86_64
[root@server1 software]# rpm -e mysql-libs-5.1.71-1.el6.x86_64 --nodeps
[root@server1 software]# rpm -qa |grep mysql

安装mysql的rpm包(yum命令自动解决依赖问题,但是如果使用rpm安装的话,需要安装numactl和libaio这两个依赖包)

[root@server1 software]# yum install mysql-community-common-5.7.17-1.el6.x86_64.rpm mysql-community-libs-5.7.17-1.el6.x86_64.rpm mysql-community-devel-5.7.17-1.el6.x86_64.rpm mysql-community-client-5.7.17-1.el6.x86_64.rpm mysql-community-server-5.7.17-1.el6.x86_64.rpm -y

Installed:
mysql-community-client.x86_64 0:5.7.17-1.el6 mysql-community-common.x86_64 0:5.7.17-1.el6
mysql-community-devel.x86_64 0:5.7.17-1.el6 mysql-community-libs.x86_64 0:5.7.17-1.el6
mysql-community-server.x86_64 0:5.7.17-1.el6 Dependency Installed:
libaio.x86_64 0:0.3.107-10.el6 numactl.x86_64 0:2.0.7-8.el6 Complete!

启动mysql

[root@server1 software]# /etc/init.d/mysqld start
Initializing MySQL database: [ OK ]
Installing validate password plugin: [ OK ]
Starting mysqld: [ OK ]
注意:当然其实也是可以不删除那个系统原装的libs库的:
这样的话就需要装mysql-community-libs-compat-5.7.17-1.el6.x86_64.rpm这个兼容库,装这个兼容库的时候会自动删除系统原装的libs库。

/etc/mysql.cnf

master:
log-bin=mysql-bin
server-id=1
binlog-do-db=test #允许主从复制的库
binlog-ignore-db=mysql #禁止主从复制的库
[root@server1 software]# /etc/init.d/mysqld restart
Stopping mysqld: [ OK ]
Starting mysqld: [ OK ]
slave:
server_id=2
[root@server2 software]# /etc/init.d/mysqld restart
Stopping mysqld: [ OK ]
Starting mysqld: [ OK ]

mysql操作

初次登陆mysql并修改密码

mysql5.7的版在数据初始化的时候就会等成一个初始密码,记录在/var/log/mysqld.log

[root@server1 software]# grep "temporary password" /var/log/mysqld.log
2017-06-13T10:13:50.746938Z 1 [Note] A temporary password is generated for root@localhost: ubP0Wisi>HJg

运行mysql_secure_installation会执行几个设置:

a)为root用户设置密码

b)删除匿名账号

c)取消root用户远程登录

d)删除test库和对test库的访问权限

e)刷新授权表使修改生效

通过这几项的设置能够提高mysql库的安全。建议生产环境中mysql安装这完成后一定要运行一次mysql_secure_installation,详细步骤请参看下面的命令:

[root@server1 software]# 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:
The 'validate_password' plugin is installed on the server.
The subsequent steps will run with the existing configuration
of the plugin.
Using existing password for root. Estimated strength of the password: 100
Change the password for root ? ((Press y|Y for Yes, any other key for No) : y New password: Re-enter new password: Estimated strength of the password: 100
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
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!
master配置

grant replication slave on . to root@172.25.44.2 identified by 'Lt@18392027447';##通过密码赋予172.25.44.2的root用户权限

flush privileges;##刷新mysql的系统权限相关表

[root@server1 software]# mysql -uroot -pLt@18392027447
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 5.7.17-log MySQL Community Server (GPL) Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> grant replication slave on *.* to root@172.25.44.2 identified by 'Lt@18392027447';
Query OK, 0 rows affected, 1 warning (0.11 sec) mysql> flush privileges;
Query OK, 0 rows affected (0.40 sec) mysql> quit
Bye
[root@server1 software]# mysql -uroot -pLt@18392027447
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 5.7.17-log MySQL Community Server (GPL) Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 1399 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec) mysql> quit
Bye
slave配置

master_log_file='mysql-bin.000001',master_log_pos=1399;##master端mysql> show master status;可查询此参数

show slave status\GSlave_IO和Slave_SQL必须开启,即Slave_IO_Running和Slave_SQL_Running必须对应yes

[root@server2 software]# mysql -uroot -pLt@18392027447
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.17 MySQL Community Server (GPL) Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> stop slave;
Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> change master to master_user='root',master_password='Lt@18392027447',master_host='172.25.44.1',master_log_file='mysql-bin.000001',master_log_pos=1399;
Query OK, 0 rows affected, 2 warnings (1.23 sec) mysql> start slave;
Query OK, 0 rows affected (0.14 sec) mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.25.44.1
Master_User: root
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 1399
Relay_Log_File: server2-relay-bin.000002
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 1399
Relay_Log_Space: 529
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
Master_UUID: ff0d1657-5020-11e7-9d4a-525400a2b9df
Master_Info_File: /var/lib/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec) mysql> quit
Bye

主从服务器测试:

master端建立数据库并插入一条数据:
[root@server1 software]# mysql -uroot -pLt@18392027447
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 14
Server version: 5.7.17-log MySQL Community Server (GPL) Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> create database westos;
Query OK, 1 row affected (0.18 sec) mysql> use westos;
Database changed
mysql> create table westos(name char(10),id int(3));
Query OK, 0 rows affected (1.64 sec) mysql> insert into westos values('kiosk',003);
Query OK, 1 row affected (0.13 sec) mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| westos |
+--------------------+
5 rows in set (0.00 sec) mysql> quit
Bye
slave端数据库查询
[root@server2 software]# mysql -uroot -pLt@18392027447
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.17 MySQL Community Server (GPL) Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| westos |
+--------------------+
5 rows in set (0.00 sec) mysql> use westos;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A Database changed
mysql> select * from westos;
+-------+------+
| name | id |
+-------+------+
| kiosk | 3 |
+-------+------+
1 row in set (0.00 sec) mysql> quit
Bye