Hướng dẫn cài đặt sql server trên ubuntu 16.04

MySQL là một hệ quản trị database open-souce, thường được cài đặt như một phần của LAMP [Linux, Apache, MySQL, PHP / Python / Perl] phổ biến. Nó sử dụng database quan hệ và SQL [Ngôn ngữ truy vấn có cấu trúc] để quản lý dữ liệu.

Version ngắn gọn cài đặt rất đơn giản: cập nhật index gói của bạn, cài đặt gói undefined

3 và sau đó chạy tập lệnh bảo mật đi kèm. undefined

  • sudo apt-get update
  • sudo apt-get install mysql-server
  • mysql_secure_installation

undefined

Hướng dẫn này sẽ giải thích cách cài đặt MySQL version 5.7 trên server Ubuntu 16.04. Tuy nhiên, nếu bạn đang tìm cách cập nhật cài đặt MySQL hiện có lên version 5.7, bạn có thể đọc hướng dẫn cập nhật MySQL 5.7 này để thay thế.

Yêu cầu

Để làm theo hướng dẫn này, bạn cần:

  • Một server Ubuntu 16.04 được cài đặt theo hướng dẫn cài đặt server ban đầu này , bao gồm user không phải root có quyền sudo và firewall.

Bước 1 - Cài đặt MySQL

Trên Ubuntu 16.04, chỉ có version MySQL mới nhất được bao gồm trong repository APT theo mặc định. Tại thời điểm viết bài, đó là MySQL 5.7

Để cài đặt nó, chỉ cần cập nhật index gói trên server và cài đặt gói mặc định với undefined

4 . undefined

  • sudo apt-get update
  • sudo apt-get install mysql-server

undefined

Bạn cần tạo password root trong khi cài đặt. Tiếp theo, ta sẽ hoàn thành việc cấu hình MySQL.

Bước 2 - Cấu hình MySQL

Đối với các bản cài đặt mới, bạn cần chạy tập lệnh bảo mật đi kèm. Điều này thay đổi một số tùy chọn mặc định kém an toàn hơn cho những thứ như đăng nhập root từ xa và user mẫu. Trên các version MySQL cũ hơn, bạn cũng cần phải khởi tạo folder dữ liệu theo cách thủ công, nhưng điều này được thực hiện tự động ngay bây giờ.

Chạy tập lệnh bảo mật. undefined

  • mysql_secure_installation

undefined

Thao tác này sẽ nhắc bạn nhập password root mà bạn đã tạo ở Bước 1. Bạn có thể nhấn undefined

5 rồi undefined

6 để chấp nhận các giá trị mặc định cho tất cả các câu hỏi tiếp theo, ngoại trừ câu hỏi bạn có muốn thay đổi password root hay không. Bạn chỉ cần đặt nó ở Bước 1, vì vậy bạn không cần phải thay đổi nó. Để có hướng dẫn chi tiết hơn về các tùy chọn này, bạn có thể xem .

Để khởi tạo folder dữ liệu MySQL, bạn sẽ sử dụng undefined

7 cho các version trước 5.7.6 và undefined

8 cho 5.7.6 trở lên. Tuy nhiên, nếu bạn đã cài đặt MySQL từ bản phân phối Debian, giống như trong Bước 1, folder dữ liệu được khởi tạo tự động; bạn không phải làm bất cứ điều gì. Nếu vẫn chạy lệnh, bạn sẽ thấy lỗi sau:

Kết quả

2016-03-07T20:11:15.998193Z 0 [ERROR] --initialize specified but the data directory has files in it. Aborting.

Cuối cùng, hãy kiểm tra cài đặt MySQL.

Bước 3 - Kiểm tra MySQL

MySQL tất nhiên cần chạy tự động. Để kiểm tra điều này, hãy kiểm tra trạng thái của nó. undefined

  • systemctl status mysql.service

undefined

Bạn sẽ thấy kết quả giống như sau:

Kết quả

