MySQL允许外网连接

1、设置mysql的配置文件

vim /etc/my.cnf
#bind-address=127.0.0.1
service mysql restart

2、登录mysql修改user表

mysql> use user;
mysql> select user,host from user;
mysql> update user set host='%' where user='root';
mysql> flush privileges;
或者:
mysql>GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'mypassword' WITH GRANT OPTION;

1、其中第一个*表示数据库名;第二个*表示该数据库的表名;如果像上面那样 *.*的话表示所有到数据库下面的所有表都允许访问。
2、’%’表示允许访问到mysql的ip地址,也可以配置为具体的ip,%表示所有ip均可以访问。

Linux 安装配置Redis

下载安装

wget http://download.redis.io/releases/redis-4.0.1.tar.gz
解压tar zxvf redis-4.0.1.tar.gz
make && make install

用cp命令复制到usr目录下运行

cp redis-server /usr/local/bin/
cp redis-cli /usr/local/bin/

新建目录,存放配置文件

mkdir /etc/redis
mkdir /var/redis
mkdir /var/redis/log
mkdir /var/redis/run
mkdir /var/redis/6379

在redis解压根目录中找到配置文件模板,复制到如下位置。

cp redis.conf /etc/redis/6379.conf

通过vim命令修改

daemonize yes
pidfile /var/redis/run/redis_6379.pid
logfile /var/redis/log/redis_6379.log
dir /var/redis/6379

最后运行redis:

$ redis-server /etc/redis/6379.conf

添加到系统服务并配置开机启动

进入到redis源码的utils目录,执行 ./install_server.sh ,所有选项默认即可。
修改 /etc/init.d/redis_6379 为 redis : mv /etc/init.d/redis_6379 /etc/init.d/redis

添加到系统服务:chkconfig --add redis
开机启动:chkconfig redis on
这样执行 service redis start|restart|stop 就可以控制redis的启动、重启、停止了。

参考:redis安装部署维护备份

Tomcat配置启动脚本和服务

vim /etc/init.d/tomcat

#!/bin/bash
#
# chkconfig: - 95 15
# description: Tomcat start/stop/status script

#Location of JAVA_HOME (bin files)

export JAVA_HOME=/usr/local/jdk.1.8.0_144

#Add Java binary files to PATH
export PATH=$JAVA_HOME/bin:$PATH

#CATALINA_HOME is the location of the configuration files of this instance of Tomcat
CATALINA_HOME=/usr/local/apache-tomcat-8.5.13

#TOMCAT_USER is the default user of tomcat
TOMCAT_USER=www

#TOMCAT_USAGE is the message if this script is called without any options
TOMCAT_USAGE="Usage: $0 {\e[00;32mstart\e[00m|\e[00;31mstop\e[00m|\e[00;32mstatus\e[00m|\e[00;31mrestart\e[00m}"

#SHUTDOWN_WAIT is wait time in seconds for java proccess to stop
SHUTDOWN_WAIT=20

tomcat_pid() {
echo `ps -ef | grep $CATALINA_HOME | grep -v grep | tr -s " "|cut -d" " -f2`
}

start() {
pid=$(tomcat_pid)
if [ -n "$pid" ];then
echo -e "\e[00;31mTomcat is already running (pid: $pid)\e[00m"
else
echo -e "\e[00;32mStarting tomcat\e[00m"
if [ `user_exists $TOMCAT_USER` = "1" ];then
su $TOMCAT_USER -c $CATALINA_HOME/bin/startup.sh
else
$CATALINA_HOME/bin/startup.sh
fi
status
fi
return 0
}

status(){
pid=$(tomcat_pid)
if [ -n "$pid" ];then
echo -e "\e[00;32mTomcat is running with pid: $pid\e[00m"
else
echo -e "\e[00;31mTomcat is not running\e[00m"
fi
}

加入服务列表

chkconfig --add tomcat

启动停止tomcat

service tomcat start/stop

MySQL忘记root密码

刚在Linux装完MySQL,然而登录的时候发现不知道root密码。更坑爹的是这个版本没有生成my.cnf的文件。所以不知道如何设置登录的时候跳过密码,so google 了一下,发现可以用安全模式登录修改。操作如下:

$mysqld_safe --skip-grant-tables
> mysql
> use mysql
> update user set authentication_string = PASSWORD('123456') where user = 'root';
flush privileges;

然后用exit,重启mysql,用刚才设置的密码重新登录。

$mysql -u root -p
> show database;

发现报错:ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

