sqlcmd - connect to local instance of SQLEXPRESS - sqlcmd

I want to run a .sql batch script to delete records from a number of tables from cmd window. So, for that I am trying to connect to my local instance of SQLEXPRESS using following command-
sqlcmd -e -s \SQLEXPRESS
However, I am not able to connect and getting following error:
'Named Pipes Provider: Could not open a connection to SQL Server [2]'.
One more weird thing that I notice from the error message is that above command is trying to connect to SQL Server 2005 whereas the instance running on my machine is SQLEXPRESS 2008.
Can anyone please guide.
P.S. I am using Windows Authentication to connect to SQLEXPRESS which is when I go to connect using Mgmt Studio.

Sorry about the late response (just came across your question when searching for something unrelated), but it looks to me like you're just using the wrong case on your arguments:
sqlcmd -E -S .\SQLEXPRESS
-S means "server", whereas -s means "colseparator" (and similarly, lowercase e means echo rather than integrated auth)

Isn't it possible that the '.' is missing from your command?
sqlcmd -e -S .\SQLEXPRESS
EDIT:
Visit this page. There you fill find some instructions to get connected to your server. First check the Hresult error code in your error message.
I also tried it myself, because I could emulate your problem. I had to do the steps below in order to connect with sqlcmld
Go to All programs\Microsoft SQL Server\Configuration Tools\SQL Server Surface Area Configuration
In Services and connections select SQLEXPRESS\Database Engine\Remote connections
Check Local and remote connections and Using both TCP/IP and named pipes. Click OK.
Restart your database service.
Try to connect to the server:
sqlcmd -Snp:\\.\pipe\MSSQL$SQLEXPRESS\sql\query

Related

How to check if MySQL service is running with DBD::mysql

I am using the following DBD::mysql statement to connect to a MySQL database:
use DBI;
# Connect to the database.
my $dbh = DBI->connect("DBI:mysql:database=test;host=localhost",
"usr", "usr's password",
{'RaiseError' => 1});
Is there a way to check if MySQL service is running, before trying to connect to the database? What if the database is running on a remote server?
If you want a solution to check if the service is running without attempting to connect to it, you could use some Perl package to check the process table (works locally only), or check that the MySQL port (3306 by default) has a process listening to it.
I'm not sure what the purpose of this check is, because even if the service is running, the next thing you'll probably want to do is open a DB connection. Opening a DB connection is a quick and easy thing to do, and it has good error reporting if it doesn't work. So your intention to check that the service is running first is just unnecessary overhead.
I would just try to connect as you are doing. This is the most direct way of checking that the service is running, and it works both locally and remotely.
If there's an error, catch the error and interpret the error message. It'll be error 2002 (for localhost) or 2003 (for TCP/IP, whether it's the same host or a remote host).
These errors are mostly reliable. But there could be red herrings, for example if the service is running on a remote host, but your client host can't reach it because of firewalls or routing issues.
If you get an error 1045 (Access Denied), at least you know the service is running and you can reach it, the problem is only that your user & password are incorrect, or you tried to access a schema you don't have privilege to use.

Visual Studio Code mssql extention: SQL Server (mssql) - Unable to connect to server

I'm trying for the first time to get my project to run in Visual Studio Code. This includes a MySQL database. So I've installed the SQL Server (mssql) extension version 1.7.1. I follow the instructions here: https://learn.microsoft.com/en-us/sql/visual-studio-code/sql-server-develop-use-vscode?view=sql-server-ver15
I get to the section "Connect to SQL Server" and enter:
servername: localhost
db name: c3
Authentication type: Integrated
At the end of the section it should connect with the server but instead I get two errors:
mssql: Error "Unable to connect using the connection information
provided. Retry profile creation?"
mssql: Error 2: A network-related or instance-specific error occurred
while establishing a connection to SQL Server. The server was not
found or was not accessible. Verify that the instance name is correct
and that SQL Server is configured to allow remote connections.
(provider: Named Pipes Provider, error: 40 - Could not open a
connection to SQL Server)
Does anyone see what is going wrong here?
I've re-installed the extension but with the same result.
A MySQL db and Microsoft SQL Server db are two different things.
It seems that there's no SQL Server installed? You might install the free SQL Server Express Version, create the database in there and then connect from Visual Studio Code.
You have to install the database server yourself as VS Code is just a client. You might also use somekind of hosted offers (many webhosters offer a mysql server with self service installation, or use a cloud based database server). Yout can also use a local database (e.g. SqlLite) which is file based and doesn't need a special installation.
You should do the following
install SQL server in your machine and create Database here
Create a login and a database user here
If you are Using VS code install SQL Server (mssql) open new
connection, follow the prompts to specify the properties for the new
connection profile
If you want to login to your server to make sure of your data
sqlcmd -S localhost -U SA -P '<YourPassword>'

Save Asterisk CDR to an external host

I'm trying to save Asterisk's CDR to an external mysql host.
I've modified the the connection details in the FreePBX advanced settings section,
and I also tried editing the cdr_mysql.conf file in /etc/asterisk,
but FreePBX still can not connect to the external mysql host.
When I'm trying to log into the UCP I get the failed to connect DB with the connection string.
It looks like the string FreePBX tries to connect with is wrong, becuase when I set up a test .NET web site to try and open a connection to mysql it works, but I am using a different syntax in the connection string.
BTW, I can telnet the external host from my pbx machine and I know for sure that this is not a network issue.
How can I have my CDR saved to an external mysql host?
**Edit: I also copied the db structure from the original mysql server so it won't be an issue.
Thank You!
Check connection string, check firewall
You can use
mysql -h hostname -u user -ppassword databasename
from command line to check connection is really allowed and no firewall.
You can get db access info from /etc/amportal.conf or /etc/asterisk/freepbx.conf
If you still have issues after that check, you can enable debug in logger.conf

Cannot connect to MySQL Server database at localhost:3306

When I first set up MySQL Server in Netbeans everything worked well. Now one day I think I accidentally clicked something or my computer lost battery life when I was connected to MySQL Server, and after these event(s) I have not been able to connect to MySQL Server. I do not know for sure if either of the above events would have any effect on connecting to a MySQL Server database.
Anyway since these events I have found that I cannot "start" or "connect" to a MySQL Server databse. I am doing this all through Netbeans IDE.
Here is a picture of how I try to connect to my databaseL:
This is what the error message says after I click connect:
Unable to connect. Cannot establish a connection to jdbc:mysql://localhost:3306/twitterdata using com.mysql.jdbc.Driver(Unknown database 'twitterdata'). The name of the database I am trying to connect to is twitterdata
How can I get reconnected to the MySQL Server database "twitterdata"?
I am not familiar with MySQL, databases, servers and I have no idea how to get reconnected. If there is any other helpful information that I should provide let me know.
What MySQL tools can I run to find my problem?
Why would the error message tell me that the database is unknown?
If I got to C:\ProgramData\MySQL\MySQL Server 5.5\data. There is a folder named twitterdata.
Here are the contents of that directory:
Please let me know if you need more information or if you have other suggestions that would help!
Thanks for all the help!
If you are using windows system and facing the error 'unable to connect localhost 3306' at the connection time. First you have to need start MySQL services in 2 steps:
Press "Windows key + R" write "services.msc"
And run as admin start MySQL service.
There can be three issues:
1) Either your server isn't even running. Open your command prompt and enter mysql
2)Let's say it is , can you check if it's connecting on the correct port.
Like in the url, you are using 3306,
Try following this post:
How do I find out my MySQL URL, host, port and username?
3) Can you check what if there are any credentials to the database you are connecting?
https://netbeans.org/kb/docs/ide/install-and-configure-mysql-server.html

