赞
踩
1.1主机规划,两台主机,mysql-master 和mysql-agent
mysql-master ip:192.168.120
mysql-agent ip:192.168.121
1.2下载 相关rpm包(也可以配置yum源的方式安装)
下载地址:https://download.csdn.net/download/Z_xingwei/88065250
也可以从清华源下载,https://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql-5.7-community-el6-x86_64/
分别下载Mysql四个包(主从都要下载)
mkdir /servyou/install -p
cd /servyou/install
wget https://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql-5.7-community-el6-x86_64/mysql-community-server-5.7.16-1.el6.x86_64.rpm
wget https://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql-5.7-community-el6-x86_64/mysql-community-libs-5.7.16-1.el6.x86_64.rpm
wget https://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql-5.7-community-el6-x86_64/mysql-community-common-5.7.16-1.el6.x86_64.rpm
wget https://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql-5.7-community-el6-x86_64/mysql-community-client-5.7.16-1.el6.x86_64.rpm
1.3安装启动
主从都要执行的操作(设置时间同步,自行完成)
[root@mysql-agent install]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
# 加入以下内容
192.168.10.120 mysql-master
192.168.10.121 mysql-agent
[root@mysql-agent install]# yum localinstall -y mysql-community-common-5.7.16-1.el6.x86_64.rpm
[root@mysql-agent install]# yum localinstall -y mysql-community-libs-5.7.16-1.el6.x86_64.rpm
[root@mysql-agent install]# yum localinstall -y mysql-community-client-5.7.16-1.el6.x86_64.rpm
[root@mysql-agent install]# yum localinstall -y mysql-community-server-5.7.16-1.el6.x86_64.rpm
启动mysql
[root@mysql-agent install]# systemctl start mysqld
1.4修改Mysql密码
安装好后查看默认密码
[root@mysql-agent install]# grep 'password' /var/log/mysqld.log
2021-12-12T09:33:42.707054Z 1 [Note] A temporary password is generated for root@localhost: .:?li?7sz8%E
修改:
[root@mysql-agent install]# mysql -uroot -p .:?li?7sz8%E
mysql>set global validate_password_policy=0; # 关闭密码验证策略
mysql> set global validate_password_length=4; # 设置密码长度不小于4位,这两步便于设置简单密码
mysql> set password='123456'; # 设置密码
1.5设置主从配置文件
mysql-master执行的操作
[root@mysql-master date]# grep -Ev '^$|^#' /etc/my.cnf [mysqld] port = 3306 datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid lower_case_table_names=1 # 不去分别大小写 log-bin=/var/lib/mysql/mysql-bin # 必须开启二进制日志日志 server_id = 1 # server_id 不能相同 ----------------------------------------------------------------------------------------------------- 友情提示: mysql5.7也可以使用以下配置开启二进制日志 log_bin=ON log_bin_basename=/var/lib/mysql/mysql-bin log_bin_index=/var/lib/mysql/mysql-bin.index 三个参数来指定, 第一个参数是打开binlog日志 第二个参数是binlog日志的基本文件名,后面会追加标识来表示每一个文件 第三个参数指定的是binlog文件的索引文件,这个文件管理了所有的binlog文件的目录 第二种: log-bin=/var/lib/mysql/mysql-bin 这一个参数的作用和上面三个的作用是相同的,mysql会根据这个配置自动设置log_bin为on状态,自动设置log_bin_index文件为你指定的文件名后跟.index,这些配置完毕之后对于5.7以下版本应该是可以了,但是我们这个时候用的如果是5.7及以上版本的话,重启mysql服务会报错。这个时候我们必须还要指定一个参数 server-id=1 随机指定一个不能和其他集群中机器重名的字符串,如果只有一台机器,那就可以随便指定了
master数据库已经有业务数据,需要停服务拷贝到slave服务器。
注意:slave是一个干净系统,没有安装mysql数据库,所以需要将所有的安装目录和数据目录一起拷贝
重启mysql
[root@mysql-agent install]# systemctl restart mysqld
[root@mysql-master date]# mysql -uroot -p 123456
# 创建并授权用户
mysql> grant replication slave on *.* to 'slave'@'192.168.10.%' identified by '123456';
mysql> flush tables with read lock; # 先加锁,防止两边数据不一致;如果业务还未上线,这个就没有必要了
Query OK, 0 rows affected (0.00 sec)
mysql> show master status; # 只有打开二进制日志,这句命令才有结果,表示当前数据库的二进制日志写到什么位置
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 601 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
# File:二进制文件名 Position:正在写入的位置
mysql-agent执行的操作
# 修改slave配置文件 [root@mysql-agent install]# grep -Ev "^#|^$" /etc/my.cnf [mysqld] port=3306 datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid lower_case_table_names=1 server_id = 2 relay_log = /var/lib/mysql/relay.log 重启mysql [root@mysql-agent install]# systemctl restart mysqld [root@mysql-agent install]# mysql -u root -p 123456 mysql> change master to # slave端设定复制信息 master_host='192.168.10.120',master_user='slave',master_password='123456',master_port=3306,master_log_file='mysql-bin.000001',master_log_pos=601; Query OK, 0 rows affected, 2 warnings (0.01 sec) mysql> start slave; Query OK, 0 rows affected (0.00 sec) mysql> show slave status \G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.10.120 Master_User: slave Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 601 Relay_Log_File: relay.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: 601 Relay_Log_Space: 517 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: 0e139eb8-5b2b-11ec-9d6c-000c298d544e 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) ERROR: No query specified Slave_IO_Running: Yes # 代表成功连接到master并且下载日志 Slave_SQL_Running: Yes # 代表成功执行日志中的SQL语句
mysql-master执行
# 回到master端解锁:
mysql> unlock tables;
1.6 测试验证
master写——>slave可以看到
slave写——>master看不到
第一次写博客有什么不好的可以提提意见,见谅。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。