> SET PASSWORD = PASSWORD('123456');

搞定。

Linux系统安装MySQL

Linux版本:CentOS7.3 64位

MySQL官方下载

本文所安装的版本是5.7.19-linux-glibc,也就是说不是编译版的,是直接编译好的,相当于windows的绿色版。而且5.7的新特性中已经不再推荐用执行 mysql_install_db来初始化了。具体请看下面操作:

把下载好的安装包上传到/usr/local目录下并解压。

tar zxvf mysql-5.7.19-linux-glibc2.12-x86_64.tar.gz

复制解压后的MySQL目录到系统的本地mysql目录

cp mysql-5.7.19-linux-glibc2.12-x86_64 /usr/local/mysql -r

添加系统mysql组和mysql用户,修改目录拥有者为mysql用户

groupadd mysql
useradd -r -g mysql -s /bin/false mysql
cd mysql/
chown -R mysql:mysql ./

执行初始化命令

./bin/mysqld --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data  --initialize-insecure

结果报错:mysqld: Table ‘mysql.plugin’ doesn’t exist

于是换成

./mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data  --initialize-insecure

添加mysql开启启动

cp support-files/mysql.server /etc/init.d/mysql
# 赋予可执行权限
chmod +x /etc/init.d/mysql
# 添加服务
chkconfig --add mysql 
# 显示服务列表
chkconfig --list

启动mysql服务

service mysql start

创建软链接

ln -s /usr/local/mysql/bin/mysql /usr/bin

 

Nginx反向代理配置

先说需求。一台服务器上有多个web容器,apache、nginx、tomcat。由于每个容器都需要监听不同的端口。所以为了用户访问方便,带个端口访问是在是太不专业了。使用Nginx来方向代理,至于原理就不说明了,这里只说如何配置:

Nginx默认监听80端口,假设apache监听81、tomcat监听82端口。那么我们要跳转的域名是apache.06peng.com,tomcat.06peng.com。

server {
        listen       80;
        server_name  apache.06peng.com;

        location / {
        	proxy_redirect off;
   		proxy_set_header Host $host;
    		proxy_set_header X-Real-IP $remote_addr;
    		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_pass http://127.0.0.1:81;
        }        
    }
    
    server {
        listen       80;
        server_name  tomcat.06peng.com;
        location / {
        	proxy_redirect off;
   		proxy_set_header Host $host;
    		proxy_set_header X-Real-IP $remote_addr;
    		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_pass http://127.0.0.1:82;
        }     
    }

 

 

Tomcat 跨域问题处理方案

1、允许Tomcat所有域访问,需要新建文件clientaccesspolicy.xml和crossdomain.xml加入%TOMCAT_HOME%\webapps\ROOT 目录下

clientaccesspolicy.xml

<?xml version=”1.0″ encoding=”utf-8″ ?>
<access-policy>
<cross-domain-access>
<policy>
<allow-from http-request-headers=”*”>
<domain uri=”*”/>
</allow-from>
<grant-to>
<resource path=”/” include-subpaths=”true”/>
</grant-to>
</policy>
</cross-domain-access>
</access-policy>

crossdomain.xml

<?xml version=”1.0″?>
<cross-domain-policy>
<allow-access-from domain=”*” />
</cross-domain-policy>

2、maven配置

<dependency>
    <groupId>com.thetransactioncompany</groupId>
    <artifactId>cors-filter</artifactId>
    <version>2.4</version>
</dependency>
<dependency>
    <groupId>com.thetransactioncompany</groupId>
    <artifactId>java-property-utils</artifactId>
    <version>1.9.1</version>
</dependency>
3、ROOT下的web.xml
<!--解决tomcat部署跨域问题  -->
<filter>
    <filter-name>CORS</filter-name>
    <filter-class>com.thetransactioncompany.cors.CORSFilter</filter-class>
    <init-param>
        <param-name>cors.allowOrigin</param-name>
        <param-value>*</param-value>
    </init-param>
    <init-param>
        <param-name>cors.supportedMethods</param-name>
        <param-value>GET, POST, HEAD, PUT, DELETE</param-value>
    </init-param>
    <init-param>
        <param-name>cors.supportedHeaders</param-name>
        <param-value>Accept, Origin, X-Requested-With, Content-Type, Last-Modified</param-value>
    </init-param>
    <init-param>
        <param-name>cors.exposedHeaders</param-name>
        <param-value>Set-Cookie</param-value>
    </init-param>
    <init-param>
        <param-name>cors.supportsCredentials</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>CORS</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>