Hướng dẫn cài mysql trên centos 7

MySQL là hệ quản trị cơ sở dữ liệu mã nguồn mở và miễn phí, phổ biến nhất thế giới và được các developer ưa chuộng nhất trong quá trình phát triển ứng dụng. Mình đi deploy nhiều quá, nên tiện viết lại bài viết này để gửi cho bên triển khai luôn cho đỡ mất công viết lại docs hướng dẫn lần nữa. Trong bài viết này mình sẽ hướng dẫn step by step bằng các command để các bạn cài đặt MySQL trên CentOS 7,

Trong bài viết này mình sẽ hướng dẫn càu đặt hai phiên bản phổ biến nhất và hay được sử dụng nhất là phiên bản 8.0 và phiên bản 5.7. Chú ý tuỳ từng nhu cầu để chọn phiên bản phù hợp nhất nhé.

1. Cài đặt MySQL

Với MySQL 8.0 trên CentOS 7.

Thêm repository

sudo yum localinstall //dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpm

Cài đặt sau khi thêm repository

sudo yum install mysql-community-server

Với MySQL 5.7 trên CentOS 7.

Thêm repository

sudo yum localinstall //dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm

Cài đặt sau khi thêm repository

sudo yum install mysql-community-server

2. Khởi động MySQL

Kích hoạt MySQL khởi động cùng system

sudo systemctl enable mysqld

Khởi động MySQL

sudo systemctl start mysqld

Kiểm tra MySQL service đã hoạt động chưa?

sudo systemctl status mysqld

2. Đặt lại MySQL Root Password

Với MySQL 5.7

Khi MySQL khởi chạy lần đầu, một mật khẩu tạm sẽ được sinh ra ngẫu nhiên cho root user của MySQL. Bạn có thể tìm mật khẩu đó bằng câu lệnh bên dưới:

Tìm mật khẩu tạm được gen ngẫu nhiên lần đầu:

sudo grep 'temporary password' /var/log/mysqld.log

Ouput lệnh trên sẽ hiện ra password

2021-09-22T17:33:40.984983Z 1 [Note] A temporary password is generated for root@localhost: odG*hI-hn9Rx

Giờ chúng ta cần đi thay đổi mật khẩu này bằng một mật khẩu dễ nhớ hơn, bằng cách chạy lệnh mysql_secure_installation để cải thiện tính bảo mật của MySQL

sudo mysql_secure_installation

Nhập mật khẩu tạm thời của bạn. Sau đó làm theo hướng dẫn:

Enter password for user root: odG*hI-hn9Rx New password: Nhập mật khẩu Mysql mới Re-enter new password: Nhập lại mật khẩu Mysql mới Change the password for root? y Remove anonymous users? y Disallow root login remotely? y Remove test database and access to it? y Reload privilege tables now? y

Với MySQL 8.0

Dừng service MySQL trước tiên

sudo systemctl stop mysqld

Restart lại MySQL trong safe mode, để bỏ qua mật khẩu.

sudo mysqld_safe --skip-grant-tables

Sau khi và safe mode sẽ đơ một chút và chờ để login với root user.

mysql -uroot

Để đổi mật khẩu root thành "newpassword", nhập lệnh sau

mysql> USE MYSQL; mysql> UPDATE USER SET PASSWORD=PASSWORD["newpassword"] WHERE USER='root'; mysql> FLUSH PRIVILEGES; mysql> EXIT

Khởi động lại MySQL

sudo systemctl start mysqld

2. Tạo một user cho phép remote login.

Mặc định user: root chỉ cho phép đăng nhập qua localhost để đảm bảo tính bảo mật, và không cho phép đăng nhập từ xa, vì vậy để dễ dàng có thể login từ một tools ở client hay để test các database riêng biệt, chúng ta sẽ tạo một user riêng biệt.

Cộng đồng MySQL đã phát hành phiên bản MySQL 5.7. Nó có sẵn trên trang web chính thức của MySQL. Trong bài viết này, chúng ta sử dụng hệ thống CentOS 7.9, 64 bit. Đối với các phiên bản hệ điều hành khác [ví dụ: Windows], bạn có thể tải tệp từ đây. Đồng thời thay đổi tên rpm trong tất cả các lệnh được cung cấp trong hướng dẫn này. Bài viết sẽ giúp bạn install MySQL 5.7 CentOS 7.

Bước 1: Kích hoạt kho lưu trữ MySQL

Trước tiên, bạn cần phải bật kho lưu trữ yum của phiên bản cộng đồng MySQL 5.7 trên hệ thống. Các gói rpm cho cấu hình kho lưu trữ yum có sẵn trên trang web chính thức của MySQL.

Đầu tiên, hãy nhập key GPG mới nhất của MySQL vào hệ thống.

$ sudo rpm --import //repo.mysql.com/RPM-GPG-KEY-mysql-2022

