วันจันทร์ที่ 15 เมษายน พ.ศ. 2562

Install MySQL 5.7 on Ubuntu 18.04

Install MySQL Server 5.7

การติดตั้ง MySQL Server 5.7

Reference: Install MySQL | Install phpMyAdmin
# Install MySQL Server
sudo apt update
sudo apt install mysql-server

# Check MySQL Status
sudo systemctl status mysql
# Initial Server
sudo mysql_secure_installation

Securing the MySQL server deployment.

Connecting to MySQL using a blank password.

VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?

Press y|Y for Yes, any other key for No:
Please set the password for root here.

New password:

Re-enter new password:
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) :

 ... skipping.


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) :

 ... skipping.
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) :

 ... skipping.
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) :

 ... skipping.
All done!

ทดสอบเข้าใช้งานโดยเข้าจาก User root
su -
mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 5.7.25-0ubuntu0.18.04.2 (Ubuntu)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

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>

-- Show help
\?

-- Quit
\q


Install phpMyAdmin

การติดตั้ง phpMyAdmin

sudo apt install phpmyadmin php-mbstring php-gettext

เมื่อทำการติดตั้งระบบจะให้เราเลือกว่าจะมีการ Config เข้ากับเว็บ Server ไหน ? ตอนนี้เราเลือกเป็น Apache2
และจากนั้นก็เป็นหน้าจอตั้งค่ารหัสผ่านสำหรับเข้าใช้งานระบบ

หน้าจอตั้งรหัสผ่านสำหรับ phpmyadmin ตั้งสองรอบ

เมื่อทำการติดตั้งเสร็จแล้วให้เข้า Web Browser แล้วเข้าไปที่ http://[ip-address or domain-name]/phpmyadmin/
แล้วใช้ Username: phpmyadmin และรหัสผ่านที่เราตั้งไว้ เข้าใช้งานเพื่อทดสอบ

การตั้งค่าให้ root สามารถเข้าใช้งานได้

เมื่อเราลองใช้ root เข้าใช้งานระบบ phpmyadmin เราจะพบ error ไม่สามารถเข้าใช้งานได้
su -
mysql
SELECT user, plugin, host FROM mysql.user;

-- Output
-- +------------------+-----------------------+-----------+
-- | user             | plugin                | host      |
-- +------------------+-----------------------+-----------+
-- | root             | auth_socket           | localhost |
-- | mysql.session    | mysql_native_password | localhost |
-- | mysql.sys        | mysql_native_password | localhost |
-- | debian-sys-maint | mysql_native_password | localhost |
-- | phpmyadmin       | mysql_native_password | localhost |
-- +------------------+-----------------------+-----------+
-- 7 rows in set (0.00 sec)

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
FLUSH PRIVILEGES;

SELECT user, plugin, host FROM mysql.user;
-- Output
-- +------------------+-----------------------+-----------+
-- | user             | plugin                | host      |
-- +------------------+-----------------------+-----------+
-- | root             | mysql_native_password | localhost |
-- | mysql.session    | mysql_native_password | localhost |
-- | mysql.sys        | mysql_native_password | localhost |
-- | debian-sys-maint | mysql_native_password | localhost |
-- | phpmyadmin       | mysql_native_password | localhost |
-- +------------------+-----------------------+-----------+

เสร็จแล้วลองเข้าใช้ผ่าน User root อีกครั้ง


Allow remote connection

การอนุญาตให้สามารถเชื่อต่อจากเครื่องอื่นได้

Reference: MySQL Config | Grant Database

แก้ไขส่วนของ bind-address ไฟล์จะอยู่ใน /etc/mysql/ โดยใส่เป็น IP Address ที่เราอนุญาตให้ทำการเชื่อมต่อ
ถ้าใช้ 127.0.0.1 ก็จะไม่อนุญาต ให้เราใส่ IP เครื่อง MySQL Server หรือใช้ 0.0.0.0 และกรองการเข้าถึงด้วย Firewall
ถ้าเจอ skip-networking ให้ทำการ Comment ด้วยเพื่อไม่ให้ Skip Network
[mysqld]
#
# * Basic Settings
#
user            = mysql
pid-file        = /var/run/mysqld/mysqld.pid
socket          = /var/run/mysqld/mysqld.sock
port            = 3306
basedir         = /usr
datadir         = /var/lib/mysql
tmpdir          = /tmp
lc-messages-dir = /usr/share/mysql
skip-external-locking
#
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
# bind-address            = 127.0.0.1
bind-address            = 192.168.1.245
# skip-networking

เสร็จแล้วให้เราทำการ Restart MySQL Server
sudo systemctl restart mysql

จากนั้นให้ทำการเพิ่มสิทธิในตัว Database ด้วย
su -
mysql
CREATE USER 'root'@'%' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'password' WITH GRANT OPTION;
FLUSH PRIVILEGES;

-- Example: create new database and allow remote
CREATE DATABASE foo;
CREATE USER 'bar'@'%' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON foo.* TO 'bar'@'%' WITH GRANT OPTION;
FLUSH PRIVILEGES;

เท่านี้เราก็สามารถเชื่อมต่อจากเครื่องอื่นได้แล้ว



เจอปัญหา ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO) ตอนใช้ คำสั่ง mysql
ให้ลองทำตามนี้ แก้ไขไฟล์ /etc/mysql/mysql.conf.d/mysqld.conf เพิ่ม skip-grant-tables เข้าไป
[mysqld]
skip-grant-tables
#
# * Basic Settings
#
user            = mysql
pid-file        = /var/run/mysqld/mysqld.pid
socket          = /var/run/mysqld/mysqld.sock
port            = 3306
basedir         = /usr
datadir         = /var/lib/mysql
tmpdir          = /tmp
lc-messages-dir = /usr/share/mysql
skip-external-locking
#
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
bind-address            = 127.0.0.1

Save ไฟล์ แล้วสั่ง Restart MySQL จากนั้นลองใช้เข้าจากคำสั่ง mysql อีกครั้ง และให้ทำการเปลี่ยนรหัสผ่าน root
systemctl restart mysql
mysql
alter user 'root'@'localhost' identified by 'password';
flush privileges;

แล้วให้ไปลบหรือ Comment skip-grant-tables เสร็จแล้ว Restart MySQL อีกครั้ง และลองเข้าใช้งานผ่าน mysql -u root -p
mysql -u root -p
Enter password:

ไม่มีความคิดเห็น:

แสดงความคิดเห็น