How Containerized SQL Server Makes Development Easier

How Containerized SQL Server Makes Development Easier

Managing development databases can be tricky, especially if members of the team want to develop on a Mac or Linux device. In this presentation, we learn how running SQL Server in Docker containers can help ensure a consistent development & testing experience for the entire team.

How Containerized SQL Server Makes Development Easier

Managing development databases can be tricky, especially if members of the team want to develop on a Mac or Linux device. In this presentation, we learn how running SQL Server in Docker containers can help ensure a consistent development & testing experience for the entire team.

How to Restore SQL Server Master Database With or Without Backup

<strong>To reduce failed attempts from users, we will talk about restoring the SQL master database in this post.</strong>

To reduce failed attempts from users, we will talk about restoring the SQL master database in this post.

Despite being a popular relational database management system, SQL Server often encounters corruption and other issues. For this reason, SQL users often complain about being in trouble and unable to perform some simple tasks. However, it is not always the fault of SQL Server that the users cannot restore the SQL Server master database seamlessly. Sometimes, users do not have sufficient knowledge about the process either, and that is what stops them from successful database restoration. To reduce failed attempts from users, we will talk about restoring the SQL master database in this post. Continue reading to learn about the processes in detail.

Why Do You Need to Restore SQL Master Database?

Master database file (A.K.A. MDF file) is the main user database file that contains all the major data in it. If the database gets infected by a virus or encounters some other severe issue, it is the database file that gets affected in the beginning. In case of database corruption or some other errors, master database restoration is the only solution users are left with.

How to Restore SQL Server Master Database

Users who face the need to restore MDF file may or may not have a backup file with them. No matter how strange it may seem, many database admins do not regularly back up their SQL database. Hence, they do not have any backup files to restore. We will talk about both situations and how users can restore their master database with and without a backup file.

Restore SQL Server Master Database from Backup File

**User’s Query: **“Hello all. I am in urgent need to restore SQL master database. But sadly, when I try out the manual technique, I fail to perform the restoration process. I started the process by putting the Server in Single user mode, but after performing one to two more steps, I encountered some error. Not sure where I made the mistake that the process got aborted. Can you suggest how to restore master database from backup manually? I am using SQL Server Enterprise 2005 version and my operating system is Windows Server 2008 R2.”

If you have a backup of the master database, then you can easily restore the MDF file using the manual method. Make sure the backup does not contain any corruption issues. The process is short and does not need the help ofany extra tool. Here is the guide to manually restorethe master database in SQL Server.

  • Put your SQL Server instance into the single user Mode.
  • Run this TSQL command to restore the master database:

“RESTORE DATABASE master FROM<backup_device>WITH REPLACE”

The “replace” command means the restoration process will continue even if there is a database with the same name, and the existing database will be removed.

Note: When the restoration process is complete, SQL Server instance will close down. You have to switch the single-user parameter before starting the Server again.

Restart SQL Server and perform other recovery tasks like database attachment, other database file restoration, etc.

Restore Master Database Without Backup File

User’s Query: “I am a SQL Server 2016 users and my database has become corrupt severely. The database contains a lot of business-critical data, so I cannot do away with the MDF file. Is it possible to restore SQL Server Master database without backup file? Please tell me as I am okay with both the manual and the automated method.”

It is possible that you may not have the MDF file backup with you. Earlier, users had a troublesome time without a backup, but not anymore. Now, they can easily restore the master database from their corrupt or damaged MDF file with the help of SysTools SQL Recovery. The process of restoration is simple and straight forward. Just follow the steps mentioned below and get your task done.

  • Select the Advance Scan option. Also, add the SQL Server version. If you do not know the version of the SQL MDF file, then it has an option to Auto-detect the version. Click on OK.

  • The application will scan the MDF file to fix all the corruption issues and show the scanning report.

  • You can preview any item of the master database by clicking on the tree structure of the left panel and clicking the Export button.

  • Choose Export as an option, whether you want to export as SQL Server Database or as SQL .csv scipt.

  • Enter SQL Server credentials like its server name, username, and password, and select the destination database in which you want to export your recovered database objects.

  • Select the With Schema & Data option and click the Export button, and the restoration of the master database of SQL Server will be done effortlessly.

  • You can visit the SQL Server and locate the restored database there.

Conclusion

Various reasons can lead you to restore the SQL Server master database. While the manual approach is fine, if you have a backup, the software will let you restore the master database without backup. Choose the solution based on your demand and restore without any trouble.

How to install SQL Server on Mac and Windows with sample Database

How to install SQL Server on Mac and Windows with sample Database

Getting Started with Microsoft SQL Server on Mac and Windows in 2020. SQL for beginners 2020 - How to install Microsoft SQL Server on Mac and Windows with sample database

Do you want to learn a skill that can help you earn nearly $100,000 per year? If so, then this course is for you!

In this course, we will teach you how to install SQL Server 2017 Express edition for free on both Mac and Windows.

Then will teach how to connect to the SQL Server that you installed using Azure Data Studio.

