How to change the port when calling sqlcmd - sql

I have this command
sqlcmd.exe -E -i "C:\joe-db-scripts\joe-db-tasks.Install.sql"
and I need to add the specific non-standard port of 32001. How do I do that? I was thinking of some flag like -p or something but I don't know the syntax.

According to this MSDN link, you'd use -S tcp:servername,32001.

That's it: How to Connect to the Database Engine using sqlcmd. Something like this:
sqlcmd -S ComputerA,1691

Following solution is working for me in SQL Server 2008 and above.
sqlcmd -S "hostname or IP,port number" -U SA -P password

Mayo/23/2022, works
sqlcmd -S "hostname or IP,port number"
SQL Express 2019 from linux ubuntu client connecting to sql express on windows server

Related

Why do I have to specify the default port in order to connect?

The following connection attempt works!
sqlcmd -S SOMEOTHERPC\SQLEXPRESS,1433 -Q "SELECT * WHERE Lol=1" -d "mydb"
the following does not work, it times out after 10 seconds and fails
sqlcmd -S SOMEOTHERPC\SQLEXPRESS -Q "SELECT * WHERE Lol=1" -d "mydb"
given that 1433 is the default port for sql server, why is it that I have to provide it? used to work without it?
many applications are getting this connection time out issue And i feel it is due to this?
had the default port been changes somewhere in sqlserver config?
EDIT:
Microsoft SQL Server 2014
Windows Embedded Standard 32bit (6.1 Build 7601)

Running queries using osql

When executing any one of the following commands:
osql -E -S ComputerName\InstanceName
osql -E -S ComputerName\InstanceName -i MyScript.sql -o MyOutput.rpt
osql -E -q "SELECT * FROM Northwind.dbo.Shippers"
osql -E -Q "SELECT * FROM Northwind.dbo.Shippers" -o MyOutput.rpt
I am getting the following error:
[SQL Server Native Client 10.0]SQL Server Network Interfaces: Connection
string is not valid [87].
[SQL Server Native Client 10.0]Login timeout expired
[SQL Server Native Client 10.0]A network-related or instance-specific error
has occurred while establishing a connection to SQL Server. Server is not
found or not accessible. Check if instance name is correct and if SQL Server
is configured to allow remote connections. For more information see SQL Server
Books Online.
However, I am able, without issue to login and run SELECT queries from SSMS.
How do I run queries against SQL Server 2008 using osql?
Do you have your logged in account set up as a user in SQL Server?
I usually work with specific accounts and SQL Server logins instead of Trusted Logins, and then just specify the database coordinates on the command line with the -S, -D, -U, and -P options:
osql -S %SERVERNAME% -U %USERNAME% -P %PASSWORD% -d %DBNAME%
For instance, if your server name is MyServer\SQL2008 and your user name is Foo and your password is Bar and your database is MyDB, then you'd use this:
osql -S MyServer\SQL2008 -U Foo -P Bar -d MyDB
And then continue on with the rest of your options after that.
If you really want to use your Trusted connection, you need to go to SQL Server Management Studio, and ensure your current Widows Login is added as a user and given appropriate permissions to your database, etc.
In SSMS, connect to your server manually (the 'sa' user and password perhaps), and then expand the "Security" node and look at the logins. If your currently logged in Windows User isn't listed, you'll want to right-click, add new Login, and add your current user.
Then you should be able to run with a Trusted Connection.
You have to run all command in a single line
like this
osql -E -S ComputerName\InstanceName -i MyScript.sql -o MyOutput.rpt
or
osql -E -S ComputerName\InstanceName -Q "SELECT * FROM Northwind.dbo.Shippers" -o MyOutput.rpt
Now you have to see if you can log in SQL Server or if the service is up or even if the TCP/IP protocol is enable
Use the value in the Server name: field for ComputerName\InstanceName. (e.g. MYPC\SQLEXPRESS)
Type:
osql -E -S MYPC\SQLEXPRESS
You will see the interactive prompt.
Then enter your commands:
USE pubs
GO
Also, you can use sqlcmd:
sqcmd -E -S MYPC\SQLEXPRESS

MySQLDump to local machine from remote server connected via SSH

mysqldump -h xxx.xxx.xxx.xxx -u username -ppassword databasename > C:\path\to\store\file
It seemed to work as it paused while the file was downloading, however no file appears once it completes.
Do I have something wrong in the command line?
Use like this:
mysqldump -P3306 -h192.168.20.151 -u root -p database > c:/my.sql
Hope to help you:)
Edition for linux
mysqldump -u root -p databasename > ~/Downlaods/filename.sql
Simply run mysqldump -h xxx.xxx.xxx.xxx -u username -ppassword databasename > C:\path\to\store\file from the command prompt on your local machine.
I don't understand why you involve ssh in your question but...
First try the same command without redirecting it to a file to see that you can connect to the database.
Second make sure that you can write to that location (try to create and edit a file in the same path).
If those to work your command should work.

Import a local SQL File To MySQL on a Remote Server Using SSH Tunnel

