[Reprint] Quick Start: Installing SQL Server on Red Hat and create a database

Quickstart: Installing SQL Server on Red Hat and create a database

Suitable for: is SQL Server (only Linux) noAzure SQL database  noAzure Synapse Analytics (SQL DW)  noParallel Data Warehouse

This quick start describes how to install SQL Server 2019 on 8 in Red Hat Enterprise Linux (RHEL). Then use sqlcmd to connect, first create a database and run the query.

 prompt

This tutorial requires user input and an Internet connection. If unattended or offline are interested in the installation process, see the  Installation Guide for SQL Server on Linux .

prerequisites

You must have 8 or RHEL 7.3,7.4,7.5,7.6 computer (memory of at least at least 2 GB).

To install Red Hat Enterprise Linux on your computer, go to  https://access.redhat.com/products/red-hat-enterprise-linux/evaluationYou can also create RHEL virtual machine in Azure. See  created using Azure CLI and manage Linux VM and for  az vm create use in the call  --image RHEL.

If you previously CTP or RC version of SQL Server installed, you must first delete the old store, and then follow these steps. For more information, see Configuring Linux repository for SQL Server 2017 and 2019 .

For additional system requirements, see  System Requirements for SQL Server on Linux .

Installing SQL Server

 Remark

The following commands for SQL Server 2019 point to RHEL 8 repository. RHEL 8 not pre-install the required SQL Server python2. For more information, see the following blog to learn how to install and configure default python2 interpreter:  https://www.redhat.com/en/blog/installing-microsoft-sql-server-red-hat-enterprise- Beta-8-Linux  .

If you are using RHEL 7, add the following path to change  /rhel/7 instead  /rhel/8.

To configure SQL Server on RHEL, run the following command to install the mssql-server package in the terminal:

  1. Download Microsoft SQL Server 2019 Red Hat repository configuration file:

    bash
    sudo curl -o /etc/yum.repos.d/mssql-server.repo https://packages.microsoft.com/config/rhel/8/mssql-server-2019.repo
    
  2. Run the following command to install the SQL Server:

    bash
    sudo yum install -y mssql-server
    
  3. After the package installation is complete, run the mssql-conf setup, follow the prompts to set the SA password, and select the version.

    bash
    sudo /opt/mssql/bin/mssql-conf setup
    

     Remark

    Be sure to specify a strong password for the SA account (at least eight characters, including uppercase and lowercase letters, decimal numbers and / or non-alphanumeric symbols).

  4. Once configured, verify that the service is running:

    bash
    systemctl status mssql-server
    
  5. To allow remote connections, open SQL Server ports on RHEL firewall. The default SQL Server port TCP 1433. If you are using a firewall FirewallD, you can use the following command:

    bash
    sudo firewall-cmd --zone=public --add-port=1433/tcp --permanent
    sudo firewall-cmd --reload
    

At this point, SQL Server 2019 are running RHEL on your computer, ready to be used!

Installing SQL Server command-line tool

To create the database, you need to use tools to run Transact-SQL statements on the SQL Server to connect. The following steps will install SQL Server command-line tool: the sqlcmd  and  BCP .

  1. Download Microsoft Red Hat repository configuration file.

    bash
    sudo curl -o /etc/yum.repos.d/msprod.repo https://packages.microsoft.com/config/rhel/8/prod.repo
    
  2. If you have an earlier version of mssql-tools to install, remove all old unixODBC package.

    bash
    sudo yum remove unixODBC-utf16 unixODBC-utf16-devel
    
  3. Run the following command to use the unixODBC developer packages installed mssql-tools.

    bash
    sudo yum install -y mssql-tools unixODBC-devel
    
  4. For convenience, add to the PATH environment variable  /opt/mssql-tools/bin/ . This can run these tools without specifying a full path. Run the following command to modify the path login sessions and interactive / non-login session:

    bash
    echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bash_profile
    echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc
    source ~/.bashrc 

local connection

The following steps use sqlcmd local connection to the new SQL Server instance.

  1. Using SQL Server name (-S), user name (-U) and password (-P) argument run sqlcmd. In this tutorial, locally connected users, the server name  localhostUser name  SA, password is the password provided for the SA account during the installation process.

    bash
    sqlcmd -S localhost -U SA -P '<YourPassword>'
    

     prompt

    You may omit the password on the command line, in order to receive a password prompt.

     prompt

    If you later decide to connect remotely, specify the -S parameter of the computer name or IP address, and make sure that port 1433 on the firewall is turned on.

  2. If successful, it should show sqlcmd command prompt: 1>.

  3. If the connection fails, first try to diagnose the problem described in the error message. And then view connection troubleshooting advice .

Create and query data

The following sections describe how to use sqlcmd will gradually create a new database, add data and run a simple query.

New Database

The following steps create a named  TestDB the new database.

  1. At the sqlcmd command prompt, paste the following Transact-SQL command to create a test database:

    SQL
    CREATE DATABASE TestDB
    
  2. On the next line, write a query to return the names of all databases on the server:

    SQL
    SELECT Name from sys.Databases
    
  3. The first two orders are not executed immediately. You must type in a new line  GO to execute the previous command:

    SQL
    GO
    

 prompt

To learn more about how to write Transact-SQL statements and queries, see Tutorial: Writing Transact-SQL statement .

Insert data

Next, create a new table  Inventory, and then insert two new rows.

  1. In sqlcmd command prompt, the context switch to the new  TestDB database:

    SQL
    USE TestDB
    
  2. Create a file named  Inventory new table:

    SQL
    CREATE TABLE Inventory (id INT, name NVARCHAR(50), quantity INT) 
  3. Insert data into the new table:

    SQL
    INSERT INTO Inventory VALUES (1, 'banana', 150); INSERT INTO Inventory VALUES (2, 'orange', 154); 
  4. To execute the above command type  GO:

    SQL
    GO
    

Select Data

Now, in order to run a query from the  Inventory return data table.

  1. Sqlcmd command prompt by entering a query, to return  Inventory the table number of rows is greater than 152:

    SQL
    SELECT * FROM Inventory WHERE quantity > 152;
    
  2. Execute this command:

    SQL
    GO
    

Sqlcmd command prompt exit

To end the sqlcmd session, type  QUIT:

SQL
QUIT

Performance Best Practices

After installing SQL Server on Linux, please view the configuration of Linux and SQL Server best practices to improve production performance. For more information, see the  Performance Best Practices and Configuration Guide SQL Server on Linux .

Cross-platform Data Tools

In addition to "sqlcmd", you can also use the following cross-platform tools to manage SQL Server:

Table 1
   
Azure Data Studio Cross-platform GUI database management utility.
Visual Studio Code A cross-platform GUI code editor that uses mssql extended run Transact-SQL statements.
PowerShell Core Cmdlet-based cross-platform automation and configuration tools.
mssql-cli Cross-platform Transact-SQL commands used to run a command line interface.

Connection from Windows

SQL Server tools on Windows connected to a Linux instance of SQL Server, the operating mode and connect to any remote SQL Server instance of the same.

If you have a Windows computer can connect to a Linux computer, run sqlcmd from the Windows command prompt, try to execute the same steps in this topic. Only validates the use of Linux is the target computer name or IP address instead of localhost, and ensure that TCP port 1433 is open. If there is any connection from a Windows problem, see connection troubleshooting suggestions .

About running on Windows, but other tools to connect to SQL Server on Linux, see:

Other deployment scenarios

For additional installation programs, see the following resources:

Table 2
   
upgrade Learn how to upgrade an existing SQL Server version of Linux installed
Uninstall Uninstall SQL Server on Linux
Unattended Setup Learn how to write the silent installation script
Offline installation Learn how to manually download the offline installer package

Guess you like

Origin www.cnblogs.com/jinanxiaolaohu/p/12635721.html