Laravel database connection to separate SQL Server - sql-server

I have a tricky one here that I'm looking for advice to see how much flexibility we have with a project.
We have a database that is hosted on a server locally that we can remote desktop into. I have the credentials and the domain example: SQL-CVW\BRADNAMS and I have the database name BRENT and the user and login that works for the program that the database feeds.
I can login to the remote desktop and open the database using SQL Server Management Studio but I need to be able to access it through Laravel.
I have tried using the sqlsrv setup in my .env file and I have the pdo_sqlsrv and sqlsrv php extensions installs.
I try to run php artisan migrate and I get the following error:
SQLSTATE[08001]: [Microsoft][ODBC Driver 17 for SQL Server]TCP Provider: The wait operation timed out.
(SQL: select * from sysobjects where type = 'U' and name = migrations)

Related

Drupal 9 SQL Server connection error upon installing locally

As per multiple documents I am able to configure the SQL Server and copy drivers to ext folder
Now when I am trying to install Drupal I am getting an error
Resolve all issues below to continue the installation. For help configuring your database server, see the installation handbook, or contact your hosting provider.
Failed to connect to your database server. The server reports the following message: SQLSTATE[08001]: [Microsoft][ODBC Driver 17 for SQL Server]Named Pipes Provider: Could not open a connection to SQL Server [2].
Is the database server running?
Does the database exist, and have you entered the correct database name?
Have you entered the correct username and password?
Have you entered the correct database hostname?
Enter image description here
I have only added a username for the password used blank for the default installation of SQL.

Polybase external data source using ODBC, login failed error

Using SQL Server 2019 Enterprise Edition (CU14)
I am attempting to use Polybase for reading data from a simple CSV text file.
When executing the create external data source I get the error "Msg 46721, Level 20, State 1 Login failed. The login is from an untrusted domain and cannot be used with Integrated authentication."
The statement I'm using is:
create external data source datasourcename
with (
location = 'odbc:\\localhost',
connection_options='Driver=Microsoft Access Text Driver (*.txt, *.csv); Dbq=d:\data\; ApplicationIntent=ReadOnly;',
pushdown=off,
credential = <credential name> -- < On DEV server it works both with/without this
);
This actually works on my development server, however on the production server it is throwing this error and I can't find a solution. Not really sure what is trying to be logged in to!
On the Dev server (also SQL server 2019/CU14) I can even leave off the credential option and it still works.
Both servers are using the same domain account for the SQL Server service and Polybase services, I have defined the credential identically as:
create database scoped credential <credential name> with identity = 'Sql Server service domain account', secret = 'domain account password';
I don't think it's relevant but the production server is an Availability-Group secondary replica where I'm trying a simple proof-of-concept to test reading a CSV file and inserting data into TempDB.
On both my Dev server and Production server the file I'm trying to read is located in a folder on a local drive. The drive & folder has full control permissions for the SQL Server Service account.
Both servers have the same odbc driver installed, version 16.00.4999.1000
I have tried adding "trusted_connection=Yes and No" to the connection_options.
Connecting with SSMS I have tried using my own domain account and the SQL Server sa account.
I have installing the "Kerberos Configuration Manager" and connecting to the production server to check the SPNs and it reports "status good".
I have tried adding "DisableLoopbackCheck" registry key on the server.
So far nothing has worked and I'm at a loss as to what else to try.
The external data source definition is wrong: use forward slashes, don't use localhost, the odbc string is missing the curly braces, application intent is not a property of the odbc, and you don't need a credential. See this link for an example: https://36chambers.wordpress.com/2022/02/21/polybase-versus-flat-files/

How to connect JDBC to Microsoft SQL Server in JMeter?

