2018-04-29

JAVA與MySQL的UTFB8MB4問題


1.升級MySQL 至少 > 5.5.3,並修改/etc/my.cnf,加入以下設定後,重開機
(JDBC預設會抓資料庫的character-set-server來做character判斷)
[client]
default-character-set = utf8mb4

[mysql]
default-character-set = utf8mb4

[mysqld]
character-set-client-handshake = FALSE
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci

2.修改資料table裡,將需要支援擴充字集的「欄位」改成utfbmb4_unicode_ci

3.升級 tomcat/lib裡的mysql-connector-java,至少 > 5.1.13,建議5.1.47(含)以上

4.tomcat修改context.xml,連線字串中加入
(characterEncoding=UTF8已預設相容UTF8MB4,此設定拿掉會直接讀資料庫的character-set-server,其實也沒影響)
(autoReconnect=true是為了能自動重新讀取資料庫設定,以避免讀到舊暫存設定)
useUnicode=true&autoReconnect=true&characterEncoding=UTF8

5.java正則表示式中,使用Pattern.compile("regex")似乎無法正常判斷unicode範圍20000 ~ 2A6D6 (42711字)及2F800 ~ 2FA1D (542字)的4 bytes字元,因此改用String.matches("regex"):
//是否全為漢字
str.matches("^[\\u4E00-\\u9FBB\\u3400-\\u4DB5\\uF900-\\uFAD9\\x{20000}-\\x{2A6D6}\\x{2F800}-\\x{2FA1D}\\(\\)]+$")

6.javascript的全漢字正則表示式:
!/^([\u4E00-\u9FBB|\u3400-\u4DB5|\uF900-\uFAD9]|[\uD800-\uDBFF][\uDC00-\uDFFF])+$/.test(STRING)

ref:
https://segmentfault.com/a/1190000000616820
http://seanlook.com/2016/10/23/mysql-utf8mb4/

CenOS上升級JRE及TOMCAT

切換為root
sudo -i

安裝tomcat9
cd /www
wget http://ftp.mirror.tw/pub/apache/tomcat/tomcat-9/v9.0.7/bin/apache-tomcat-9.0.7.tar.gz
tar zxvf apache-tomcat-9.0.7.tar.gz

新增tomcat使用者及群組
groupadd tomcat useradd -s /bin/false -g tomcat -d /www/apache-tomcat-9.0.7 tomcat

修改tomcat目錄owner及group
chwon -hR tomcat.tomcat /www/apache-tomcat-9.0.7

安裝jre
cd /usr/java
wget https://www.oracle.com/paht/to/jre/for/jdk-XXX-linux-x64.tar.gz
tar zxvf jre-XXX-linux-x64.tar.gz
alternatives --install /usr/bin/java java /usr/java/jdkXXX/jre/bin/java 300
rm latest
ln -s /usr/java/jdkXXX latest

修改jre路徑
update-alternatives --config java

移除原來jre
sudo update-alternatives --remove java /usr/java/jdk_oldXXX/jre/bin/java

重建並移除無用soft link
cd /etc/alternatives
ln -sf /usr/java/jdkXXX/jre/bin/* .
ln -sf /usr/java/jdkXXX/bin/* .
ln -sf /usr/java/jdkXXX/man/man1/* .
find -xtype l -delete

修改 /etc/profile
-----------------
...
export JAVA_HOME=/usr/java/jdkXXX
export CATALINA_HOME=/apps/tomcat-x.x.x
-----------------
source /etc/profile
java -version
echo $JAVA_HOME

修改 /etc/systemd/system/multi-user.target.wants/tomcat.service
將 jre跟tomcat路徑改掉
=====================
# Systemd unit file for tomcat
[Unit]
Description=Apache Tomcat Web Application Container
After=syslog.target network.target

[Service]
Type=forking
User=tomcat
Group=tomcat

#restart after failure
Restart=on-failure
RestartSec=5
TimeoutStartSec=60
TimeoutStopSec=60


export JAVA_HOME=/usr/java/jdk1.8.0_172
export CATALINA_HOME=/apps/tomcat-9.0.7

Environment=JAVA_HOME=/usr/java/jdk1.8.0_172
Environment=CATALINA_PID=/apps/tomcat-9.0.7/temp/tomcat.pid
Environment=CATALINA_HOME=/apps/tomcat-9.0.7
Environment=CATALINA_BASE=/apps/tomcat-9.0.7
Environment='CATALINA_OPTS=-Xms512M -Xmx4096M -server -XX:+UseParallelGC'
#Environment='JAVA_OPTS=-Djava.awt.headless=true -Djava.security.egd=file:/dev/./urandom'

ExecStart=/apps/tomcat-9.0.7/bin/startup.sh
ExecStop=/apps/tomcat-9.0.7/bin/shutdown.sh
#ExecStop=/bin/kill -15 $MAINPID

[Install]
WantedBy=multi-user.target
=====================
systemctl daemon-reload

=========
找出檔案的mod number
stat --format '%a' filename

找出有+s的檔案
find . -perm -4000
find . -perm -2000

ref: