Installing SQL Server
- Download Microsoft SQL Server 2017 Red Hat repository configuration file:
sudo curl -o /etc/yum.repos.d/mssql-server.repo https://packages.microsoft.com/config/rhel/7/mssql-server-2017.repo
prompt
If you want to try SQL Server 2019, you must instead register preview version (2019) repository. For SQL Server 2019 installation, use the following command:
sudo curl -o /etc/yum.repos.d/mssql-server.repo https://packages.microsoft.com/config/rhel/7/mssql-server-preview.repo
- Run the following command to install SQL Server:
sudo yum install -y mssql-server
- When the package installation is complete, run the mssql-conf setup command and follow the prompts to set the SA password, and then select the version.
[Password] ( https://upload-images.jianshu.io/upload_images/8504906-912ea1519f1f4cec.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/606/format/webp
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).
- Once configured, verify that the service is running:
systemctl status mssql-server
- To allow remote connections, open SQL Server ports on the firewall on RHEL. The default SQL Server port TCP 1433. If you use FirewallD firewall, you can use the following command:
sudo firewall-cmd --zone=public --add-port=1433/tcp --permanent
sudo firewall-cmd --reload
RHEL SQL Server is currently running on your computer, you can use!
Installing SQL Server command-line tool
To create the database, you need to use a tool to run Transact-SQL statements to connect on SQL Server. The following steps to install SQL Server command-line tool: the sqlcmd and BCP .
sudo curl -o /etc/yum.repos.d/msprod.repo https://packages.microsoft.com/config/rhel/7/prod.repo
- Download Microsoft Red Hat repository configuration file.
sudo curl -o /etc/yum.repos.d/msprod.repo https://packages.microsoft.com/config/rhel/7/prod.repo
- If you have a previous version mssql tools installed, please remove any older unixODBC package.
sudo yum remove unixODBC-utf16 unixODBC-utf16-devel
- Run the following command to install the mssql-tools and developer unixODBC package.
sudo yum install -y mssql-tools unixODBC-devel
- For convenience, add / opt / mssql-tools / bin / to your PATH environment variable. So that you can not specify a full path when you run the tool. Run the following command to modify PATH login sessions and interactive / non-login session:
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.
- Using SQL Server name (-S), user name (-U) and password (-P) argument run sqlcmd. In this tutorial, locally connected users, the server name
localhost
. User nameSA
, password is the password provided for the SA account during the installation process.
sqlcmd -S localhost -U SA -P '<YourPassword>'
- If successful, it should show sqlcmd command prompt: 1>.
prompt
You may omit the password on the command line, in order to receive a password 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.
If there are mistakes after entering a password
View sqlserver state run by command systemctl status mssql-server, the following error
online inquiries that, SQL Server does not start, because the operating system does not have 3250MB of memory.
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.
- At the sqlcmd command prompt, paste the following Transact-SQL command to create a test database:
CREATE DATABASE TestDB
- On the next line, write a query to return the names of all databases on the server:
SELECT Name from sys.Databases
- The first two orders are not executed immediately. You must type in the new GO to execute the previous command line:
GO
Insert data
Next, create a new table Inventory
, and then insert two new rows.
- In sqlcmd command prompt, the context switch to the new
TestDB
database:
USE TestDB
- Create a new table named Inventory:
CREATE TABLE Inventory (id INT, name NVARCHAR(50), quantity INT)
- Insert data into the new table:
INSERT INTO Inventory VALUES (1, 'banana', 150); INSERT INTO Inventory VALUES (2, 'orange', 154);
- To perform the above-mentioned type GO command:
GO
Select Data
Now, in order to run a query from the Inventory
return data table.
- Sqlcmd command prompt by entering a query, to return
Inventory
the table number of rows is greater than 152:
SELECT * FROM Inventory WHERE quantity > 152;
- Excuting an order:
GO
Sqlcmd command prompt exit
To end the sqlcmd session, type QUIT
:
QUIT