首页 > 数据库 >【MSSQL】SQL SERVER导入中文乱码问题解决

【MSSQL】SQL SERVER导入中文乱码问题解决

时间:2022-11-23 17:03:05浏览次数:49  
标签:kB root ubuntu SERVER 乱码 SQL xenial com mssql

公司最近承接了一个项目,甲方现使用旧版SiteServer框架(以下简称“SiteCMS”)作为门户网站,使用的数据源是SQL Server。

现在需要对SiteCMS进行升级,在升级时数据库和数据库结构也需要同时更新,其中数据库要求从现在Windows Server服务器的SQL SERVER迁移到CentOS的Docker版SQL SERVER中以便可以Docker化统一管理(这个别人家的管理模式就不作深究了)。

为了确保工作万无一失,需要将数据备份还原到本地环境进行预演(已经过用户授权)。但是导入到测试环境时却出现中文乱码的情况。如下图所示:【MSSQL】SQL SERVER导入中文乱码问题解决_sql

本地环境模拟的镜像是“mcr.microsoft.com/mssql/server:2017-latest”,这个镜像也将会在后面生产环境下使用。【MSSQL】SQL SERVER导入中文乱码问题解决_sql_02

先查询一下当前数据库的默认编码,如下图:

【MSSQL】SQL SERVER导入中文乱码问题解决_sql_03

干过开发的都知道这个Latin1编码方式就是中文乱码的主要原因了。好了,现在问题也清晰了,就一句话“要怎样才能够在没有SQL SERVER管理器(Mac环境)的情况下修改Docker版SQL SERVER的编码规则呢?”,下面将详细讲讲这些走过的“弯路”。

按照网上的说法,首先先进入容器并切换到/opt/mssql/bin目录,如下所示:

yuanzhenhui@MacBook-Pro ~ % docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6ce7e5e399ba 314918ddaedf "/opt/mssql/bin/sqls…" 8 months ago Up 12 hours 0.0.0.0:1433->1433/tcp mssql
yuanzhenhui@MacBook-Pro ~ % docker exec -it 6ce7e5e399ba bash
root@6ce7e5e399ba:/# cd /opt/mssql/bin
root@6ce7e5e399ba:/opt/mssql/bin# ls
compress-dump.sh crash-support-functions.sh generate-sql-dump.sh handle-crash.sh mssql-conf paldumper sqlservr
root@6ce7e5e399ba:/opt/mssql/bin# ls -al
total 32280
drwxrwxr-x 2 root root 4096 Dec 17 2018 .
drwxrwxr-x 4 root root 4096 Dec 17 2018 ..
-rwxrwxr-x 1 root root 409 Dec 17 2018 compress-dump.sh
-rwxrwxr-x 1 root root 16447 Dec 17 2018 crash-support-functions.sh
-rwxrwxr-x 1 root root 1615 Dec 17 2018 generate-sql-dump.sh
-rwxrwxr-x 1 root root 1886 Dec 17 2018 handle-crash.sh
-rwxrwxr-x 1 root root 1294 Dec 17 2018 mssql-conf
-rwxrwxr-x 1 root root 31384736 Dec 17 2018 paldumper
-rwxrwxr-x 1 root root 1618456 Dec 17 2018 sqlservr
root@6ce7e5e399ba:

在该目录下有一个mssql-conf的可执行文件,然后通过“./mssql-conf set-collation”指令就能够设置你需要的编码,如下所示:

root@6ce7e5e399ba:/opt/mssql/bin# ./mssql-conf set-collation
Enter the collation: Chinese_PRC_CI_AS
Configuring SQL Server...

/opt/mssql/lib/mssql-conf/invokesqlservr.sh: line 13: sudo: command not found
root@6ce7e5e399ba:/opt/mssql/bin#

(⊙o⊙)…这里说我没有sudo执行不了?好吧,那就装个sudo吧。还好镜像是用Ubuntu操作系统为基础的,用apt-get就能够轻松安装。如下所示:

