I'm writing a small deployment SQL script for my first database-driven app.
In the process, I find that I repeat myself a lot, for instance:
GRANT USAGE ON *.* TO 'foo'#'localhost';
DROP USER 'foo'#'localhost';
CREATE USER 'foo'#'localhost' IDENTIFIED BY 'password';
It would be fantastic if I could use a variable or a macro to replace commonly occurring data. Is it possible to implement something like the the following snippet?
#define USER 'foo' #or "Type USER = 'foo'"
#define HOST 'localhost' #or "Type HOST = 'localhost'"
GRANT USAGE ON *.* TO USER#HOST
DROP USER USER#HOST
CREATE USER USER#HOST IDENTIFIED BY 'password'
Most SQL databases have some kind of bind variables that you can use for that.
For instance, in PostgreSQL you use the \set command in plsql:
\set user = foo
drop user :user;
create user :user identified by 'password';
However, I am not sure if MySQL have something like that. It do have variables, and since the host and user is a string, you might be able to do something like this:
select #user = 'foo';
select #host = 'localhost;
drop user #user##host;
create user #user##host identified by 'password';
If variables doesn't work with the drop and create user statements, you can always modify the mysql.user table directly, just don't forget to execute flush privileges after.
insert into user values(host,#user,PASSWORD('some_pass'),
'Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y');
flush privileges;
You can certainly do something like:
SET #user='foo';
Related
We are trying to copy the data frame to Teradata specific database and the script is not accepting the schema_name parameter. Data Copy to User Database, which used in logon command is happening. But I tried to override the default and specifying Database Name in the copy_to_sql it is failing.
from teradataml import *
from teradataml.dataframe.copy_to import copy_to_sql
create_context(host = "ipaddrr", username='uname', password = "pwd")
df = DataFrame.from_query("select top 10* from dbc.tables;")
copy_to_sql(df = df ,table_name = 'Tab', schema_name='DB_Name',if_exists = 'replace')
Error: TeradataMlException: [Teradata][teradataml](TDML_2007) Invalid value(s) 'DB_Name' passed to argument 'schema_name', should be: A valid database/schema name..
Do you have a database / user named DB_Name? If not, try creating the database first and then running your copy script:
CREATE DATABASE DB_NAME FROM <parent_DB> AS PERMANENT = 1000000000;
I don't think the utilities / packages will typically create a database for you on the fly, since it can be a more involved operation (locks, space allocation, etc.) than creating a table.
Need to query a specific key in the HKLM hive for security permissions. We do not want to change the permissions, just log them to a file. To start I would be satisfied with using WriteLine or a msgbox to display the permissions on the key. I'm new to VB.net. I can't find an example of querying keys only adding them, modifying permissions. Can RegistryRights.FullControl be used to return True or False? If so, can someone provide a link online that has a good example?
I have searched for hours online and have tried to modify the examples here to only perform a query on a key, but I cannot create a query on registry key permissions. https://learn.microsoft.com/en-us/dotnet/api/system.security.accesscontrol.registryrights?view=netframework-4.8
Here is a sample of code to attempt permission changes. But we want to query, not change permissions.
' Prevent the current user from writing or changing the
' permission set of the key. Note that if Delete permission
' were not allowed in the previous access rule, denying
' WriteKey permission would prevent the user from deleting the
' key.
rs.AddAccessRule(New RegistryAccessRule(user,
RegistryRights.WriteKey Or RegistryRights.ChangePermissions,
InheritanceFlags.None, PropagationFlags.None, AccessControlType.Deny))
Expected output would be something like this where the field is True or false or 983103 or some other value:
Console.WriteLine("Regkey rights on HKLM\Software\wow6432node\somekey\ " & RegistryRights.FullControl)
In case someone is trying to do the same thing, I found this Sysinternals Application that does exactly what we need to do.
https://learn.microsoft.com/en-us/sysinternals/downloads/accesschk
We'll include it with our app and call it.
Sample command we'll be using
accesschk.exe -k users hklm\software\wow6432node\somekeyhere
In liquibase, "author" is normally hardcoded in the changeset. But we want to set it to the db user against which the changeset is being run. So we will have the author dev_schema in dev, and prod_schema in prod and so on. The db user are not known beforehand, so we like to set at runtime automatically from --username option of liquibase connection string.
./liquibase.bat --driver=oracle.jdbc.OracleDriver --changeLogFile="changelog.xml" --url="jdbc:oracle:thin:#localhost:1521:xe" --username=dev_schema ...
In the changeset tag I set the attribute author to ${username} but it is not picked up.
<changeSet author="${username}" ...
Also tried setting the environment variable, which worked, but then you have to set the same username twice. There is also a risk that if someone uses a different username, liquibase will fail to execute due to checksum failure.
Is that possible? Alternatively, any way around?
I guess that username is liquibase's system parameter and it is not used in changelog for placeholder replacement. Try to specify parameter like java param -Dusername=<your user> and let's see what will happen.
We have a DACPAC that has a create user script.
The user has a log in that needs to be set to a windows domain user
When we do a build for Test/Staging/Release we need to be able to apply a different domain and user for the users log in.
I thought we might be able to use SQLCMD variables but I just get a SQL71501 Error when trying to use this That script looks something like this:
CREATE USER [Username]
For Login [$(SQLLoginDomain)]
WITH DEFAULT_SCHEMA = [SCHEMANAME]
GO
My advice based on bitter experience is to keep anything that is environment specific out of your SQL Server Database Project. Rather, apply anything that is environment specific (permisins etc) as a separate T-SQL script after the DACPAC has been deployed. If you are doing this with Release Management I have a soup-to-nuts blog series that includes publishing DACPACs and separate permissions scripts here.The post about applying a tokenised permissions script is here.
We ended up solving this by using a post deployment script in the dacpac and as long as you do a check before trying to call the user to see if they already exist it all works as that is the only place you can use them bar the pre deployment script.
You can read the Windows domain from the sys.dm_exec_sessions system view.
For example, you could put something like this in the DACPAC's post-deployment script...
DECLARE #host_name nvarchar(128);
DECLARE #nt_domain nvarchar(128);
SELECT
#host_name = host_name,
#nt_domain = nt_domain
FROM
sys.dm_exec_sessions
WHERE
session_id = ##SPID;
-- If the domain is present, setup the login/user.
-- Otherwise, we are probably running under test environment, so we can skip this.
-- `#nt_domain = #host_name` tends to hold for LocalDB.
IF #nt_domain IS NOT NULL AND #nt_domain <> #host_name BEGIN
DECLARE #user_nonquoted nvarchar(128) = #nt_domain + '\YourUserName';
DECLARE #user nvarchar(128) = QUOTENAME(#user_nonquoted);
IF #user_nonquoted NOT IN (SELECT name FROM sys.database_principals)
EXEC ('CREATE LOGIN ' + #user + ' FROM WINDOWS');
IF #user_nonquoted NOT IN (SELECT name FROM sys.server_principals)
EXEC ('CREATE USER ' + #user + ' FOR LOGIN ' + #user);
END
Is anyone using Cacti to monitor SQL server counters (disk queue length, i/o requests etc).
If you are, how did you go about accomplishing this? Basically I gather a number of performance counters on my SQL Servers. I need a way to create graphs and slice and dice the data that I have gathered? If you know of any other graphing solutions let me know?
Yes, done this a few times:
http://docs.cacti.net/usertemplate:host:microsoft:sqlserver
It works really well. You need access to create a login. This is the script you run which is not invasive:
/* SQL 2005/2008 */
USE [master]
GO
CREATE LOGIN [cactistats] WITH PASSWORD=SomePassword, DEFAULT_DATABASE=[master], DEFAULT_LANGUAGE=[us_english], CHECK_EXPIRATION=OFF, CHECK_POLICY=OFF
GO
EXEC sys.sp_addsrvrolemember #loginame = N'cactistats', #rolename = N'processadmin'
GO
CREATE USER [cactistats] FOR LOGIN [cactistats] WITH DEFAULT_SCHEMA=[dbo]
GO
GRANT SELECT ON [sys].[dm_os_performance_counters] TO [cactistats]
GO
/* END */
Once it's run and you've added the scripts as per the installation documentation, you will be able to graph SQL metrics.
Mike
This answer is in addition to the correctly marked answer.
if you need to monitor a particular sql server instance, then you need to edit this script file
/usr/share/cacti/site/scripts/ss_win_mssql.php
and change the line:
if (! $link = mssql_connect($host.':'.$port, $username, $password) )
to
$host = ($port == '1433' ? $host : $host.':'.$port);
if (! $link = mssql_connect($host, $username, $password) )
return;
and when creating the graphs set the hostname and instance like such: