Centos7.8部署mysql与Nacos-2.0.3 集群保姆级文档

Linux安全运维 (4222) 2023-07-15 14:27:29

一、Nacos 简介

Nacos 致力于帮助您发现、配置和管理微服务。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据及流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。 Nacos 是构建以“服务”为中心的现代应用架构 (例如微服务范式、云原生范式) 的服务基础设施。

nacos官网:https://nacos.io/zh-cn/docs/what-is-nacos.html

二、Nacos 安装

2.1 Nacos 环境依赖

Nacos 基于 java 开发的,运行依赖于64 bit JDK 1.8+环境。

前往官网下载JDK: Java Downloads | Oracle

2.2 Nacos 服务端安装

由于Nacos是开源的,可以从github下载源码编译后安装,也可以直接从github下载官方编译好的安装包进行安装,本文直接使用官方编译好的包进行安装,基于目前官方最新release2.0.3版本。请勿使用2.x以下的版本,之前爆出过漏洞。2.x版本相对于1.x版本,性能也提升了10倍。

下载地址:https://github.com/alibaba/nacos/releases
Centos7.8部署mysql与Nacos-2.0.3 集群保姆级文档_https://www.tiejiang.org_Linux安全运维_第1张

三、Nacos 部署

3.1 集群部署

3.2.1 集群架构

Centos7.8部署mysql与Nacos-2.0.3 集群保姆级文档_https://www.tiejiang.org_Linux安全运维_第2张
● 高可用 Nginx 集群
● Nacos 集群(至少三个实例)
● 高可用数据库集群(取代 Nacos 内嵌数据库)

3.2.2 模拟部署

系统版本 机器IP 部署应用版本
centos7.9 192.168.1.1 mysql 5.7
centos7.9 192.168.1.1 nacos 2.0.3
centos7.9 192.168.1.1 nacos 2.0.3
centos7.9 192.168.1.1 nacos 2.0.3

四、搭建步骤

1、mysql单点部署

[root@tiejiang.org ~] cd /vdb/
[root@tiejiang.org vdb] mkdir src
[root@tiejiang.org vdb] wget https://cdn.mysql.com/archives/mysql-5.7/mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz
[root@tiejiang.org vdb] yum install libaio -y

-- 添加mysql用户
[root@tiejiang.org vdb] groupadd mysql
[root@tiejiang.org vdb] useradd -r -g mysql mysql
[root@tiejiang.org vdb] tar zxvf mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz
[root@tiejiang.org vdb] mv mysql-5.7.37-linux-glibc2.12-x86_64 /usr/local/mysql
[root@tiejiang.org vdb] cd /usr/local/mysql/;
[root@tiejiang.org mysql] mkdir /vdb/mysql;
[root@tiejiang.org mysql] mkdir /vdb/mysql/data;
[root@tiejiang.org mysql] mkdir /vdb/mysql/binlog
[root@tiejiang.org mysql] mkdir /vdb/mysql/relaylog;
[root@tiejiang.org mysql] mkdir /vdb/mysql/log
[root@tiejiang.org mysql] mkdir /vdb/mysql/run
[root@tiejiang.org mysql] mkdir /tmp/mysql;
[root@tiejiang.org mysql] touch /vdb/mysql/log/mysql.err
[root@tiejiang.org mysql] chown -R mysql.mysql /vdb/mysql;
[root@tiejiang.org mysql] chown -R mysql.mysql /tmp/mysql;

[root@tiejiang.org mysql] rm -rf /usr/local/mysql/data;
[root@tiejiang.org mysql] ln -s /vdb/mysql/data data;
[root@tiejiang.org mysql] cd support-files/;
[root@tiejiang.org support-files] cp mysql.server /etc/init.d/mysqld;
[root@tiejiang.org support-files] chmod 755 /etc/init.d/mysqld;

