Linux搭建MySQL主从复制

1.先检查系统是否装有mysql

rpm -qa | grep mysql

如有,需要卸载

rpm -e mysqlXXX.rpm

2.下载mysql的repo源

cd /software

wget -c http://repo.mysql.com/mysql57-community-release-el7-9.noarch.rpm

如果提示没有wget 执行 yum -y install wget安装

3.安装mysql rpm包

rpm -ivh mysql57-community-release-el7-9.noarch.rpm

安装了这个,yum就可以在线安装mysql了,这里版本是mysql5.7.36

4.安装MySQL

yum install mysql-server

Linux搭建MySQL主从复制_第1张图片

 5.重置MySQL密码

5.1首先,修改配置文件

vi /etc/my.cnf

在文件末尾增加一行跳过权限验证

skip-grant-tables

然后重启mysql

service mysqld restart

接着可以用root免密登录

mysql -uroot -p

直接回车不用输入密码

5.2修改密码

切换mysql库

    use mysql;

执行修改密码命令

    update user set authentication_string=password('123456') where user='root' and Host='localhost';

执行允许任意ip

    UPDATE mysql.user SET Host='%' where user='root' AND Host='localhost' LIMIT 1;

刷新权限

    flush privileges;

退出mysql

    quit

然后把my.cnf跳过权限那一行注释或删除,重启mysql

这样密码就改好了

Linux搭建MySQL主从复制_第2张图片

 MySQL主从复制配置

按以上步骤配置2台服务器(虚拟机也可以复制,注意server_id不要相同),都配置好mysql密码,并可以使用后,我们开始配置主从

6.主服务器节点:ss1

6.1修改my.cnf,增加以下内容

#skip-grant-tables
###服务器id标识
server_id=1
###开启日志文件
log-bin=mysql-bin

Linux搭建MySQL主从复制_第3张图片

 重启 service mysqld restart

6.2进入mysql验证,看到id正常就ok了

[root@ss1 software]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.36-log MySQL Community Server (GPL)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

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 variables like '%server_id%';
+----------------+-------+
| Variable_name  | Value |
+----------------+-------+
| server_id      | 1     |
| server_id_bits | 32    |
+----------------+-------+
2 rows in set (0.00 sec)

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |      154 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

7.从服务器节点

7.1同样需要修改配置文件my.cnf

vi /etc/my.cnf ,增加以下内容,这里注意我们需要配置需要同步的数据库名称

###从服务器server_id
server_id=2
###日志文件同步方式
log-bin=mysql-bin
###需要同步数据库,如果有多个逗号分隔
binlog_do_db=test,hivetest

改完配置后,记得重启mysql

7.2重启后再次进入mysql命令行

show variables like '%server_id%';//验证配置

mysql> show variables like '%server_id%';
+----------------+-------+
| Variable_name  | Value |
+----------------+-------+
| server_id      | 2     |
| server_id_bits | 32    |
+----------------+-------+
2 rows in set (0.01 sec)

如果验证配置没问题,接下来我们就要设置master同步了,注意master的地址,密码以及log文件名和起始位置信息,在上面已经查询过了,我们执行下面的命令

change master to master_host='ss1',master_user='root',master_password='123456', master_log_file='mysql-bin.000001',master_log_pos=154;

 然后执行start slave开始同步,同步后可以查看下数据库列表

Linux搭建MySQL主从复制_第4张图片

 可以看到数据库test已经从master同步过来了,这里的test是我在同步前建好的,里面没有表

8.演示数据同步

我们在ss1主库新建一张表students

CREATE TABLE `students` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(15) NOT NULL,
  `age` tinyint(3) unsigned DEFAULT '0',
  `high` decimal(5,2) DEFAULT '0.00',
  `gender` enum('男','女','中性','保密') DEFAULT '保密',
  `cls_id` int(10) unsigned NOT NULL,
  `is_delete` bit(1) DEFAULT b'0',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8;