I have a connection between my localhost and a remote server using putty SSH tunnel.
Thats fine.
Now I need a command to get the sql file on my local machine i.e. c:\folder\test.sql and import it into mysql on the remote server
I thought maybe...
mysql -u prefix_username -p testpass -h localhost -P 3307 prefix_testdb
then do a command like
mysql -p testpass -u prefix_username prefix_testdb < c:\folder\test.sql
this command did not work.
How can I acheive this?
You should run this command
mysql -h host -u user_name -pPassword database < file.sql > output.log
file.sql contains the sql queries to run and output.log makes sense only when you have a query that returns something (like a select)
The only thing different I can see in your code is the blank space between the -p option and the password. If you use the -p option, you must write the password without leaving any blank space. Or you just can user the option --password=Password
I hope you can solve the problem
You will need to ssh to the remote machine with the mysql command appended:
ssh remote_user#remote_server mysql -p testpass -u username testdb < c:\folder\test.sql
1. mysql -h xxx -uxxx -pxxx . //login to the remote mysql
2. use DATABASE. //assign which db to import
3. source path/to/file.sql //the path can be your local sql file path.
Reference: Import SQL file into mysql
Use 'scp' to copy and mysql to insert to you local machine.
Syntax:
scp remote_user#remove_server:/path/to/sql/file.sql ~/path/to/local/directory
after you transfered the file use:
mysql -uYouUserName -p name_of_database_to_import_to < ~/path/to/local/directory/file.sql
mysql {mydbname} --host {server}.mysql.database.azure.com --user {login} --password={password} < ./{localdbbackupfile}.sql
As managed services, DevOps, and CI/CD workflows have become more popular by this point, most providers of those managed services want to remove the human error part of getting the connection strings correct. If you happen to be using Azure, AWS, GCP, etc, There usually is a page or terminal command that shows you these strings to help you easily integrate. Don't forget to check their docs if you're using something like that. They are auto generated, so they are most likely 'best practice' with spot-on correct syntax for the db version you may be using.
The above command is from "connection strings" on the product details page of my Azure Managed Mysql DB Server instance.
Not necessarily asked, but an fyi, a lot of those services auto generate templates for use in a lot of common connection scenarios:
{
"connectionStrings": {
"ado.net": "Server={server}.mysql.database.azure.com; Port=3306; Database=mytestdb; Uid={login}; Pwd={password};",
"jdbc": "jdbc:mysql://{server}.mysql.database.azure.com:3306/mytestdb?user={login}&password={password}",
"jdbc Spring": "spring.datasource.url=jdbc:mysql://{server}.mysql.database.azure.com:3306/mytestdb spring.datasource.username={login} spring.datasource.password={password}",
"mysql_cmd": "mysql mytestdb --host {server}.mysql.database.azure.com --user {login} --password={password}",
"node.js": "var conn = mysql.createConnection({host: '{server}.mysql.database.azure.com', user: '{login}', password: {password}, database: mytestdb, port: 3306});",
"php": "$con=mysqli_init(); [mysqli_ssl_set($con, NULL, NULL, {ca-cert filename}, NULL, NULL);] mysqli_real_connect($con, '{server}.mysql.database.azure.com', '{login}', '{password}', 'mytestdb', 3306);",
"python": "cnx = mysql.connector.connect(user='{login}', password='{password}', host='{server}.mysql.database.azure.com', port=3306, database='mytestdb')",
"ruby": "client = Mysql2::Client.new(username: '{login}', password: '{password}', database: 'mytestdb', host: '{server}.mysql.database.azure.com', port: 3306)"
}
}
You can use pscp to upload file to the server. Go to your command line and type this
pscp.exe c:\folder\test.sql usernameoftheserver#websitename.com:/serverpath

Not able to connect to the SQLCMD Utility

When I try to connect to the sql server using the sqlcmd utility, I use the following syntax:
sqlcmd -q "SELECT * FROM AdventureWorks2008R2.Person.Person"
I get this following error, can anyone pleae help on this.
HResult 0x2, Level 16, State 1
Named Pipes Provider: Could not open a connection to SQL Server [2].
Sqlcmd: Error: Microsoft SQL Native Client : An error has occurred while establi
shing a connection to the server. When connecting to SQL Server 2005, this failu
re may be caused by the fact that under the default settings SQL Server does not
allow remote connections..
Sqlcmd: Error: Microsoft SQL Native Client : Login timeout expired.
Supply the server you wish to connect to:
sqlcmd -S <server> -q "SELECT * FROM AdventureWorks2008R2.Person.Person"
For instance if you use SQL Server Express <server> would be .\SQLEXPRESS
For further options of sqlcmd use sqlcmd -?
sqlcmd -S <server> -d <database> -q "query"
this syntax worked perfectly for me...
First connect to SQL Server, as:
C:\>sqlcmd -U sa -P your_password
Then enter queries:
SELECT * FROM AdventureWorks2008R2.Person.Person;
When you want to exit out of sqlcmd, just type:
quit
and enter.