## 去修改my.conf的配置文件,把下面的conf文件内容放进去。
[root@tiejiang.org support-files] vim /etc/my.conf
      [mysql]
      port = 3306
      default_character_set = utf8mb4
      socket = /vdb/mysql/run/mysql.socket

      [mysqld]
      log_slave_updates = 1
      lower_case_table_names=1
      # disable auto start slave and need to start slave by mual
      skip_slave_start
      #read_only = 1
      # charset
      character_set_server = utf8mb4
      # server
      max_connections = 1000
      max_connect_errors = 99999
      max_allowed_packet = 1000M
      net_buffer_length = 16K
      skip_name_resolve
      skip_external_locking
      back_log = 150
      query_cache_size = 0
      query_cache_type = 0
      basedir = /usr/local/mysql
      datadir = /vdb/mysql/data
      port = 3306
      server_id = 12855
      socket = /vdb/mysql/run/mysql.socket
      pid_file = /vdb/mysql/run/mysql.pid
      log_error = /vdb/mysql/log/mysql.err
      explicit_defaults_for_timestamp = 1

      # binlog
      log_bin = mysql-bin
      binlog_row_image = full
      log_bin_index = /vdb/mysql/data/mysql-bin.index
      binlog_format = MIXED
      expire_logs_days = 1
      sync_binlog = 1000

      # slowlog
      slow_query_log = 1
      slow_query_log_file = /vdb/mysql/log/slow.log
      long_query_time = 1
      binlog_rows_query_log_events = 1

      # master & relay record method
      master_info_repository = table
      relay_log_info_repository = table
      relay_log_recovery = 1
      slave_net_timeout = 20

      # aviod to duplicate in the double master
      #auto_increment_increment=2
      #auto_increment_offset=2

      # session conn
      binlog_cache_size = 32M
      thread_stack = 512K217
      join_buffer_size = 512K
      sort_buffer_size = 256K
      read_buffer_size = 512K
      read_rnd_buffer_size = 256K
      tmp_table_size = 64M
      max_heap_table_size = 64M
      bulk_insert_buffer_size = 1M

      # Global share memory
      table_definition_cache = 400
      table_open_cache = 400
      thread_cache_size = 512

      # event
      event_scheduler = 1

      # innodb
      innodb_data_home_dir = /vdb/mysql/data
      innodb_data_file_path = ibdata1:1000M:autoextend
      innodb_file_per_table = 1
      innodb_log_group_home_dir = /vdb/mysql/data
      innodb_log_files_in_group = 3
      innodb_log_buffer_size = 16M
      innodb_log_file_size = 256M
      innodb_flush_log_at_trx_commit = 2
      innodb_file_format = Barracuda
      innodb_file_format_max = Barracuda
      innodb_buffer_pool_size = 20000M      //一般设置内存的一半
      #innodb_buffer_pool_dump_at_shutdown = 1
      #innodb_buffer_pool_dump_now = 1
      #innodb_buffer_pool_load_at_startup = 1
      #innodb_buffer_pool_load_now = 1
      innodb_buffer_pool_instances = 8
      innodb_flush_method = O_DIRECT
      innodb_io_capacity = 600
      innodb_lock_wait_timeout = 10
      open_files_limit = 400
      innodb_open_files = 400
      innodb_purge_threads = 1
      innodb_read_io_threads = 12
      innodb_write_io_threads = 12
      innodb_thread_concurrency = 24

      # MyISAM
      key_buffer_size = 8M
      myisam_sort_buffer_size = 128K

      # sql profiling
      sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

[root@tiejiang.org support-files] mysqld --initialize --user=mysql   //初始化mysql数据库
[root@tiejiang.org support-files] service mysqld start      //启动mysql服务
[root@tiejiang.org support-files] cat /vdb/mysql/log/mysql.err | grep password    //查看初始化后mysql的密码

//用初始化mysql后拿到的密码登录mysql,修改mysql的root密码
[root@tiejiang.org ~] mysql -uroot -palsgfasfa8s
  mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'Liu180854';
  mysql> exit;

2、nacos集群部署,3台机器都要操作