And finally, will teach how to download and install a sample database(AdventureWorks) from the official Microsoft website for free which you can use in our next FREE course to learn SQL programming using real scenarios.

This course will be your first and most important step towards learning SQL programming on Microsoft SQL Server.

No prior knowledge is required - just a willingness to learn.

What you'll learn

  • Getting Started Microsoft SQL Server on Mac and Windows in 2020 - Installation Tutorial (FREE)
  • How to install SQL Server on Mac and Windows in 2020
  • Docker download and Installation on Mac
  • Kitematic download and Installation on Mac
  • Docker Configuration for SQL Server on Mac
  • Download and Install SQL Server 2017 Express edition on Mac and Windows
  • Download and Install Azure Data Studio on Mac and Windows
  • Download and Restore a sample database(AdventureWorks) to SQL Server on Mac and Windows

Connect to Microsoft SQL Server database on MacOS using Python

Connect to Microsoft SQL Server database on MacOS using Python

Connect to your MS SQL using python. The first thing you need is to install Homebrew. You need the copy the content in the square brackets which in my case is “ODBC Driver 13 for SQL Server”. Replace “ODBC Driver 13 for SQL Server” with the content you copied in the square brackets.

There are always situations where I want to automate small tasks. I like using Python for these kind of things, you can quickly get something working without much hassle. I needed to perform some database changes in a Microsoft SQL Server database and wanted to connect to it using Python. On Windows this is usually pretty straight forward. But I use macOS as my main operating system and I had some hurdles along the way so here is a quick how to.

Preparing

If Homebrew isn't installed yet let's do that first. It's an excellent package manager for macOS. Paste the following command in the terminal to install it:

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Once finished run the following commands to install the Microsoft ODBC Driver 13.1 for SQL Server. This driver will be used to connect to the MS SQL database.

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
brew tap microsoft/mssql-release https://github.com/Microsoft/homebrew-mssql-release
brew update
brew install [email protected] [email protected]
Python package: pyodbc or pypyodbc

With the driver installed, we'll be using a Python package that uses the driver to connect to the database. There are two frequently used packages to connect in Python: pyodbc and pypyodbcpypyodbc is a pure Python re-implementation of pyodbc. The main thing I took a way was that pypyodbcis easier to set up on different platforms. I could not get pyodbc working, it simply wouldn't connect to the database.

Installing pypyodbc can be done using pip, the python package installer.

pip install pypyodbc
Code

Now that the necessary components have been installed, let's open our favorite code editor and write code. The first thing that needs to be done is importing pypyodbc. The script starts with this:

import pypyodbc

Then we need a connection to the database:

sqlConnection = pypyodbc.connect(
                "Driver={ODBC Driver 13 for SQL Server};"
        "Server=<server IP address>;"
        "Database=<database>;"
        "uid=<username>;pwd=<password>");

Note that you have to replace four values in this code: server IP addressdatabase , username and password. The value for the driver is a hard coded value which indicates what driver to use to connect to the database, this value points to the driver that was installed earlier.

Now all what rests is use the connection and run a query.

cursor = sqlConnection.cursor()
cursor.execute("select * from Values")

The cursor now contains the result of our query, the property cursor.rowcount returns the number of rows the query returned. It's now possible to loop through the rows and access the different columns:

for row in cursor:
    print(cursor)
    # first column
    firstColumn = row[0]
    # ...

When we're done, we need to clean up the cursor and database connection by closing it.

cursor.close()
sqlConnection.close()

And that's it, save the file and use the python <filename>.py or python3 <filename.py> command, this depends on your configuration, to run. Here is the entire script:

import pypyodbc

sqlConnection = pypyodbc.connect(
"Driver={ODBC Driver 13 for SQL Server};"
"Server=<server IP address>;"
"Database=<database>;"
"uid=<username>;pwd=<password>");

cursor = sqlConnection.cursor()
cursor.execute("select * from Values")

for row in cursor:
print(cursor)
# first column
firstColumn = row[0]
# ...

cursor.close()
sqlConnection.close()

The with syntax can also be used to automatically close the cursor and the connection, this is another way of writing the same script:

import pypyodbc

with pypyodbc.connect(
"Driver={ODBC Driver 13 for SQL Server};"
"Server=<server IP address>;"
"Database=<database>;"
"uid=<username>;pwd=<password>") as sqlConnection:

with sqlConnection.cursor() as cursor:

    cursor.execute("select * from Values")

    for row in cursor:
        print(cursor)
        # first column
        firstColumn = row[0]
        # ...

If you're looking for some more reading on the topic:

Thanks for reading. If you liked this post, share it with all of your programming buddies!

Further reading

☞ Python for Time Series Data Analysis

☞ Python Programming For Beginners From Scratch

☞ Python Network Programming | Network Apps & Hacking Tools

☞ Intro To SQLite Databases for Python Programming

☞ Ethical Hacking With Python, JavaScript and Kali Linux

☞ Beginner’s guide on Python: Learn python from scratch! (New)

☞ Python for Beginners: Complete Python Programming