How to connect JDBC to Microsoft SQL Server so that I can test my SQL Server database in JMeter.
I am using Apache Jmeter 2.13. I have put the sqljdbc4.jar file in JMeter/lib folder. I also created a very simple database in Microsoft SQL Server Management Studio where my server name is localhost and I have used windows authentication to connect to my server. Then in JMeter I have used the following properties in JDBC Connection Configuration :
Database URL:jdbc:sqlserver://localhost:1433;DatabaseName=JmeterTestDatabase.
JDBC Driver Class:com.microsoft.sqlserver.jdbc.SQLServerDriver.
Username:ABC-PC\ABC.
Password: Somepassword.
When I run the test I get the following error:
com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user
'ABC-PC\ABC'. ClientConnectionId:7c9e308b-1bdc-4513-8386-5bc51f249029
What is going wrong here?
Log in failed is a clear message we are seeing , please check it once again.
TRY this as well, I have given below by assuming username as "ABC-PC\ABC" and password as "Somepassword" change accordingly.
Database URL:jdbc:sqlserver://ABC-PC\ABC/Somepassword#localhost:1433;DatabaseName=JmeterTestDatabase.
JDBC Driver Class:com.microsoft.sqlserver.jdbc.SQLServerDriver.

Migrate SQL Server CE to Azure

I have finished my first complete Orchard CMS website, developed locally in SQL Server CE. I want to publish it to Azure, but I am having difficulty with the WebMatrix Migrate function.
I have a SQL Server instance in Azure with an blank Orchard database already there (I want to use the replace option late in the migration to just nuke the existing db). I have added my IP to the server firewall rule.
In WebMatrix 3 I can connect to my Azure database using:
Server: myserver.database.windows.net
Database: orchard-db
Username: admin#myserver
Password: mypassword
I can view the tables in WebMatrix.
When I select my CE sdf file (Orchard.sdf) and click the Migrate button and enter the exact same details I get the following (can provide full stack trace if needed):
We were unable to connect using the connection details provided.
Microsoft.SqlServer.Management.Common.ConnectionFailureException:
Failed to connect to server . ---> System.Data.SqlClient.SqlException:
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)
---> System.ComponentModel.Win32Exception: The network path was not found
I'm really not sure what the issue is. I have tried various configurations of the details but nothing seems to work. If I connect as my database user (i.e. not a server admin) I get a different error:
The server principal "orchard_user" is not able to access the database "master" under the current security context.
I went and had something to eat, and now I can get to the next step of the migration. When I select replace database I get the following error:
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> Microsoft.SqlServer.Management.Common.ConnectionFailureException: Failed to connect to server . ---> System.Data.SqlClient.SqlException: Login failed for user 'admin'.
EDIT: I have come to the conclusion WebMatrix is not going to migrate my SQL Server CE database to my Azure SQL Server database. I installed SQL Server Express and migrated it to that, when I get home from work I will use the SQL Server Migration Assistant to move it to Azure.
I ended up installing SQL Server Express, using WebMatrix to migrate the SQL Server CE database to that. I then ran the Deploy Database to Microsoft Azure Database Wizard in SSMS to push my database to Azure.
I can't be certain, but it seems that WebMatrix can't migrate CE to Azure. According to Azure doco:
It is recommended that you always use the latest version of Management Studio to remain synchronized with updates to Microsoft Azure and SQL Database. Update SQL Server Management Studio.
I guess WebMatrix is behind or something. Hopefully this helps someone in my situation.

Connecting to SQL Server using PHP, ODBC, and Windows authentication