root@6ce7e5e399ba:/opt/mssql/bin# apt-get update
Get:1 http://archive.ubuntu.com/ubuntu xenial InRelease [247 kB]
Get:2 http://security.ubuntu.com/ubuntu xenial-security InRelease [99.8 kB]
Get:3 https://packages.microsoft.com/ubuntu/16.04/prod xenial InRelease [4010 B]
Get:4 https://packages.microsoft.com/ubuntu/16.04/prod xenial/main amd64 Packages [323 kB]
Get:5 http://security.ubuntu.com/ubuntu xenial-security/universe Sources [256 kB]
Get:6 http://archive.ubuntu.com/ubuntu xenial-updates InRelease [99.8 kB]
Get:7 http://archive.ubuntu.com/ubuntu xenial-backports InRelease [97.4 kB]
Get:8 http://archive.ubuntu.com/ubuntu xenial/universe Sources [9802 kB]
Get:9 http://security.ubuntu.com/ubuntu xenial-security/main amd64 Packages [2051 kB]
Get:10 http://security.ubuntu.com/ubuntu xenial-security/restricted amd64 Packages [15.9 kB]
Get:11 http://security.ubuntu.com/ubuntu xenial-security/universe amd64 Packages [984 kB]
Get:12 http://security.ubuntu.com/ubuntu xenial-security/multiverse amd64 Packages [8820 B]
Get:13 http://archive.ubuntu.com/ubuntu xenial/main amd64 Packages [1558 kB]
Get:14 http://archive.ubuntu.com/ubuntu xenial/restricted amd64 Packages [14.1 kB]
Get:15 http://archive.ubuntu.com/ubuntu xenial/universe amd64 Packages [9827 kB]
Get:16 http://archive.ubuntu.com/ubuntu xenial/multiverse amd64 Packages [176 kB]
Get:17 http://archive.ubuntu.com/ubuntu xenial-updates/universe Sources [548 kB]
Get:18 http://archive.ubuntu.com/ubuntu xenial-updates/main amd64 Packages [2560 kB]
Get:19 http://archive.ubuntu.com/ubuntu xenial-updates/restricted amd64 Packages [16.4 kB]
Get:20 http://archive.ubuntu.com/ubuntu xenial-updates/universe amd64 Packages [1544 kB]
Get:21 http://archive.ubuntu.com/ubuntu xenial-updates/multiverse amd64 Packages [26.2 kB]
Get:22 http://archive.ubuntu.com/ubuntu xenial-backports/main amd64 Packages [10.9 kB]
Get:23 http://archive.ubuntu.com/ubuntu xenial-backports/universe amd64 Packages [12.7 kB]
Fetched 30.3 MB in 33s (908 kB/s)
Reading package lists... Done
root@6ce7e5e399ba:/opt/mssql/bin# apt-get install sudo
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following NEW packages will be installed:
sudo
0 upgraded, 1 newly installed, 0 to remove and 96 not upgraded.
Need to get 390 kB of archives.
After this operation, 1626 kB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu xenial-updates/main amd64 sudo amd64 1.8.16-0ubuntu1.10 [390 kB]
Fetched 390 kB in 1s (225 kB/s)
debconf: delaying package configuration, since apt-utils is not installed
Selecting previously unselected package sudo.
(Reading database ... 8900 files and directories currently installed.)
Preparing to unpack .../sudo_1.8.16-0ubuntu1.10_amd64.deb ...
Unpacking sudo (1.8.16-0ubuntu1.10) ...
Setting up sudo (1.8.16-0ubuntu1.10) ...
root@6ce7e5e399ba:/opt/mssql/bin#

然后就又出现了另一个问题,如下图:

root@ff6113eba616:/opt/mssql/bin#sudo ./mssql-conf set-collation
Enter the collation: Chinese_PRC_CI_AS
Configuring SQL Server...

No passwd entry for user 'mssql'
root@ff6113eba616:

mssql用户没有密码?按照这个提示又去网上找了一圈,下面就按照Stack Overflow里面的解答来进行了操作,如下所示:

root@a4b1b86ad87c:/# useradd mssql
root@a4b1b86ad87c:/# mkdir -p /var/opt/mssql
root@a4b1b86ad87c:/# chmod -R 777 /var/opt/mssql
root@a4b1b86ad87c:/opt/mssql/bin# echo "Chinese_PRC_CI_AS" | /opt/mssql/bin/mssql-conf set-collation
Enter the collation: Configuring SQL Server...

2022-11-22 05:35:39.66 Server Microsoft SQL Server 2017 (RTM-CU31) (KB5016884) - 14.0.3456.2 (X64)
Sep 2 2022 11:01:50
Copyright (C) 2017 Microsoft Corporation
Enterprise Edition (64-bit) on Linux (Ubuntu 18.04.6 LTS)
2022-11-22 05:35:39.68 Server UTC adjustment: 0:00
2022-11-22 05:35:39.68 Server (c) Microsoft Corporation.
2022-11-22 05:35:39.68 Server All rights reserved.
2022-11-22 05:35:39.69 Server Server process ID is 392.
2022-11-22 05:35:39.69 Server Logging SQL Server messages in file '/var/opt/mssql/log/errorlog'.
2022-11-22 05:35:39.70 Server Registry startup parameters:
-d /var/opt/mssql/data/master.mdf
-l /var/opt/mssql/data/mastlog.ldf
-e /var/opt/mssql/log/errorlog
2022-11-22 05:35:39.71 Server Command Line Startup Parameters:
-q "Chinese_PRC_CI_AS"
2022-11-22 05:35:39.71 Server Error: 17113, Severity: 16, State: 1.
2022-11-22 05:35:39.71 Server Error 5(Access is denied.) occurred while opening file '/var/opt/mssql/data/master.mdf' to obtain configuration information at startup. An invalid startup option might have caused the error. Verify your startup options, and correct or remove them if necessary.

WTF,master.mdf文件没有访问权限?已经执行了chmod -R 777的情况下居然没有权限?到了这一步实在没有办法,既然这样就回归本源不搞全局算了。

接着我这边就删除了容器并重新运行,先创建一个名为SiteCMS的数据库,接着直接对数据库进行编码的设定,如下图:【MSSQL】SQL SERVER导入中文乱码问题解决_sql_04

再之后重新导入数据【MSSQL】SQL SERVER导入中文乱码问题解决_sql_05

导入数据之后一切正常,ε=(´ο`*)))唉早知道就不折腾了。

标签:kB,root,ubuntu,SERVER,乱码,SQL,xenial,com,mssql
From: https://blog.51cto.com/u_15761576/5881506

相关文章