从 SQL Server Management Studio 导出数据库脚本文件,在 linux 上通过 mssql-cli 命令导入
mssql-cli -i blog.sql
命令执行时报错
Traceback (most recent call last):
File "/mssql-cli/lib/python3.6/runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "/mssql-cli/lib/python3.6/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/mssql-cli/lib/python3.6/site-packages/mssqlcli/main.py", line 122, in <module>
main()
File "/mssql-cli/lib/python3.6/site-packages/mssqlcli/main.py", line 115, in main
run_cli_with(mssqlcli_options)
File "/mssql-cli/lib/python3.6/site-packages/mssqlcli/main.py", line 67, in run_cli_with
text = f.read()
File "/mssql-cli/lib/python3.6/codecs.py", line 321, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte
请问如何解决?
出现这种问题绝大部分情况是因为文件不是 UTF8 编码的(例如,可能是 GBK 编码的),而系统默认采用 UTF8 解码。解决方法是改为对应的解码方式。
极少数情况是因为文件损坏了或者和一部分非 UTF8 编码混在一起,可以修复文件或采用 replace 等方式解码。
的确是文件编码问题引起的,file 命令查看文件是 unicode(utf-16) 编码
$ file -i blog.sql
blog.sql: text/plain; charset=utf-16le
改为 utf-8 编码就解决了
file -i blog.sql
blog.sql: text/plain; charset=utf-8
为什么不直接用数据库复制功能呢
另外ssms也可以连接linux的数据库
https://docs.microsoft.com/zh-cn/sql/linux/sql-server-linux-manage-ssms?view=sql-server-ver15