1. 程式人生 > 其它 >ubuntu20.04部署mysql5.7

ubuntu20.04部署mysql5.7

一 安裝依賴

root@mysql-01:~# apt -y install libncurses-dev libncurses5

二 建立使用者

root@mysql-01:~# groupadd -g 1021 -o -r mysql
root@mysql-01:~# useradd -M -N -g mysql -o -r -d /data/mysql -s /bin/false -c "MySQL Server" -u 1021  mysql
root@mysql-01:~# id mysql
uid=1021(mysql) gid=1021(mysql) groups=1021(mysql)

三 下載並解壓

root@mysql-01:~# wget  https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
root@mysql-01:~# tar xf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
root@mysql-01:~# ln -sv /usr/local/mysql-5.7.22-linux-glibc2.12-x86_64/ /usr/local/mysql
root@mysql-01:~# echo "export PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile.d/mysql.sh
root@mysql-01:~# . /etc/profile

四 初始化mysql

官方安裝文件:https://dev.mysql.com/doc/mysql-secure-deployment-guide/5.7/en/secure-deployment-install.html

root@mysql-01:~# mkdir -pv /data/mysql/{data,logs}
root@mysql-01:~# chmod 750 /data/mysql/data
root@mysql-01:~# touch /data/mysql/my.cnf
root@mysql-01:~# chmod 644 /data/mysql/my.cnf
root@mysql-01:~# chown -R mysql.mysql /data/mysql/
root@mysql-01:~# /usr/local/mysql/bin/mysqld --defaults-file=/data/mysql/my.cnf   --user=mysql  --datadir=/data/mysql/data  --initialize
root@mysql-01:~# ls -l /data/mysql/data/ -l
total 110620
-rw-r----- 1 mysql mysql       56 Oct 30 19:09 auto.cnf
-rw-r----- 1 mysql mysql      417 Oct 30 19:09 ib_buffer_pool
-rw-r----- 1 mysql mysql 12582912 Oct 30 19:09 ibdata1
-rw-r----- 1 mysql mysql 50331648 Oct 30 19:09 ib_logfile0
-rw-r----- 1 mysql mysql 50331648 Oct 30 19:09 ib_logfile1
drwxr-x--- 2 mysql mysql     4096 Oct 30 19:09 mysql
drwxr-x--- 2 mysql mysql     4096 Oct 30 19:09 performance_schema
drwxr-x--- 2 mysql mysql    12288 Oct 30 19:09 sys

五 新增mysqld.service檔案

cat /lib/systemd/system/mysqld.service
[Unit]
Description=MySQL Server
Documentation=man:mysqld(7)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target

[Install]
WantedBy=multi-user.target

[Service]
User=mysql
Group=mysql

Type=forking

PIDFile=/data/mysql/data/mysqld.pid

# Disable service start and stop timeout logic of systemd for mysqld service.
TimeoutSec=0

# Start main service
ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/data/mysql/my.cnf --daemonize --pid-file=/data/mysql/data/mysqld.pid $MYSQLD_OPTS

# Use this to switch malloc implementation
EnvironmentFile=-/etc/sysconfig/mysql

# Sets open_files_limit
LimitNOFILE = 50000

Restart=on-failure

RestartPreventExitStatus=1

PrivateTmp=false

六 開機啟動

root@mysql-01:~# systemctl enable mysqld
Created symlink /etc/systemd/system/multi-user.target.wants/mysqld.service → /lib/systemd/system/mysqld.service.
root@mysql-01:~# systemctl start mysqld
root@mysql-01:~# systemctl status mysqld
● mysqld.service - MySQL Server
     Loaded: loaded (/lib/systemd/system/mysqld.service; enabled; vendor preset: enabled)
     Active: active (running) since Sat 2021-10-30 19:41:12 CST; 2min 12s ago
       Docs: man:mysqld(7)
             http://dev.mysql.com/doc/refman/en/using-systemd.html
    Process: 76068 ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/data/mysql/my.cnf --daemonize --pid-file=/data/mysql/data/mysqld.pid $MYSQ>
   Main PID: 76070 (mysqld)
      Tasks: 27 (limit: 18882)
     Memory: 175.2M
     CGroup: /system.slice/mysqld.service
             └─76070 /usr/local/mysql/bin/mysqld --defaults-file=/data/mysql/my.cnf --daemonize --pid-file=/data/mysql/data/mysqld.pid

Oct 30 19:41:12 mysql-01 systemd[1]: Starting MySQL Server...
Oct 30 19:41:12 mysql-01 systemd[1]: Started MySQL Server.
lines 1-14/14 (END)

七 安全設定

root@mysql-01:~# root@mysql-01:~# /usr/local/mysql/bin/mysql_secure_installation

Securing the MySQL server deployment.

Enter password for user root:

The existing password for the user account root has expired. Please set a new password.

New password:

Re-enter new 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:
Using existing password for root.
Change the password for root ? ((Press y|Y for Yes, any other key for No) :

... skipping.
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!

八 登入驗證

mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.22-log MySQL Community Server (GPL)

Copyright (c) 2000, 2018, 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>