I'm trying to use PHP to connect to an ODBC data source using Windows authentication. I can connect just fine to the server in SQL Server so I know it's running. When I try to run the command
$link = odbc_connect("my_odbc","","");
I get the error:
"Warning: odbc_connect(): SQL error: [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Login failed for user ''., SQL state 28000 in SQLConnect in C:\Users..."
I tried:
$link = odbc_connect("Driver={ODBC Driver 11 for SQL Server};
Server='my_odbc';Integrated Security=SSPI","","");
Which returned the message:
Warning: odbc_connect(): SQL error: [Microsoft][ODBC Driver 11 for SQL Server]Named Pipes Provider: Could not open a connection to SQL Server [53]. , SQL state 08001 in SQLConnect in C:\Users..."
Not sure what I'm doing wrong.
my_odbc is a SQL Server (2008) on a different machine. I don't have admin privileges on that database so I can't change anything on that end (such as enabling SQL Server authentication).
I am running Windows 7 and using PHP Version 5.6.12
phpinfo() indicates that ODBC Support is enabled as well as pdo_sqlsrv support
Maybe you should try PDO (the performance difference isn't that great) with SQLsrv plugin (this what I'm using to connect to my other boss' software which use SQL Server 2008 database):
$connection = new PDO("sqlsrv:Server=" . $this->sourceServer . ";Database=" . $this->sourceDB, $this->sourceUser, $this->sourcePW);
$connection->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
You can download the plugin here
https://www.microsoft.com/en-ca/download/details.aspx?id=36434
In Xampp, you must copy the dll in that folder:
C:\xampp\php\ext
And you must add that line to your php.ini
extension = php_pdo_sqlsrv_56_ts.dll
Note: Don't forget to restart your server so it can take in account the new php.ini file.
Let me know if it works for you
This error is a General Error that arises due to unable to establish connect to the server for some reasons. Its important to know what server are you using with PHP and the application used with it. Example Apache, Xampp or Wamp etc.
Here are somethings you could try like.
Let me brief, below are the reference links.
Check whether it pings
Enable TCP/IP connection SQL Server Configuration if not enabled. To do so, Open SQL Server Configuration Manager -> SQL Server Network
Configuration->Protocols for SQL server->TCP/IP(set to enabled).
Restart SQL services.
Enable Remote Connection from server
Open the Port, To do so Windows Firewall Settings-> Exceptions -> add a Port (Name:SQL;Port:1433;TCP) then from Exceptions tick SQL and save.
Enable running browser services.SQL Server Configuration Manager -> SQL Server Services - > SQL Server Browser set to running.
defining Port in connection string
add instance name with machine name if more than one instance is being used
sample
$user = 'username';
$pass = 'password';
//Use the machine name and instance if multiple instances are used
$server = 'serverName\instanceName';
//Define Port
$port='Port=1433';
$database = 'database';
$connection_string = "DRIVER={ODBC Driver 11 for SQL Server};SERVER=$server;$port;DATABASE=$database";
$conn = odbc_connect($connection_string,$user,$pass);
here are useful links that i found regarding the issue
resolving could not open a connection to sql server-errors
sql server provider named pipes provider error
Have a look at these links too
Named Pipes Provider: Could not open a connection to SQL Server [53]
Someone had the same error message and asked and answered his own question here.
To quote...
I´m sorry for the troubles.
The problem was, that I was using SQL Server Native Client 11.0 as driver. I switched it to SQL Server and now it works :/
Hopefully this at least helps someone, being in a similar problem....
A significant omission from your question is how you are running PHP and with which credentials.
Assuming you are using IIS or Apache (or another web server), then your PHP process is probably running under the local system account:
Since this account is local it doesn't have any authorisation to access your remote SQL Server.
You could alter the IIS/Apache service to run with the credentials (yours?) that are authorised to connect to SQL Server, but be aware this might cause other permissions issues with the service as well as being a problem if you change your password in the future.
Try running a test script from the command line (which should run with your credentials by default) to determine where the problem is:
php -r "var_dump(odbc_connect(...));"
Look at the regedit HKLM/SOFTWARE/ODBC
what is your folder name ?
If there is a "SQL Server Native Client 11.0" then you have to write your php code like
odbc_connect("Driver={SQL Server Native Client 11.0};Server=ip;Database=db;", "user", "pass");
However some of the server like server 2008 will be record this odbc like
ODBC Driver 11 for SQL Server
Then you have to replace SQL Server Native Client 11.0 by ODBC Driver 11 for SQL Server.
You have to create an ODBC interface first. In principle, this should also work without a user and pw. If defined in interface.
$pdo = new PDO('odbc:dsnName','user','pw');
Enable in IIS on Manager PHP o PDO_ODBC:
I have authenticated by Windows with following PHP statement:
$ Conn = new PDO ("odbc: Driver = {SQL Server}; Server = JAMILI-PC \ SQLEXPRESS; null; null");
I am using Windows 2008.
I hope it solves your problem.

Resources