● mysql.service - MySQL Community Server
   Loaded: loaded [/lib/systemd/system/mysql.service; enabled; vendor preset: en
   Active: active [running] since Wed 2016-11-23 21:21:25 UTC; 30min ago
 Main PID: 3754 [mysqld]
    Tasks: 28
   Memory: 142.3M
      CPU: 1.994s
   CGroup: /system.slice/mysql.service
           └─3754 /usr/sbin/mysqld

Nếu MySQL không chạy, bạn có thể khởi động nó bằng undefined

9 .

Để kiểm tra thêm, bạn có thể thử kết nối với database bằng công cụ undefined

0 , là một ứng dụng client cho phép bạn chạy các lệnh quản trị. Ví dụ: lệnh này cho biết kết nối với MySQL dưới dạng root [ undefined

1 ], nhắc nhập password [ undefined

2 ] và trả về version. undefined

  • mysqladmin -p -u root version

undefined

Bạn sẽ thấy kết quả giống như sau:

Kết quả undefined

2

Nghĩa là MySQL đang hoạt động.

Kết luận

Đến đây ta đã cài đặt MySQL cơ bản trên server. Dưới đây là một số ví dụ về các bước tiếp theo mà ta có thể thực hiện:

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Quickstart: Install SQL Server and create a database on Ubuntu

  • Article
  • 10/24/2023

In this article

Applies to: - Linux

In this quickstart, you install SQL Server 2017 [14.x] on Ubuntu 18.04. Then you can connect with sqlcmd to create your first database and run queries.

For more information on supported platforms, see Release notes for SQL Server 2017 on Linux.

In this quickstart, you install SQL Server 2019 [15.x] on Ubuntu 20.04. Then you can connect with sqlcmd to create your first database and run queries.

For more information on supported platforms, see Release notes for SQL Server 2019 on Linux.

In this quickstart, you install SQL Server 2022 [16.x] on Ubuntu 20.04 or 22.04. Then you can connect with sqlcmd to create your first database and run queries.

For more information on supported platforms, see Release notes for SQL Server 2022 [16.x] on Linux.

If you choose to have a preinstalled SQL Server VM on Ubuntu ready to run your production-based workload, then follow the best practices for creating the SQL Server VM.

Azure Marketplace images

You can create your VM based on one of the following two Azure Marketplace images:

  • Ubuntu 20.04
  • Ubuntu 18.04

When you use these images, you avoid the installation step, and can directly configure the instance by providing the SKU and the

sudo apt-get update
sudo apt-get install -y mssql-server

1 password needed to get started with SQL Server. SQL Server Azure VMs deployed on Ubuntu Pro using the above Marketplace images, are fully supported by both Microsoft and Canonical.

You can configure SQL Server on Linux with mssql-conf, using the following command:

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

Azure Marketplace image

You can create your VM based on the following Azure Marketplace image: Ubuntu 20.04.

When you use this image, you avoid the installation step, and can directly configure the instance by providing the SKU and the

sudo apt-get update
sudo apt-get install -y mssql-server

1 password needed to get started with SQL Server. SQL Server Azure VMs deployed on Ubuntu Pro using the above Marketplace images, are fully supported by both Microsoft and Canonical.

You can configure SQL Server on Linux with mssql-conf, using the following command:

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

Prerequisites

If you've previously installed a Community Technology Preview [CTP] or Release Candidate [RC] of SQL Server, you must first remove the old repository before following these steps. For more information, see Configure repositories for installing and upgrading SQL Server on Linux.

The Windows Subsystem for Linux isn't supported as an installation target for SQL Server.

For other system requirements, see .

Install SQL Server

To configure SQL Server on Ubuntu, run the following commands in a terminal to install the mssql-server package.

  1. Import the public repository GPG keys:

    curl //packages.microsoft.com/keys/microsoft.asc | sudo tee /etc/apt/trusted.gpg.d/microsoft.asc

  2. Register the SQL Server Ubuntu repository:

    sudo add-apt-repository "$[wget -qO- //packages.microsoft.com/config/ubuntu/18.04/mssql-server-2017.list]"

  3. Run the following commands to install SQL Server:

    sudo apt-get update sudo apt-get install -y mssql-server

  4. After the package installation finishes, run

    sudo apt-get update sudo apt-get install -y mssql-server

    3 and follow the prompts to set the SA password and choose your edition. As a reminder, the following SQL Server editions are freely licensed: Evaluation, Developer, and Express.

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

    Remember to specify a strong password for the SA account. You need a minimum length 8 characters, including uppercase and lowercase letters, base-10 digits and/or non-alphanumeric symbols.
  5. Once the configuration is done, verify that the service is running:

    systemctl status mssql-server --no-pager

  6. If you plan to connect remotely, you might also need to open the SQL Server TCP port [default 1433] on your firewall.
  1. Import the public repository GPG keys:

    curl //packages.microsoft.com/keys/microsoft.asc | sudo tee /etc/apt/trusted.gpg.d/microsoft.asc

  2. Register the SQL Server Ubuntu repository:

    sudo add-apt-repository "$[wget -qO- //packages.microsoft.com/config/ubuntu/20.04/mssql-server-2019.list]"

  3. Run the following commands to install SQL Server:

    sudo apt-get update sudo apt-get install -y mssql-server

  4. After the package installation finishes, run

    sudo apt-get update sudo apt-get install -y mssql-server

    3 and follow the prompts to set the SA password and choose your edition. As a reminder, the following SQL Server editions are freely licensed: Evaluation, Developer, and Express.

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

    Remember to specify a strong password for the SA account. You need a minimum length 8 characters, including uppercase and lowercase letters, base-10 digits and/or non-alphanumeric symbols.
  5. Once the configuration is done, verify that the service is running:

    systemctl status mssql-server --no-pager

  6. If you plan to connect remotely, you might also need to open the SQL Server TCP port [default 1433] on your firewall.
  1. Import the public repository GPG keys:

    curl //packages.microsoft.com/keys/microsoft.asc | sudo tee /etc/apt/trusted.gpg.d/microsoft.asc

  2. Register the SQL Server Ubuntu repository:

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

    3
  3. Run the following commands to install SQL Server:

    sudo apt-get update sudo apt-get install -y mssql-server

  4. After the package installation finishes, run

    sudo apt-get update sudo apt-get install -y mssql-server

    3 and follow the prompts to set the SA password and choose your edition. As a reminder, the following SQL Server editions are freely licensed: Evaluation, Developer, and Express.

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

    Remember to specify a strong password for the SA account. You need a minimum length 8 characters, including uppercase and lowercase letters, base-10 digits and/or non-alphanumeric symbols.
  5. Once the configuration is done, verify that the service is running:

    systemctl status mssql-server --no-pager

  6. If you plan to connect remotely, you might also need to open the SQL Server TCP port [default 1433] on your firewall.
  1. Download the public key, convert from ASCII to GPG format, and write it to the required location:

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

    7
  2. Manually download and register the SQL Server Ubuntu repository:

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

    8
  3. Run the following commands to install SQL Server:

    sudo apt-get update sudo apt-get install -y mssql-server

  4. After the package installation finishes, run

    sudo apt-get update sudo apt-get install -y mssql-server

    3 and follow the prompts to set the SA password and choose your edition. As a reminder, the following SQL Server editions are freely licensed: Evaluation, Developer, and Express.

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

    Remember to specify a strong password for the SA account. You need a minimum length 8 characters, including uppercase and lowercase letters, base-10 digits and/or non-alphanumeric symbols.
  5. Once the configuration is done, verify that the service is running:

    systemctl status mssql-server --no-pager

  6. If you plan to connect remotely, you might also need to open the SQL Server TCP port [default 1433] on your firewall.

At this point, SQL Server is running on your Ubuntu machine and is ready to use!

Disable the

sudo apt-get update
sudo apt-get install -y mssql-server

1 account as a best practice

When you connect to your SQL Server instance using the

sudo apt-get update
sudo apt-get install -y mssql-server

1 account for the first time after installation, it's important for you to follow these steps, and then immediately disable the

sudo apt-get update
sudo apt-get install -y mssql-server

1 login as a security best practice.

  1. Create a new login, and make it a member of the sysadmin server role.
    • Depending on whether you have a container or non-container deployment, enable Windows authentication, and create a new Windows-based login and add it to the sysadmin server role.
      • Tutorial: Use adutil to configure Active Directory authentication with SQL Server on Linux
      • Tutorial: Configure Active Directory authentication with SQL Server on Linux containers
    • Otherwise, create a login using SQL Server authentication, and add it to the sysadmin server role.
  2. Connect to the SQL Server instance using the new login you created.
  3. Disable the

    sudo apt-get update sudo apt-get install -y mssql-server

    1 account, as recommended for security best practice.

To create a database, you need to connect with a tool that can run Transact-SQL statements on SQL Server. The following steps install the SQL Server command-line tools: sqlcmd utility and bcp utility.

Use the following steps to install the mssql-tools18 on Ubuntu.

Note

  • Ubuntu 18.04 is supported starting with SQL Server 2019 [15.x] CU 3.
  • Ubuntu 20.04 is supported starting with SQL Server 2019 [15.x] CU 10.
  • Import the public repository GPG keys.

    curl //packages.microsoft.com/keys/microsoft.asc | sudo tee /etc/apt/trusted.gpg.d/microsoft.asc

  • Register the Microsoft Ubuntu repository.
    • For Ubuntu 20.04, use the following command:

      curl //packages.microsoft.com/keys/microsoft.asc | sudo tee /etc/apt/trusted.gpg.d/microsoft.asc

      3
    • For Ubuntu 18.04, use the following command:

      curl //packages.microsoft.com/keys/microsoft.asc | sudo tee /etc/apt/trusted.gpg.d/microsoft.asc

      4
    • For Ubuntu 16.04, use the following command:

      curl //packages.microsoft.com/keys/microsoft.asc | sudo tee /etc/apt/trusted.gpg.d/microsoft.asc

      5
  • Update the sources list and run the installation command with the unixODBC developer package.

    curl //packages.microsoft.com/keys/microsoft.asc | sudo tee /etc/apt/trusted.gpg.d/microsoft.asc

    6 Note To update to the latest version of mssql-tools, run the following commands:

    curl //packages.microsoft.com/keys/microsoft.asc | sudo tee /etc/apt/trusted.gpg.d/microsoft.asc

    7
  • Optional: Add

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

    1 to your

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

    2 environment variable in a bash shell. To make sqlcmd and bcp accessible from the bash shell for login sessions, modify your

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

    2 in the

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

    4 file with the following command:

    curl //packages.microsoft.com/keys/microsoft.asc | sudo tee /etc/apt/trusted.gpg.d/microsoft.asc

    8 To make sqlcmd and bcp accessible from the bash shell for interactive/non-login sessions, modify the

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

    2 in the

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

    6 file with the following command:

    curl //packages.microsoft.com/keys/microsoft.asc | sudo tee /etc/apt/trusted.gpg.d/microsoft.asc

    9

Connect locally

The following steps use sqlcmd to locally connect to your new SQL Server instance.

  1. Run sqlcmd with parameters for your SQL Server name [

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

    7], the user name [

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

    8], and the password [

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

    9]. In this tutorial, you are connecting locally, so the server name is

    systemctl status mssql-server --no-pager

    0. The user name is

    sudo apt-get update sudo apt-get install -y mssql-server

    1 and the password is the one you provided for the SA account during setup.

    sudo add-apt-repository "$[wget -qO- //packages.microsoft.com/config/ubuntu/18.04/mssql-server-2017.list]"

    0 Note Newer versions of sqlcmd are secure by default. For more information about connection encryption, see sqlcmd utility for Windows, and Connecting with sqlcmd for Linux and macOS. If the connection doesn't succeed, you can add the

    systemctl status mssql-server --no-pager

    2 option to sqlcmd to specify that encryption is optional, not mandatory. You can omit the password on the command line to be prompted to enter it. If you later decide to connect remotely, specify the machine name or IP address for the

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

    7 parameter, and make sure port 1433 is open on your firewall.
  2. If successful, you should get to a sqlcmd command prompt:

    systemctl status mssql-server --no-pager

    4.
  3. If you get a connection failure, first attempt to diagnose the problem from the error message. Then review the .

Create and query data

The following sections walk you through using sqlcmd to create a new database, add data, and run a simple query.

For more information about writing Transact-SQL statements and queries, see Tutorial: Writing Transact-SQL Statements.

Create a new database

The following steps create a new database named

systemctl status mssql-server --no-pager

5.

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

    sudo add-apt-repository "$[wget -qO- //packages.microsoft.com/config/ubuntu/18.04/mssql-server-2017.list]"

    1
  2. On the next line, write a query to return the name of all of the databases on your server:

    sudo add-apt-repository "$[wget -qO- //packages.microsoft.com/config/ubuntu/18.04/mssql-server-2017.list]"

    2
  3. The previous two commands were not executed immediately. You must type

    systemctl status mssql-server --no-pager

    6 on a new line to execute the previous commands:

    sudo add-apt-repository "$[wget -qO- //packages.microsoft.com/config/ubuntu/18.04/mssql-server-2017.list]"

    3

Insert data

Next create a new table,

systemctl status mssql-server --no-pager

7, and insert two new rows.

  1. From the sqlcmd command prompt, switch context to the new

    systemctl status mssql-server --no-pager

    5 database:

    sudo add-apt-repository "$[wget -qO- //packages.microsoft.com/config/ubuntu/18.04/mssql-server-2017.list]"

    4
  2. Create new table named

    systemctl status mssql-server --no-pager

    7:

    sudo add-apt-repository "$[wget -qO- //packages.microsoft.com/config/ubuntu/18.04/mssql-server-2017.list]"

    5
  3. Insert data into the new table:

    sudo add-apt-repository "$[wget -qO- //packages.microsoft.com/config/ubuntu/18.04/mssql-server-2017.list]"

    6
  4. Type

    systemctl status mssql-server --no-pager

    6 to execute the previous commands:

    sudo add-apt-repository "$[wget -qO- //packages.microsoft.com/config/ubuntu/18.04/mssql-server-2017.list]"

    3

Select data

Now, run a query to return data from the

systemctl status mssql-server --no-pager

7 table.

  1. From the sqlcmd command prompt, enter a query that returns rows from the

    systemctl status mssql-server --no-pager

    7 table where the quantity is greater than 152:

    sudo add-apt-repository "$[wget -qO- //packages.microsoft.com/config/ubuntu/18.04/mssql-server-2017.list]"

    8
  2. Execute the command:

    sudo add-apt-repository "$[wget -qO- //packages.microsoft.com/config/ubuntu/18.04/mssql-server-2017.list]"

    3

Exit the sqlcmd command prompt

To end your sqlcmd session, type

curl //packages.microsoft.com/keys/microsoft.asc | sudo tee /etc/apt/trusted.gpg.d/microsoft.asc

3:

sudo apt-get update
sudo apt-get install -y mssql-server

0

Performance best practices

After installing SQL Server on Linux, review the best practices for configuring Linux and SQL Server to improve performance for production scenarios. For more information, see Performance best practices and configuration guidelines for SQL Server on Linux.

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

Tool Description Azure Data Studio A cross-platform GUI database management utility. Visual Studio Code A cross-platform GUI code editor that run Transact-SQL statements with the mssql extension. PowerShell Core A cross-platform automation and configuration tool based on cmdlets. mssql-cli A cross-platform command-line interface for running Transact-SQL commands.

Connect from Windows

SQL Server tools on Windows connect to SQL Server instances on Linux in the same way they would connect to any remote SQL Server instance.

If you have a Windows machine that can connect to your Linux machine, try the same steps in this topic from a Windows command-prompt running sqlcmd. You must use the target Linux machine name or IP address rather than

systemctl status mssql-server --no-pager

0, and make sure that TCP port 1433 is open on the SQL Server machine. If you have any problems connecting from Windows, see .

For other tools that run on Windows but connect to SQL Server on Linux, see:

  • SQL Server Management Studio [SSMS]
  • Windows PowerShell
  • SQL Server Data Tools [SSDT]

Other deployment scenarios

For other installation scenarios, see the following resources:

  • : Learn how to upgrade an existing installation of SQL Server on Linux
  • : Uninstall SQL Server on Linux
  • : Learn how to script the installation without prompts
  • : Learn how to manually download the packages for offline installation

For answers to frequently asked questions, see the SQL Server on Linux FAQ.

Related content

  • Explore the tutorials for SQL Server on Linux

Contribute to SQL documentation

Did you know that you can edit SQL content yourself? If you do so, not only do you help improve our documentation, but you also get credited as a contributor to the page.

Chủ Đề