正文

环境:

ip 系统 服务
192.168.182.155 centos7.4 安装mariadb
192.168.182.156 centos7.4 安装nginx

一、mariadb安装及配置

1.1 在192.168.182.155安装mariadb

yum install mariadb-server mariadb 

systemctl start mariadb #启动MariaDB

systemctl stop mariadb #停止MariaDB

systemctl restart mariadb #重启MariaDB

systemctl enable mariadb #设置开机启动

接下来进行MariaDB的相关简单配置

mysql_secure_installation

首先是设置密码,会提示先输入密码

Enter current password for root (enter for none):<–初次运行直接回车

设置密码

Set root password? [Y/n] <– 是否设置root用户密码,输入y并回车或直接回车
New password: <– 设置root用户的密码
Re-enter new password: <– 再输入一次你设置的密码

其他配置

Remove anonymous users? [Y/n] <– 是否删除匿名用户,回车

Disallow root login remotely? [Y/n] <–是否禁止root远程登录,回车,

Remove test database and access to it? [Y/n] <– 是否删除test数据库,回车

Reload privilege tables now? [Y/n] <– 是否重新加载权限表,回车

初始化MariaDB完成,接下来测试登录

mysql -uroot -ppassword

完成。

1.2 配置MariaDB的字符集

文件/etc/my.cnf

vi /etc/my.cnf

在[mysqld]标签下添加

init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake

文件/etc/my.cnf.d/client.cnf

vi /etc/my.cnf.d/client.cnf

在[client]中添加

default-character-set=utf8

文件/etc/my.cnf.d/mysql-clients.cnf

vi /etc/my.cnf.d/mysql-clients.cnf

在[mysql]中添加

default-character-set=utf8

全部配置完成,重启mariadb

systemctl restart mariadb

之后进入MariaDB查看字符集

mysql> show variables like "%character%";show variables like "%collation%";

显示为

+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client    | utf8                      |
| character_set_connection | utf8                      |
| character_set_database  | utf8                      |
| character_set_filesystem | binary                    |
| character_set_results    | utf8                      |
| character_set_server    | utf8                      |
| character_set_system    | utf8                      |
| character_sets_dir      | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)

+----------------------+-----------------+
| Variable_name        | Value          |
+----------------------+-----------------+
| collation_connection | utf8_unicode_ci |
| collation_database  | utf8_unicode_ci |
| collation_server    | utf8_unicode_ci |
+----------------------+-----------------+
3 rows in set (0.00 sec)

字符集配置完成。

1.3  添加用户,设置权限

创建用户命令

mysql>create user username@localhost identified by 'password';

直接创建用户并授权的命令

mysql>grant all on *.* to username@localhost indentified by 'password';

授予外网登陆权限

mysql>grant all privileges on *.* to username@'%' identified by 'password';

授予权限并且可以授权

mysql>grant all privileges on *.* to username@'hostname' identified by 'password' with grant option;

简单的用户和权限配置基本就这样了。

其中只授予部分权限把 其中 all privileges或者all改为select,insert,update,delete,create,drop,index,alter,grant,references,reload,shutdown,process,file其中一部分。

1.4 防火墙设置

添加3306端口的访问权限,这里添加后永久生效
firewall-cmd --zone=public --add-port=3306/tcp --permanent
firewall-cmd --reload 启动: systemctl start firewalld
查看状态: systemctl status firewalld
停止: systemctl disable firewalld
禁用: systemctl stop firewalld
启动服务:systemctl start firewalld.service
关闭服务:systemctl stop firewalld.service
重启服务:systemctl restart firewalld.service
服务的状态:systemctl status firewalld.service
在开机时启用一个服务:systemctl enable firewalld.service
在开机时禁用一个服务:systemctl disable firewalld.service
查看服务是否开机启动:systemctl is-enabled firewalld.service
查看已启动的服务列表:systemctl list-unit-files|grep enabled

二、nginx安装及配置

1.1 安装nginx

下载1.9以上版本只有1.9以上版本才支持,安装过程略

注意编译的时候加上--with-stream

./configure --prefix=/usr/local/nginx  \

--conf-path=/etc/nginx/nginx.conf   \ 

--error-log-path=/var/log/nginx/error.log  \

--http-log-path=/var/log/nginx/access.log  \

--pid-path=/var/run/nginx.pid  \

--lock-path=/var/run/nginx.lock  \

--http-client-body-temp-path=/var/cache/nginx/client_temp  \

--http-proxy-temp-path=/var/cache/nginx/proxy_temp  \

--http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp  \

--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp  \

--http-scgi-temp-path=/var/cache/nginx/scgi_temp  \

--user=nginx --group=nginx  \

--with-http_ssl_module  \

--with-http_realip_module  \

--with-http_addition_module  \

--with-http_sub_module  \

--with-http_dav_module  \

--with-http_flv_module  \

--with-http_mp4_module  \

--with-http_gunzip_module  \

--with-http_gzip_static_module  \

--with-http_random_index_module  \

--with-http_secure_link_module  \

--with-http_stub_status_module  \

--with-http_auth_request_module

--with-threads  \

--with-stream  \

--with-stream_ssl_module  \

--with-http_slice_module  \

--with-file-aio --with-http_v2_module --with-ipv6  

2.2、配置

cat /etc/nginx/nginx.conf

