Ruby on Rails | Setup MS-SQL server in Ubuntu 17.04

MSSQL Server Installation

Follow these instructions https://www.rootusers.com/how-to-install-microsoft-sql-server-on-ubuntu-linux/

Note: During installation it will ask you for password, put a strong password remember it or note it down.

Reset Server Admin(SA) password

If you, unfortunately, forgot the SA password then you can reset follow the commands

$ sudo systemctl stop mssql-server
$ sudo /opt/mssql/bin/mssql-conf setup

Testing

If you wish to test if Server is installed and ready to receive requests

sqlcmd -S localhost -U sa -P your_strong_password

Install FreeTDS binaries

$ apt-get wget
$ apt-get install build-essential
$ apt-get install libc6-dev

$ wget ftp://ftp.freetds.org/pub/freetds/stable/freetds-1.00.21.tar.gz
$ tar -xzf freetds-1.00.21.tar.gz
$ cd freetds-1.00.21
$ ./configure --prefix=/usr/local --with-tdsver=7.3
$ make
$ make install

Install required gems

# SQL Server (2012 or higher required)
#
# Install the adapters and driver
#   gem install tiny_tds
#   gem install activerecord-sqlserver-adapter
#
# Ensure the activerecord adapter and db driver gems are defined in your Gemfile
#   gem 'tiny_tds'
#   gem 'activerecord-sqlserver-adapter'

If still, you cannot install the gem tiny_tds then try using sudo
$ sudo gem install tiny_tds -v '2.0.0'

Importing from Remote to Localhost db

Exporting from remote

bcp my_table_name out ~/my_table_name.txt -S yourdb.database.secure.windows.net -U yourusername -P 'YourStrongPassword123!!' -d mydbname -c -t ','

 

Import to Local

bcp my_table_name in ~/my_table_name.txt -S localhost -U sa -P 'PrashantBhusal1' -d my_local_db_name -c -t ',' -q

**Output When Successful**

Starting copy...

5 rows copied.
Network packet size (bytes): 4096
Clock Time (ms.) Total : 16 Average : (312.5 rows per sec.)

Useful links

https://askubuntu.com/questions/870927/sqlcmd-not-available-after-installing-mssql-tools

https://docs.microsoft.com/en-us/sql/linux/sql-server-linux-troubleshooting-guide

https://docs.microsoft.com/en-us/sql/linux/sql-server-linux-troubleshooting-guide#connection

How To Install Microsoft SQL Server On Ubuntu Linux

https://github.com/rails-sqlserver/tiny_tds#install

Leave a comment