Bây giờ, sử dụng một trong các lệnh dưới đây để cấu hình kho lưu trữ Yum tương ứng với phiên bản hệ điều hành của bạn.

$ sudo yum localinstall //dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm

Bước 2: Cài đặt máy chủ MySQL 5.7

Sau khi bạn đã thành công trong việc bật kho lưu trữ yum MySQL trên hệ thống. Bây giờ, cài đặt MySQL 5.7 community server bằng các lệnh dưới đây tương ứng với phiên bản hệ điều hành của bạn.

$ sudo yum install mysql-community-server

Lệnh trên sẽ cài đặt MySQL community server và các phụ thuộc khác trên hệ thống của bạn. Trong quá trình cài đặt gói, mật khẩu tạm thời được tạo ra và ghi vào tệp nhật ký của MySQL.

Sau khi cài đặt RPM, sử dụng lệnh sau để khởi động dịch vụ MySQL.

$ sudo systemctl start mysqld

Trong lần khởi động đầu tiên, MySQL lưu trữ mật khẩu tài khoản root trong tệp nhật ký, có thể tìm thấy bằng lệnh sau.

$ grep 'A temporary password' /var/log/mysqld.log |tail -1

Đầu ra mẫu:

2017-03-30T02:57:10.981502Z 1 [Note] A temporary password is generated for root@localhost: Nm[!pKkkjo68e

Bước 4: Cấu hình ban đầu cho MySQL

Chạy kịch bản mysql_secure_installation và tuân theo hướng dẫn. Nó sẽ yêu cầu mật khẩu root. Sử dụng mật khẩu root tạm thời đã nhận được trong bước trước đó.

$ /usr/bin/mysql_secure_installation

Hướng dẫn này sẽ yêu cầu bạn nhập thông tin. Hãy nhập một mật khẩu root mới mạnh cho tài khoản MySQL root. Đối với các tùy chọn còn lại, đọc tùy chọn và cung cấp thông tin như yêu cầu. Bạn nên nhấn 'y' cho tất cả để tài khoản được bảo mật tốt hơn.

Securing the MySQL server deployment. Enter password for user root: The 'validate_password' plugin is installed on the server. The subsequent steps will run with the existing configuration of the plugin. Using existing password for root. Estimated strength of the password: 100 Change the password for root ? [[Press y|Y for Yes, any other key for No] : y New password: Re-enter new password: Estimated strength of the password: 100 Do you wish to continue with the password provided?[Press y|Y for Yes, any other key for No] : y By default, a MySQL installation has an anonymous user, allowing anyone to log into MySQL without having to have a user account created for them. This is intended only for testing, and to make the installation go a bit smoother. You should remove them before moving into a production environment. Remove anonymous users? [Press y|Y for Yes, any other key for No] : y Success. Normally, root should only be allowed to connect from 'localhost'. This ensures that someone cannot guess at the root password from the network. Disallow root login remotely? [Press y|Y for Yes, any other key for No] : y Success. By default, MySQL comes with a database named 'test' that anyone can access. This is also intended only for testing, and should be removed before moving into a production environment. Remove test database and access to it? [Press y|Y for Yes, any other key for No] : y

  • Dropping test database... Success.
  • Removing privileges on test database... Success. Reloading the privilege tables will ensure that all changes made so far will take effect immediately. Reload privilege tables now? [Press y|Y for Yes, any other key for No] : y Success. All done!

Bước 5: Đăng nhập vào MySQL

Bạn đã thành công trong việc install MySQL 5.7 CentOS 7. Hãy đăng nhập vào MySQL bằng quyền truy cập root và thử tạo một cơ sở dữ liệu thử nghiệm. Sử dụng mật khẩu bạn đã gán ở bước 4.

$ mysql -u root -p

Đầu ra:

Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 5 Server version: 5.7.38 MySQL Community Server [GPL] Copyright [c] 2000, 2022, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>

Sau khi đăng nhập vào máy chủ MySQL, hãy tạo một cơ sở dữ liệu và tài khoản người dùng bằng các lệnh sau đây.

$ sudo yum localinstall //dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm

0

Bước 6: Kiểm tra phiên bản MySQL

Xác minh phiên bản MySQL đã được cài đặt trên hệ thống của bạn. Lệnh sau sẽ hiển thị phiên bản MySQL đã cài đặt.

$ sudo yum localinstall //dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm

1

Đầu ra:

$ sudo yum localinstall //dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm

2

Trong hướng dẫn này, bạn đã học cách cài đặt MySQL 5.7 trên các hệ thống CentOS 7.

Mọi người cũng tìm kiếm: install mysql 5.7 centos 7, centos 7 install mysql 5.7, mysql 5.7 centos 7, cài đặt mysql trên centos 7, centos 7 mysql 5.7, install mysql 5.7 on centos 7

Chủ Đề