CentOS 7 下 MySQL 5.7.x 安装

CentOS 7 下 MySQL 5.7.x 安装

安装准备

安装包准备
  • 上传安装包
mysql-5.7.25-1.el7.x86_64.rpm-bundle.tar
  • 解压安装包
# tar -xf mysql-5.7.25-1.el7.x86_64.rpm-bundle.tar
卸载 MariaDB
  • 查看是否安装 MariaDB
# rpm -qa | grep mariadb
mariadb-libs-5.5.56-2.el7.x86_64
  • 卸载 MariaDB
# rpm -e mariadb-libs-5.5.56-2.el7.x86_64 --nodeps

MySQL 5.7.x 安装

  • 安装 mysql-community-common
# rpm -ivh mysql-community-common-5.7.25-1.el7.x86_64.rpm
warning: mysql-community-common-5.7.25-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:mysql-community-common-5.7.25-1.e################################# [100%]
  • 安装 mysql-community-libs
# rpm -ivh mysql-community-libs-5.7.25-1.el7.x86_64.rpm
warning: mysql-community-libs-5.7.25-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:mysql-community-libs-5.7.25-1.el7################################# [100%]
  • 安装 mysql-community-client
# rpm -ivh mysql-community-client-5.7.25-1.el7.x86_64.rpm
warning: mysql-community-client-5.7.25-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:mysql-community-client-5.7.25-1.e################################# [100%]
  • net-tools 安装
# rpm -ivh mysql-community-server-5.7.25-1.el7.x86_64.rpm
warning: mysql-community-server-5.7.25-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
error: Failed dependencies:
	net-tools is needed by mysql-community-server-5.7.25-1.el7.x86_64
# yum -y install net-tools
Loaded plugins: fastestmirror
Determining fastest mirrors
 * base: mirror.bit.edu.cn
 * extras: mirror.bit.edu.cn
 * updates: mirrors.huaweicloud.com
base                                                                                                                                                                                              | 3.6 kB  00:00:00     
extras                                                                                                                                                                                            | 2.9 kB  00:00:00     
updates                                                                                                                                                                                           | 2.9 kB  00:00:00     
(1/4): base/7/x86_64/group_gz                                                                                                                                                                     | 153 kB  00:00:00     
(2/4): extras/7/x86_64/primary_db                                                                                                                                                                 | 205 kB  00:00:00     
(3/4): updates/7/x86_64/primary_db                                                                                                                                                                | 3.0 MB  00:00:00     
(4/4): base/7/x86_64/primary_db                                                                                                                                                                   | 6.1 MB  00:00:01     
Resolving Dependencies
--> Running transaction check
---> Package net-tools.x86_64 0:2.0-0.25.20131004git.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=========================================================================================================================================================================================================================
 Package                                           Arch                                           Version                                                             Repository                                    Size
=========================================================================================================================================================================================================================
Installing:
 net-tools                                         x86_64                                         2.0-0.25.20131004git.el7                                            base                                         306 k

Transaction Summary
=========================================================================================================================================================================================================================
Install  1 Package

Total download size: 306 k
Installed size: 917 k
Downloading packages:
warning: /var/cache/yum/x86_64/7/base/packages/net-tools-2.0-0.25.20131004git.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY
Public key for net-tools-2.0-0.25.20131004git.el7.x86_64.rpm is not installed
net-tools-2.0-0.25.20131004git.el7.x86_64.rpm                                                                                                                                                     | 306 kB  00:00:00     
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Importing GPG key 0xF4A80EB5:
 Userid     : "CentOS-7 Key (CentOS 7 Official Signing Key) <security@centos.org>"
 Fingerprint: 6341 ab27 53d7 8a78 a7c2 7bb1 24c6 a8a7 f4a8 0eb5
 Package    : centos-release-7-5.1804.el7.centos.x86_64 (@anaconda)
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Warning: RPMDB altered outside of yum.
** Found 2 pre-existing rpmdb problem(s), 'yum check' output follows:
2:postfix-2.10.1-6.el7.x86_64 has missing requires of libmysqlclient.so.18()(64bit)
2:postfix-2.10.1-6.el7.x86_64 has missing requires of libmysqlclient.so.18(libmysqlclient_18)(64bit)
  Installing : net-tools-2.0-0.25.20131004git.el7.x86_64                                                                                                                                                             1/1 
  Verifying  : net-tools-2.0-0.25.20131004git.el7.x86_64                                                                                                                                                             1/1 