#user  nobody;
worker_processes 1; #error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info; #pid logs/nginx.pid; events {
worker_connections 1024;
} http {
include mime.types;
default_type application/octet-stream; #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on;
#tcp_nopush on; #keepalive_timeout 0;
keepalive_timeout 65; #gzip on; server {
listen 80;
server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / {
root html;
index index.html index.htm;
} #error_page 404 /404.html; # redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
} # proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#} # deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
} # another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias; # location / {
# root html;
# index index.html index.htm;
# }
#} # HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost; # ssl_certificate cert.pem;
# ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on; # location / {
# root html;
# index index.html index.htm;
# }
#} } stream { upstream cloudsocket {
hash $remote_addr consistent;
# $binary_remote_addr;
server 192.168.182.155:3306 weight=5 max_fails=3 fail_timeout=30s;
}
server {
listen 3306;#数据库服务器监听端口
proxy_connect_timeout 10s;
proxy_timeout 300s;#设置客户端和代理服务之间的超时时间,如果5分钟内没操作将自动断开。
proxy_pass cloudsocket;
}
}

2.3、重启nginx

/usr/local/nginx/sbin/nginx 

三、验证

登录192.168.182.156服务器执行看是否有3306端口的监听

[root@localhost sbin]# netstat -nap|grep 3306
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 89870/nginx: master

用Navicat for MySQ工具测试是否能连接

作者:凉生墨客 本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
 
分类: mysql,nginx
 
好文要顶 关注我 收藏该文  
0
0
 
 
 

nginx代理tcp协议连接mysql的更多相关文章

  1. nginx 代理tcp长连接短连接配置

    https://blog.csdn.net/tayinyinyueyue/article/details/78932697 nginx使用ngx_stream_core_module模块代理tcp长连 ...

  2. Nginx代理tcp端口实现负载均衡

    Nginx代理tcp端口实现负载均衡 1.修改配置文件 vi /etc/nginx/nginx.conf 添加如下配置: stream { ###XXX upstream notify {   has ...

  3. 实现Nginx代理WSS协议

    因为线上H5游戏需要加上SSL,不想在原来的Web 服务器和游戏服务器支持SSL,只希望 在Nginx代理集群支持SSL.整体架构如下: 从上图可以看出需要总共涉及到https/http 和wss/w ...

  4. Nginx均衡TCP协议服务器案例

    Nginx在企业运维中通常用来均衡HTTP协议,例如我们熟知的80.8080.8081等服务.因为大部分的服务都是http请求访问协议,那有时候需要用到TCP协议,如果来实现均衡呢? 默认nginx不 ...

  5. 我为什么要谈KeepAlive(文末增加nginx 负载tcp长连接保持 demo)

    http://blog.sina.com.cn/s/blog_e59371cc0102ux5w.html 最近工作中遇到一个问题,想把它记录下来,场景是这样的: 从上图可以看出,用户通过Client访 ...

  6. TCP 协议连接与关闭的握手

     原文链接 http://blog.csdn.net/oney139/article/details/8103223   TCP头部: 其中 ACK   SYN  序号  这三个部分在以下会用到,它们 ...

  7. TCP协议 连接三次握手

    TCP(Transmission Control Protocol) 传输控制协议 TCP是主机对主机层的传输控制协议,提供可靠的连接服务,采用三次握手确认建立一个连接: 位码即tcp标志位,有6种标 ...

  8. nginx代理tcp请求

    1.概述 ngx_stream_core_module 这个module在nginx1.90后开始支持.开启nginx的tcp代理支持--with-stream=dynamic --with-stre ...

  9. Nginx 代理TCP/UDP 端口

    Nginx 在1.9版本后新增TCP/UDP 代理 Nginx默认是没有开启TCP/UDP代理.需要在编译Nginx是添加--with-stream进行开启. 编译安装Nginx tar zxf cd ...

随机推荐

  1. SSM框架中,controller的action返回参数给vue.js

    在SSM框架中,controller的action中,返回的是视图,即jsp页面或是ModelAndView,若是通过axios给vue传值的话,需要转换为字符串或是user实体类对象. 使用@Res ...

  2. Exp3 免杀原理与实践

    一.实验过程 1.编码器 (1)使用msf编码器,直接生成meterpreter可执行文件(跟Exp2中生成backdoor.exe的过程一样,生成后门文件),送到Virscan.VirusTotal ...

  3. netcore项目在Windows部署:使用NSSM部署Windows服务

    NSSM部署Windows服务 1 准备工作 在Windows平台部署Asp.net core应用程序一般采用IIS,但是如果我们的net core应用执行的是定时任务,需要开机自启,稳定运行的话,使 ...

  4. [源码分析]StringBuilder

    [源码分析]StringBuilder StringBuilder是继承自AbstractStringBuilder的. 这里附上另外两篇文章的连接: AbstractStringBuilder :  ...

  5. java实现八大排序算法

    Arrays.sort() 采用了2种排序算法 -- 基本类型数据使用快速排序法,对象数组使用归并排序. java的Collections.sort算法调用的是归并排序,它是稳定排序 方法一:直接插入 ...

  6. node服务端口被占用Error listen EADDRINUSE :::3000

    Error: listen EADDRINUSE: address already in use :::3000,出现这个报错说明3000端口被占用 解决方法:找到占用该端口的程序,kill杀掉它就可 ...

  7. 1. Ansible 简介

    目录 1. Ansible 是什么? 2. Ansible 特性 3. 控制主机需求 4. 被管理节点需求 1. Ansible 是什么? Ansible 是一个配置管理系统(configuratio ...

  8. 使用numpy的小惊喜

    今天使用 numpy.true_divide 发现个有趣的事情, 下面的代码18.19行如果去掉,就会报下面的  RuntimeWarning def multivalue_divide(timese ...

  9. Fastjson反序列化漏洞研究

    0x01 Brief Description java处理JSON数据有三个比较流行的类库,gson(google维护).jackson.以及今天的主角fastjson,fastjson是阿里巴巴一个 ...

  10. webstorm破解安装版本

    破解链接:https://blog.csdn.net/xiaoxiong_jiaxin/article/details/83106915 方法一:获取注册码 打开网址(http://idea.lany ...