# 安装jdk环境
[root@tiejiang.org ~] yum -y remove java
[root@tiejiang.org ~] mkdir /opt/src
[root@tiejiang.org ~] wget -P /opt/src http://10.205.210.70/jdk/jdk-8u181-linux-x64.tar.gz
[root@tiejiang.org ~] tar -xzvf /opt/src/jdk-8u181-linux-x64.tar.gz -C /opt
[root@tiejiang.org ~] rm -rf /opt/java
[root@tiejiang.org ~] ln -sf /opt/jdk1.8.0_181 /opt/java
[root@tiejiang.org ~] echo 'export JAVA_HOME=/opt/java'>>/etc/profile
[root@tiejiang.org ~] echo 'export JAVA_BIN=$JAVA_HOME/bin'>>/etc/profile
[root@tiejiang.org ~] echo 'export JAVA_LIB=$JAVA_HOME/lib'>>/etc/profile
[root@tiejiang.org ~] echo 'export PATH=$JAVA_BIN:$PATH'>>/etc/profile
[root@tiejiang.org ~] source /etc/profile

[root@tiejiang.org ~] cd /usr/local/src/
[root@tiejiang.org src] wget https://github.com/alibaba/nacos/releases/download/2.0.3/nacos-server-2.0.3.tar.gz
[root@tiejiang.org src] tar -zxvf nacos-server-2.0.3.tar.gz
[root@tiejiang.org src] mv nacos /usr/local/nacos-2.0.3
[root@tiejiang.org src] PASSWORD=$(base64 < /dev/urandom | head -c8); echo "$PASSWORD"; echo -n "$PASSWORD" | sha256sum | tr -d '-'
  #生成nacos需要用到的tocken
  #生成的密码:f7me4Iel
  #生成的秘钥,开nacos鉴权的时候配置文件需要:c7020a710bc550d27fbcdd2ed067cb27965f2de9aa7212f3e4fee8b272e09874

# 登陆mysql,创建nacos需要用到的nacos_config数据库和用户
[root@tiejiang.org src] mysql -uroot -palsgfasfa8s
    mysql> create database nacos_config;   //创建数据库
    mysql> use nacos_config;     //应用数据库
    mysql> set names utf8;       //设置字符集
    mysql> CREATE USER 'nacos'@'%' IDENTIFIED BY 'Liu123456';      //创建一个nacos的用户,并将连接设置为远程
    mysql> GRANT ALL PRIVILEGES ON nacos_config.* TO 'nacos'@'%';        //授权新用户访问nacos_config数据库的权限
    mysql> FLUSH PRIVILEGES;     //权限刷新
    mysql> source /usr/local/nacos-2.0.3/conf/nacos-mysql.sql;       //导入nacos的.sql文件 

[root@tiejiang.org src] vim /usr/loacl/nacos-2.0.3/conf/application.properties
        #在#*************** Config Module Related Configurations ***************#  下面新增jdbc连接mysql参数
    spring.datasource.platform=mysql
    db.num=1
    db.url.0=jdbc:mysql://195.168.1.1:3306/nacos_config?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC
    db.user.0=nacos
    db.password.0=Liu123456

    #nacos开启授权
    nacos.core.auth.system.type=nacos
    nacos.core.auth.enabled=true

    #自定义秘钥,用于tocken
    nacos.core.auth.default.token.secret.key=c7020a710bc550d27fbcdd2ed067cb27965f2de9aa7212f3e4fee8b272e09874  //上面命令生成的base64秘钥

[root@tiejiang.org ~] vim /usr/loacl/nacos-2.0.3/conf/cluster.conf
    192.168.1.2:8848
    192.168.1.3:8848
    192.168.1.4:8848
[root@tiejiang.org ~] bash /usr/local/nacos-2.0.3/bin/startup.sh

3、登陆nacos后台配置

http://192.168.1.2:8848/nacos/index.html
http://192.168.1.3:8848/nacos/index.html
http://192.168.1.4:8848/nacos/index.html

去页面中创建一个nacos的用户,密码自定义。
Centos7.8部署mysql与Nacos-2.0.3 集群保姆级文档_https://www.tiejiang.org_Linux安全运维_第3张
Centos7.8部署mysql与Nacos-2.0.3 集群保姆级文档_https://www.tiejiang.org_Linux安全运维_第4张

4、测试

该命令的username和password是登录上面网页nacos后台创建的
[root@tiejiang.org ~] curl -X POST '192.168.1.2:8848/nacos/v1/auth/login' -d 'username=yunwei&password='yunwei123'
THE END

发表评论