一、下载 tar.gz 安装包并解压

  1. wget https://mirrors4.tuna.tsinghua.edu.cn/mysql/downloads/MySQL-5.7/mysql-5.7.28-el7-x86_64.tar.gz
  2. tar -zxvf mysql-5.7.28-el7-x86_64.tar.gz
  3. mv mysql-5.7.28-el7-x86_64 /usr/local/mysql

二、MySQL 单机安装

2.1 添加MySQL组及用户

  1. sudo groupadd mysql
  2. sudo useradd -r -g mysql mysql

2.2 修改MySQL文件夹权限

  1. sudo chown -R mysql:mysql /usr/local/mysql

2.3 初始化MySQL(记录下MySQL的临时密码,这里的临时密码是F7f_2r4h7WNu)

  1. [ifan@hadoop130 bin]$ cd /usr/local/mysql/bin
  2. [ifan@hadoop130 bin]$ sudo ./mysqld --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --initialize
  3. 2020-06-02T00:42:51.198796Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
  4. 2020-06-02T00:42:51.463650Z 0 [Warning] InnoDB: New log files created, LSN=45790
  5. 2020-06-02T00:42:51.487541Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
  6. 2020-06-02T00:42:51.543374Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: fd3de093-a469-11ea-ab26-000c2900bcd4.
  7. 2020-06-02T00:42:51.544152Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
  8. 2020-06-02T00:42:52.133913Z 0 [Warning] CA certificate ca.pem is self signed.
  9. 2020-06-02T00:42:52.784629Z 1 [Note] A temporary password is generated for root@localhost: F7f_2r4h7WNu

2.4 启动MySQL服务

  1. [ifan@hadoop130 bin]$ sudo /usr/local/mysql/support-files/mysql.server start
  2. Starting MySQL.Logging to '/usr/local/mysql/data/hadoop130.err'.
  3. SUCCESS!

2.5 将MySQL添加到服务中

  1. [ifan@hadoop130 bin]$ sudo cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
  2. [ifan@hadoop130 bin]$ sudo chmod 755 /etc/init.d/mysqld

2.6 修改MySQL的配置文件

这里的内容为主库Master的配置文件

  1. [mysqld]
  2. port=3306
  3. datadir=/usr/local/mysql/data
  4. socket=/usr/local/mysql/mysql.sock
  5. skip-name-resolve
  6. #skip-grant-tables
  7. max_connections=1000
  8. character_set_server = utf8
  9. wait_timeout=31536000
  10. interactive_timeout=31536000
  11. collation-server=utf8_general_ci
  12. lower_case_table_names=1
  13. server-id=1
  14. log-bin=mysql-bin
  15. expire-logs-days=14
  16. read-only=0
  17. binlog-ignore-db=mysql
  18. symbolic-links=0
  19. log-error=/usr/local/mysql/data/hadoop130.log
  20. pid-file=/usr/local/mysql/data/hadoop130.pid
  21. [client]
  22. socket=/usr/local/mysql/data/mysql.sock
  23. default-character-set = utf8
  24. [mysql]
  25. default-character-set = utf8
  26. [mysql.server]
  27. default-character-set = utf8
  28. [mysqld_safe]
  29. default-character-set = utf8

2.7 新建主从复制账号

  1. GRANT REPLICATION SLAVE ON *.* TO 'replication'@'%' IDENTIFIED BY '123456';
  2. flush privileges;

2.8 查看Master机器的状态,记录 File以及 Position

  1. show master status;

image.png

2.9 从机上,按照如上的步骤,安装MySQL即可

2.10 进入从机,设置 从机复制的主库

  1. # 将 MASTER_LOG_FILE 以及 MASTER_LOG_POS改成2.8中显示的值
  2. CHANGE MASTER TO MASTER_HOST='192.168.137.130',MASTER_USER='replication',MASTER_PASSWORD='123456',MASTER_LOG_FILE='mysql-bin.000003',MASTER_LOG_POS=588;

2.11 启动主从复制

  1. start slave;

查看slave的状态,如果 Slave_IO_Running 和 Slave_SQL_Running 都为yes的话,即为成功。
image.png

2.12 测试主从复制

新建数据库,新建表,以及插入数据,查看slave机器上是否能够看到相同的数据

2.13 关闭 MySQL主从复制

  1. stop slave;