
centOs转ubuntu爬坑记录
由于centos停止维护,很多服务器选择更换系统,此次是因为我之前部署过的一个小程序的后台服务,因为要从centOS7切换到Ubuntu20.04,也就有了后来在ubuntu坑里进进出出的画面。
由于centos停止维护,很多服务器选择更换系统,此次是因为我之前部署过的一个小程序的后台服务,因为要从centOS7切换到Ubuntu20.04,也就有了后来在ubuntu坑里进进出出的画面。
一、安装Mysql
我安装的是Mysql8.0,安装教程:
https://blog.csdn.net/qq_38935605/article/details/127509902
按照以上教程安装没有什么问题,然后坑来了:远程计算机无法连接到服务器,报错内容:Lost connection to MySQL server at ‘reading initial communication packet‘
后来才知道,Ubuntu上安装完mysql以后,连接地址默认是本地,需要手动改一下,之前确实没遇到过这个。
Mysql 配置文件地址: /etc/mysql/mysql.conf.d/mysqld.cnf
在上面的配置文件里,有一条是bind-address=127.0.0.1,其含义是仅监听本地连接,将该条注释后改为0.0.0.0重启Mysql服务即可顺利连接远程服务器。
#bind-address = 127.0.0.1
bind-address = 0.0.0.0
二、安装JDK
安装步骤请参考:
https://blog.csdn.net/qq_43503670/article/details/125129508
我要说的是坑:因为本次项目的服务器和系统都是客户提供的,所以不知道是因为系统安装时没有调整好时区的原因,还是其他原因,系统的时区居然不是东8区,这样的话,在运行JAVA程序获取系统时间时,自然也就不是东8区的时间。
但是由于数据库连接中配置了时区,所以在JAVA程序获取系统时间,并持久化到数据库中的时候,竟然自动补上了时区差!真是太贴心了。
spring:
datasource:
type: com.mysql.cj.jdbc.MysqlDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/mydata?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
username: root
password: ******
然后服务器的管理员将系统时区调整到了东8区,我再次执行java程序,居然还是会被补上8小时(时区确实已经设置成功)。
所以我怀疑,java取到的时区仍然不是他设置的“东8区”,要解决这个问题,我想到了三个方案。
1、在java程序获取系统时间的时候,设置一下时区。但是这样需要修改源码,我这样一个懒人,当然果断放弃;
2、查看jdk中是否有统一设置;于是我找到了:java -XshowSettings:local
root@wahaha:~# java -XshowSettings:local
VM settings:
Max. Heap Size (Estimated): 2.60G
Ergonomics Machine Class: server
Using VM: Java HotSpot(TM) 64-Bit Server VM
Property settings:
awt.toolkit = sun.awt.X11.XToolkit
file.encoding = UTF-8
file.encoding.pkg = sun.io
file.separator = /
java.awt.graphicsenv = sun.awt.X11GraphicsEnvironment
java.awt.printerjob = sun.print.PSPrinterJob
java.class.path = .
/usr/local/jdk1.8.0_192/lib
/usr/local/jdk1.8.0_192/jre/lib
java.class.version = 52.0
java.endorsed.dirs = /usr/local/jdk1.8.0_192/jre/lib/endorsed
java.ext.dirs = /usr/local/jdk1.8.0_192/jre/lib/ext
/usr/java/packages/lib/ext
java.home = /usr/local/jdk1.8.0_192/jre
java.io.tmpdir = /tmp
java.library.path = /usr/java/packages/lib/amd64
/usr/lib64
/lib64
/lib
/usr/lib
java.runtime.name = Java(TM) SE Runtime Environment
java.runtime.version = 1.8.0_192-b12
java.specification.name = Java Platform API Specification
java.specification.vendor = Oracle Corporation
java.specification.version = 1.8
java.vendor = Oracle Corporation
java.vendor.url = http://java.oracle.com/
java.vendor.url.bug = http://bugreport.sun.com/bugreport/
java.version = 1.8.0_192
java.vm.info = mixed mode
java.vm.name = Java HotSpot(TM) 64-Bit Server VM
java.vm.specification.name = Java Virtual Machine Specification
java.vm.specification.vendor = Oracle Corporation
java.vm.specification.version = 1.8
java.vm.vendor = Oracle Corporation
java.vm.version = 25.192-b12
line.separator = \n
os.arch = amd64
os.name = Linux
os.version = 5.4.0-148-generic
path.separator = :
sun.arch.data.model = 64
sun.boot.class.path = /usr/local/jdk1.8.0_192/jre/lib/resources.jar
/usr/local/jdk1.8.0_192/jre/lib/rt.jar
/usr/local/jdk1.8.0_192/jre/lib/sunrsasign.jar
/usr/local/jdk1.8.0_192/jre/lib/jsse.jar
/usr/local/jdk1.8.0_192/jre/lib/jce.jar
/usr/local/jdk1.8.0_192/jre/lib/charsets.jar
/usr/local/jdk1.8.0_192/jre/lib/jfr.jar
/usr/local/jdk1.8.0_192/jre/classes
sun.boot.library.path = /usr/local/jdk1.8.0_192/jre/lib/amd64
sun.cpu.endian = little
sun.cpu.isalist =
sun.io.unicode.encoding = UnicodeLittle
sun.java.launcher = SUN_STANDARD
sun.jnu.encoding = UTF-8
sun.management.compiler = HotSpot 64-Bit Tiered Compilers
sun.os.patch.level = unknown
user.country = US
user.dir = /root
user.home = /root
user.language = en
user.name = root
user.timezone =
Locale settings:
default locale = English
default display locale = English (United States)
default format locale = English (United States)
available locales = , ar, ar_AE, ar_BH, ar_DZ, ar_EG, ar_IQ, ar_JO,
ar_KW, ar_LB, ar_LY, ar_MA, ar_OM, ar_QA, ar_SA, ar_SD,
ar_SY, ar_TN, ar_YE, be, be_BY, bg, bg_BG, ca,
ca_ES, cs, cs_CZ, da, da_DK, de, de_AT, de_CH,
de_DE, de_GR, de_LU, el, el_CY, el_GR, en, en_AU,
en_CA, en_GB, en_IE, en_IN, en_MT, en_NZ, en_PH, en_SG,
en_US, en_ZA, es, es_AR, es_BO, es_CL, es_CO, es_CR,
es_CU, es_DO, es_EC, es_ES, es_GT, es_HN, es_MX, es_NI,
es_PA, es_PE, es_PR, es_PY, es_SV, es_US, es_UY, es_VE,
et, et_EE, fi, fi_FI, fr, fr_BE, fr_CA, fr_CH,
fr_FR, fr_LU, ga, ga_IE, hi, hi_IN, hr, hr_HR,
hu, hu_HU, in, in_ID, is, is_IS, it, it_CH,
it_IT, iw, iw_IL, ja, ja_JP, ja_JP_JP_#u-ca-japanese, ko, ko_KR,
lt, lt_LT, lv, lv_LV, mk, mk_MK, ms, ms_MY,
mt, mt_MT, nl, nl_BE, nl_NL, no, no_NO, no_NO_NY,
pl, pl_PL, pt, pt_BR, pt_PT, ro, ro_RO, ru,
ru_RU, sk, sk_SK, sl, sl_SI, sq, sq_AL, sr,
sr_BA, sr_BA_#Latn, sr_CS, sr_ME, sr_ME_#Latn, sr_RS, sr_RS_#Latn, sr__#Latn,
sv, sv_SE, th, th_TH, th_TH_TH_#u-nu-thai, tr, tr_TR, uk,
uk_UA, vi, vi_VN, zh, zh_CN, zh_HK, zh_SG, zh_TW
Usage: java [-options] class [args...]
(to execute a class)
or java [-options] -jar jarfile [args...]
(to execute a jar file)
where options include:
-d32 use a 32-bit data model if available
-d64 use a 64-bit data model if available
-server to select the "server" VM
The default VM is server,
because you are running on a server-class machine.
-cp <class search path of directories and zip/jar files>
-classpath <class search path of directories and zip/jar files>
A : separated list of directories, JAR archives,
and ZIP archives to search for class files.
-D<name>=<value>
set a system property
-verbose:[class|gc|jni]
enable verbose output
-version print product version and exit
-version:<value>
Warning: this feature is deprecated and will be removed
in a future release.
require the specified version to run
-showversion print product version and continue
-jre-restrict-search | -no-jre-restrict-search
Warning: this feature is deprecated and will be removed
in a future release.
include/exclude user private JREs in the version search
-? -help print this help message
-X print help on non-standard options
-ea[:<packagename>...|:<classname>]
-enableassertions[:<packagename>...|:<classname>]
enable assertions with specified granularity
-da[:<packagename>...|:<classname>]
-disableassertions[:<packagename>...|:<classname>]
disable assertions with specified granularity
-esa | -enablesystemassertions
enable system assertions
-dsa | -disablesystemassertions
disable system assertions
-agentlib:<libname>[=<options>]
load native agent library <libname>, e.g. -agentlib:hprof
see also, -agentlib:jdwp=help and -agentlib:hprof=help
-agentpath:<pathname>[=<options>]
load native agent library by full pathname
-javaagent:<jarpath>[=<options>]
load Java programming language agent, see java.lang.instrument
-splash:<imagepath>
show splash screen with specified image
See http://www.oracle.com/technetwork/java/javase/documentation/index.html for more details.
其中 'user.timezone = ’ 就是时区的设置,没有赋值即取系统默认时间,所以理论上将这里给定一个时区的固定值,也可以解决问题,但是我觉得这样做并不十分优雅。所以考虑第三种方案
3、既然没赋值是取的系统默认时间,那到底是从哪取的系统默认时间???
原来真正的系统时区在 /etc/timezone 中,JDK也是从这里取的系统时间,于是,我把这里的时区改成了 Asia/Shanghai,重启服务器,时差的问题终于解决了。
三、部署tomcat(设置开机自启)
我这里没有用集成运维环境,因为是之前的项目迁移,也就没想做过复杂的操作。
直接把之前的tomact拷贝过来就OK了。但是在设置tomcat开机自启上又出现了问题,在centos中,我用的chkconfig做任务管理,但是ubuntu中没有chkconfig,有人说用update-rc.d,但是不知道为什么,我这就是死活无效。
最后安装sysv-rc-conf解决问题
1、安装
sudo apt-get install sysv-rc-conf
2、放置服务,把需要自启动的脚本放置的 /etc/init.d 目录下(假设任务脚本名称为:tomact),并将脚本权限改为755或777
#设置脚本可执行权限
sudo chmod 755 /etc/init.d/tomcat
3、管理启动任务
#用命令执行
sudo sysv-rc-conf tomact on
#或者用图形化界面控制,图形化界面上有操作说明
sudo sysv-rc-conf
将你的目标任务的2~5 项都通过点击“空格键”,给它打上X,然后再输入q退出,就OK了。
这里的2-5是对应Ubuntu系统运行级别的:
0 停机
1 单用户,Does not configure network interfaces, start daemons, or allow non-root logins
2 多用户,无网络连接 Does not configure network interfaces or start daemons
3 多用户,启动网络连接 Starts the system normally.
4 用户自定义
5 多用户带图形界面
6 重启
有的文章中提到要设置软连接啥的,我这没用到,只要将脚本文件放在 /etc/init.d 中,sysv-rc-conf就可以扫描到,设置完成后,就会在etc路径下的rc2.d~rc5.d中自动挂载软连接
下面附上我的启动脚本
注意:1、停止和重启部分的脚本我没测,因为主要要求随系统启动 所以大家谨慎观看
2、如果脚本文件是在Windows电脑中编辑的,编码格式多半会变成Windows(CR LF),这种格式在Linux中是不能启动的,最起码会报一个/bin/bash^M啥的路径错误。需要在编辑器中把编码格式变成Unix(LF)。
#!/bin/bash
# description: Tomcat Start Stop Restart
# processname: tomcat-service
# sysv-rc-conf: 2345 21 81 指Linux级别需要启动httpd(3,4,5);启动序号(99);关闭序号(10)。
#idea - tomcat-service config start
#!/bin/bash
# description: Tomcat Start Stop Restart
# processname: tomcat-service
# sysv-rc-conf: 2345 20 80
# 配置 jdk 的安装目录
JAVA_HOME=/usr/local/jdk1.8.0_192
export JAVA_HOME
PATH=$JAVA_HOME/bin:$PATH
export PATH
# 配置 tomcat 的安装目录
CATALINA_SHENHE_HOME=/home/hbu_guest_shenhe/apache-tomcat-8.5.79
CATALINA_SERVER_HOME=/home/hbu_guest_server/apache-tomcat-8.5.79
case $1 in
start)
sh $CATALINA_SHENHE_HOME/bin/startup.sh
sleep 2
sh $CATALINA_SERVER_HOME/bin/startup.sh
;;
stop)
pid=`ps -ef | grep java | grep -w $CATALINA_SHENHE_HOME | awk '{print $2}'`
echo -e "$tocmat"——"$pid"
kill -9 $pid
sleep 2
pid=`ps -ef | grep java | grep -w $CATALINA_SERVER_HOME | awk '{print $2}'`
echo -e "$tocmat"——"$pid"
kill -9 $pid
;;
restart)
pid=`ps -ef | grep java | grep -w $CATALINA_SHENHE_HOME | awk '{print $2}'`
kill -9 $pid
sleep 2
bash "$CATALINA_SHENHE_HOME"/bin/startup.sh
pid=`ps -ef | grep java | grep -w $CATALINA_SHENHE_HOME | awk '{print $2}'`
echo -e "$tocmat"——"$pid"
sleep 2
pid=`ps -ef | grep java | grep -w $CATALINA_SERVER_HOME | awk '{print $2}'`
kill -9 $pid
sleep 2
bash "$CATALINA_SERVER_HOME"/bin/startup.sh
pid=`ps -ef | grep java | grep -w $CATALINA_SERVER_HOME | awk '{print $2}'`
echo -e "$tocmat"——"$pid"
;;
esac
exit 0
#chmod 755 tomcat-service
#sysv-rc-conf --add tomcat-service
#sysv-rc-conf --level 2345 tomcat-service on
更多推荐
所有评论(0)