然后切换到从库的shell界面

Linux搭建MySQL主从复制_第5张图片

可以查到表已经有了,里面没有数据

接下来我们回到主库shell界面,插入一条数据到students表

INSERT INTO students (name, age, high, gender, cls_id, is_delete) VALUES('1130', 30, 180.00, '保密', 0, b'0');

然后再回到从库界面,执行查询,可以看到刚才主库的数据了

 我们再执行SHOW SLAVE STATUS,可以查看从库的同步信息

+----------------------------------+-------------+-------------+-------------+---------------+------------------+---------------------+----------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+--------------------------------------+----------------------------+-----------+---------------------+--------------------------------------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+----------------------+--------------+--------------------+
| Slave_IO_State                   | Master_Host | Master_User | Master_Port | Connect_Retry | Master_Log_File  | Read_Master_Log_Pos | Relay_Log_File       | Relay_Log_Pos | Relay_Master_Log_File | Slave_IO_Running | Slave_SQL_Running | Replicate_Do_DB | Replicate_Ignore_DB | Replicate_Do_Table | Replicate_Ignore_Table | Replicate_Wild_Do_Table | Replicate_Wild_Ignore_Table | Last_Errno | Last_Error | Skip_Counter | Exec_Master_Log_Pos | Relay_Log_Space | Until_Condition | Until_Log_File | Until_Log_Pos | Master_SSL_Allowed | Master_SSL_CA_File | Master_SSL_CA_Path | Master_SSL_Cert | Master_SSL_Cipher | Master_SSL_Key | Seconds_Behind_Master | Master_SSL_Verify_Server_Cert | Last_IO_Errno | Last_IO_Error | Last_SQL_Errno | Last_SQL_Error | Replicate_Ignore_Server_Ids | Master_Server_Id | Master_UUID                          | Master_Info_File           | SQL_Delay | SQL_Remaining_Delay | Slave_SQL_Running_State                                | Master_Retry_Count | Master_Bind | Last_IO_Error_Timestamp | Last_SQL_Error_Timestamp | Master_SSL_Crl | Master_SSL_Crlpath | Retrieved_Gtid_Set | Executed_Gtid_Set | Auto_Position | Replicate_Rewrite_DB | Channel_Name | Master_TLS_Version |
+----------------------------------+-------------+-------------+-------------+---------------+------------------+---------------------+----------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+--------------------------------------+----------------------------+-----------+---------------------+--------------------------------------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+----------------------+--------------+--------------------+
| Waiting for master to send event | ss1         | root        |        3306 |            60 | mysql-bin.000001 |                1138 | ss2-relay-bin.000002 |          1304 | mysql-bin.000001      | Yes              | Yes               |                 |                     |                    |                        |                         |                             |          0 |            |            0 |                1138 |            1509 | None            |                |             0 | No                 |                    |                    |                 |                   |                |                     0 | No                            |             0 |               |              0 |                |                             |                1 | f352dda6-51a9-11ec-abd6-525496503cbd | /var/lib/mysql/master.info |         0 |                NULL | Slave has read all relay log; waiting for more updates |              86400 |             |                         |                          |                |                    |                    |                   |             0 |                      |              |                    |
+----------------------------------+-------------+-------------+-------------+---------------+------------------+---------------------+----------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+--------------------------------------+----------------------------+-----------+---------------------+--------------------------------------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+----------------------+--------------+--------------------+
1 row in set (0.00 sec)

总结
        感谢能看到这里的朋友

        本次的分享就到这里,猫头鹰数据致力于为大家分享技术干货

        如果以上过程中出现了任何的纰漏错误,烦请大佬们指正

        受益的朋友或对技术感兴趣的伙伴记得点赞关注支持一波

        也可以扫描二维码或搜索关注我的微信公众号【猫头鹰数据分析】,留言交流

 

 

你可能感兴趣的:(Java,工具,大数据,mysql,mysql优化)