ubuntu20.04部署mysql5.7

安装依赖

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

创建用户

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

下载并解压

https://dev.mysql.com/downloads/mysql/

root@mysql-01:~# mysql_version=5.7.40
root@mysql-01:~# wget  https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-${mysql_version}-linux-glibc2.12-x86_64.tar.gz
root@mysql-01:~# tar xf mysql-${mysql_version}-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
root@mysql-01:~# ln -sv /usr/local/mysql-${mysql_version}-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

root@mysql-01:~# mkdir -pv /data/apps/mysql/{data,logs,tmp}
root@mysql-01:~# chmod 750 /data/apps/mysql/data
root@mysql-01:~# touch /data/apps/mysql/my.cnf
root@mysql-01:~# chmod 644 /data/apps/mysql/my.cnf
root@mysql-01:~# chown -R mysql.mysql /data/apps/mysql/
root@mysql-01:~# /usr/local/mysql/bin/mysqld --defaults-file=/data/apps/mysql/my.cnf   --user=mysql  --datadir=/data/apps/mysql/data  --initialize
root@mysql-01:~# ls -l /data/apps/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 << EOF
[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/apps/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/apps/mysql/my.cnf --daemonize  --pid-file=/data/apps/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

EOF

开机启动

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/apps/mysql/my.cnf --daemonize --pid-file=/data/apps/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/apps/mysql/my.cnf --daemonize --pid-file=/data/apps/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:~# /usr/local/mysql/bin/mysql_secure_installation -S /data/apps/mysql/mysql.sock

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!

登录验证

/usr/local/mysql/bin/mysql -S /data/apps/mysql/mysql.sock -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>

常用命令

update mysql.user set Super_priv='Y' where User='root';
flush privileges;

参考文档

https://dev.mysql.com/doc/mysql-secure-deployment-guide/5.7/en/secure-deployment-install.html

posted @ 2021-10-30 21:27  小吉猫  阅读(219)  评论(0编辑  收藏  举报