Unexpected error message from aspnet_regsql

I am trying to use the aspnet___regsql utility to install all the membership tables in a database on my local SQL Express 2008 instance. I can connect without problems in Management Studio, but when I try to list the databases in the aspnet_regsql interface i get an error message saying:
Failed to query a list of database names from the SQL Server.
Invalid object name 'sysdatabases'
What am I doing wrong? How do I fix this?
(Please tell me there's a smoother way than reinstalling the thing... :P)
I have solved the problem by bypassing the graphic interface and performing the same action directly in the command prompt, with flags specifying all options. They should look like this:
Connect using SQL Authentication
C:\>Windows\Microsoft.NET\Framework\v2.0.50727\aspnet_regsql.exe -S myHostingServer -A all -d myDatabase -U myUserID -P myPassword
Connect using Windows Authentication
C:\>Windows\Microsoft.NET\Framework\v2.0.50727\aspnet_regsql.exe -S myHostingServer -A all -d myDatabase -E
The difference is at the end of the lines, where SQL Auth. has -U myUsername -P myPassword while Windows Auth. has -E (which flags for Trusted Connection).
If you want to use the graphical interface, you need to make sure the user you are logged in as has the default db in SQL set to 'master' so it can connect directly to the sysdatabases table.
Ensure that in SQL Server Configuration Manager -> SQL Server Network Configuration -> Protocols for YOUR_SQL_SERVER_INSTANCE_NAME is enabled TCP/IP. NB. for applying this change need to restart YOUR_SQL_SERVER_INSTANCE_NAME
By default on Vista the current user on the machine is not a member of SysAdmin on SQL Server 2005/2008.
So here is the trick.
1-Open Sql Server Surface Area Configuration tool from C:\Program Files\Microsoft SQL Server 2005/2008.
2-When the screen pops up click on Add New Administrator.
-on top right make sure that User to provision=Username of the pc
-on your left (Available Privileges) select Member of SQL Server SysAdmin and click on the right arrow (>)
-click ok and you should be ready to go.
My name is Talley Ouro,Developer in Raleigh,NC
Blog: http://talleyblogs.blogspot.com/
Email:johnstalley#live.com
This issue is generally caused by leaving "Server" to be your machine name instead of the name of your SQL server instance. It's tricky to figure out because they autofill the text box to your machine name, which is needed but incomplete. This may lead you to think "Server" refers to the computer itself when they really mean SQL server instance. If you are connecting to your local machine you can generally append to their entry with the name of your SQL instance and everything will work as intended
IE LOCALHOST must become LOCALHOST\SQLEXPRESS or whatever you name your box\sqlserver