How enable SQL Server Agent in docker container existing

trnvg8h3  于 2023-11-16  发布在  SQL Server
关注(0)|答案(3)|浏览(126)

I have a running SQL Server image on Docker, however I need to enable SQL Server Agent to run some jobs, but I have not succeeded. The main problem is that the container has quite a few databases and settings that need to be maintained. The command "docker run -e" MSSQL_AGENT_ENABLED = true ... "" is not useful to me because it creates a new container and I would lose the current configuration.

I used the following commands that allowed to enable the interface, but when running the job I get an error that SQL Server Agent is not enabled

exec sp_configure 'show advanced options',1
reconfigure
exec sp_configure 'Agent XPs',1
reconfigure

The error generated when executing the job is the following

An exception occurred while executing a Transact-SQL statement or batch. (Microsoft.SqlServer.ConnectionInfo) SQLServerAgent is not currently running so it cannot be notified of this action. (Microsoft SQL Server, Error: 22022)

I tried to start the SQL Server Agent with the command EXEC xp_servicecontrol N'START ', N'SQLServerAGENT' , but it generates another error

StartService() returned error 1053, 'The service did not respond to the start or control request in a timely fashion.'

The question would be, how can I enable the SQL Server Agent in the container that is already running to be able to schedule jobs

lh80um4z

lh80um4z1#

If you've already created a Docker container with something like SQL Server 2019 Developer edition:

$ docker run -e ACCEPT_EULA=Y -e MSSQL_PID=Developer -e MSSQL_SA_PASSWORD=YourStrongPassw0rd -p 1433:1433 -d mcr.microsoft.com/mssql/server:2019-latest
57301203bac57455a118e0dbe6ff392cb19313375c134050e6ecd77414555e7e

With reference to Configure SQL Server on Linux with the mssql-conf tool, get a root shell in the container:

$ docker exec -it --user root 57301203bac5 bash

Then enable SQL Agent in the configuration file and restart the SQL Server service:

root@57301203bac5:/# /opt/mssql/bin/mssql-conf set sqlagent.enabled true
SQL Server needs to be restarted in order to apply this setting. Please run
'systemctl restart mssql-server.service'.

root@57301203bac5:/# systemctl restart mssql-server.service

If you get an error message such as systemctl: command not found then just stop and start the container for the changes to take effect.

nnsrf1az

nnsrf1az2#

From outside the container go into terminal by doing the following:

sudo docker exec -it --user root sql1 "bash"

The password will not be of the container.

Next elevate yourself to superuser:

su -

Next enable sqlagent by changing the configuration file:

/opt/mssql/bin/mssql-conf set sqlagent.enabled true

Now exit the container by typing exit once to logout from root and next to exit from the container

exit
exit

Finally restart the the docker container that has your microsoft sql server instance running

(in this case tagged sql1)

docker restart sql1

N.B. Within SQL Management Studio you need to right click the SQL Server Agent Icon and refresh to verify it is up.

Note you no longer have to do this if you enabled SQL server agent with the flag when initialising the container:

docker run -d -p 1433:1433 --env ACCEPT_EULA=Y --env SA_PASSWORD='MYSUP3RS@!3' --env MSSQL_AGENT_ENABLED=True --name sql1 mcr.microsoft.com/mssql/server:2019-GDR1-ubuntu-16.04
jk9hmnmh

jk9hmnmh3#

For anyone using Docker Compose, it's much easier to just add a flag to the yaml environment:

environment:
  MSSQL_AGENT_ENABLED: true

Then just down and up the container again.

More info at Microsoft.com

Credit goes to Mickeybyte: https://stackoverflow.com/a/76182745

相关问题