Installed:
  net-tools.x86_64 0:2.0-0.25.20131004git.el7                                                                                                                                                                            

Complete!
  • 安装 mysql-community-server
# rpm -ivh mysql-community-server-5.7.25-1.el7.x86_64.rpm
warning: mysql-community-server-5.7.25-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Sign                                                                              ature, key ID 5072e1f5: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:mysql-community-server-5.7.25-1.e################################# [100%]
  • 修改配置文件
# vim /etc/my.cnf
datadir=/home/autoInstall_1.2.1/mysql/data 		# 指定 MySQL data 目录
character-set-server=utf8
collation-server=utf8_general_ci
  • 创建目录 && 赋权
# mkdir -p /home/autoInstall_1.2.1/mysql/data
# chown -R mysql:mysql /home/autoInstall_1.2.1/mysql
  • 初始化数据库配置
# mysqld --initialize --user=mysql
  • 启动 MySQL && 配置开机启动
# systemctl start mysqld.service && systemctl enable mysqld.service && systemctl status mysqld.service
  • 获取临时密码
# cat /home/mysql/logs/mysqld.log | grep password
2020-07-13T05:21:02.798254Z 1 [Note] A temporary password is generated for root@localhost: *)3QpY%yh1rw
  • 通过临时密码配置 root 密码
# mysql -u root -p
Enter password: 			# 此处输入
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.25

Copyright (c) 2000, 2019, 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>
  • 更改密码
mysql> set password = password("Test@MySQL1234!");
Query OK, 0 rows affected, 1 warning (0.00 sec)
  • 为 root 提供远程登录权限
mysql> grant all privileges on *.* to 'root' @'%' identified by 'Test@MySQL1234!';
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

MySQL 主从配置

主节点
  • 更改配置文件
server-id = 119
log-bin = master-bin
  • 授权
mysql> GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO 'mysql132'@'%' identified by 'Jhmk@MySQL2345!';
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.01 sec)
从节点
  • 更改配置文件
server-id = 119
  • 指向主节点
mysql> CHANGE MASTER TO MASTER_HOST='10.1.0.119',MASTER_PORT=3306,MASTER_user='mysql132',MASTER_PASSWORD='Jhmk@MySQL2345!';
Query OK, 0 rows affected, 2 warnings (0.03 sec)
  • 启动 Slave
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
  • 查看 master 线程
mysql> show master status \G
*************************** 1. row ***************************
             File: master-bin.000001
         Position: 613
     Binlog_Do_DB:
 Binlog_Ignore_DB:
Executed_Gtid_Set:
1 row in set (0.00 sec)
  • 查看 slave 线程
mysql> show slave status \G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.1.0.119
                  Master_User: mysql132
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000001
          Read_Master_Log_Pos: 613
               Relay_Log_File: hessian05-relay-bin.000002
                Relay_Log_Pos: 828
        Relay_Master_Log_File: master-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: 613
              Relay_Log_Space: 1039
              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: 119
                  Master_UUID: bbce4327-b150-11ea-942f-fa163e3f3868
             Master_Info_File: /home/autoInstall_1.2.1/mysql/data/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> CREATE DATABASE `TEST_SYNC`;
Query OK, 1 row affected (0.01 sec)
  • 创建表
mysql> USE `TEST_SYNC`
Database changed

mysql> CREATE TABLE `SYNC_01` (`ID` INT(11) NOT NULL AUTO_INCREMENT,`NAME` VARCHAR(255) NOT NULL,PRIMARY KEY (`ID`)) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
Query OK, 0 rows affected (0.03 sec)
  • 查看所有库
mysql> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| TEST_SYNC          |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)
  • 查看库中表 && 表结构
mysql> USE TEST_SYNC
Database changed

mysql> SHOW TABLES;
+---------------------+
| Tables_in_TEST_SYNC |
+---------------------+
| SYNC_01             |
+---------------------+
1 row in set (0.00 sec)

mysql> DESC SYNC_01;
+-------+--------------+------+-----+---------+----------------+
| Field | Type         | Null | Key | Default | Extra          |
+-------+--------------+------+-----+---------+----------------+
| ID    | int(11)      | NO   | PRI | NULL    | auto_increment |
| NAME  | varchar(255) | NO   |     | NULL    |                |
+-------+--------------+------+-----+---------+----------------+
2 rows in set (0.01 sec)
  • 查看 – 从节点信息
mysql> show slave hosts;
+-----------+------+------+-----------+--------------------------------------+
| Server_id | Host | Port | Master_id | Slave_UUID                           |
+-----------+------+------+-----------+--------------------------------------+
|       132 |      | 3306 |       119 | a909a62d-c1cd-11ea-ab37-fa163e651b69 |
+-----------+------+------+-----------+--------------------------------------+
1 row in set (0.00 sec)
  • 查看 – binary log 基本信息
mysql> show binary logs;
+-------------------+-----------+
| Log_name          | File_size |
+-------------------+-----------+
| master-bin.000001 |      1099 |
+-------------------+-----------+
1 row in set (0.00 sec)
  • 查看 – binlog event
mysql> show binlog events;
+-------------------+-----+----------------+-----------+-------------+---------------------------------------------------------------------
| Log_name          | Pos | Event_type     | Server_id | End_log_pos | Info
+-------------------+-----+----------------+-----------+-------------+---------------------------------------------------------------------
| master-bin.000001 |   4 | Format_desc    |       119 |         123 | Server ver: 5.7.25-log, Binlog ver: 4
| master-bin.000001 | 123 | Previous_gtids |       119 |         154 |
| master-bin.000001 | 154 | Anonymous_Gtid |       119 |         219 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'
| master-bin.000001 | 219 | Query          |       119 |         461 | GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'mysql132'@'%'
| master-bin.000001 | 461 | Anonymous_Gtid |       119 |         526 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'
| master-bin.000001 | 526 | Query          |       119 |         613 | FLUSH PRIVILEGES
| master-bin.000001 | 613 | Anonymous_Gtid |       119 |         678 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'
| master-bin.000001 | 678 | Query          |       119 |         789 | CREATE DATABASE `TEST_SYNC`
| master-bin.000001 | 789 | Anonymous_Gtid |       119 |         854 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'
| master-bin.000001 | 854 | Query          |       119 |        1099 | use `TEST_SYNC`; CREATE TABLE `SYNC_01` (`ID` INT(11) NOT NULL AUTO_
+-------------------+-----+----------------+-----------+-------------+---------------------------------------------------------------------
10 rows in set (0.00 sec)
从节点
  • 查看所有库
mysql> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| TEST_SYNC          |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)
  • 查看库中表 && 表结构
mysql> USE TEST_SYNC
Database changed

mysql> SHOW TABLES;
+---------------------+
| Tables_in_TEST_SYNC |
+---------------------+
| SYNC_01             |
+---------------------+
1 row in set (0.00 sec)

mysql> DESC SYNC_01;
+-------+--------------+------+-----+---------+----------------+
| Field | Type         | Null | Key | Default | Extra          |
+-------+--------------+------+-----+---------+----------------+
| ID    | int(11)      | NO   | PRI | NULL    | auto_increment |
| NAME  | varchar(255) | NO   |     | NULL    |                |
+-------+--------------+------+-----+---------+----------------+
2 rows in set (0.01 sec)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值