一、阿里云虚拟私有网络的搭建
由于安全机制,无法上传,功能服务均已实现
二、通过编译、二进制安装MySQL5.7
2.1 编译安装
yum install cmake gcc gcc-c++ ncurses-devel bison zlib libxml openssl automake autoconf make libtool bison-devel libaio-devel openssl openssl-devel -y #安装源代码编译所需的依赖包
[root@Centos7 ~]# groupadd -r mysql #创建系统组mysql
[root@Centos7 ~]# useradd -r -s /sbin/nologin -g mysql mysql # 创建系统用户mysq,并将其加入到系统组mysql之中。
`
[root@Centos7 ~]# mkdir /data/mysql #创建MySQL存放数据库目录
[root@Centos7 ~]# chown -R mysql:mysql /data/mysql #该数据目录所属者、所属主必须是mysql。
[root@Centos7 ~]# tar -zxvf mysql-boost-5.7.30.tar.gz -C /usr/local/ #解压压缩文件至/usr/local下
[root@Centos7 usr]# cd local/
[root@Centos7 local]# ll
total 4
drwxr-xr-x. 2 root root 6 Apr 11 2018 bin
drwxr-xr-x. 2 root root 6 Apr 11 2018 etc
drwxr-xr-x. 2 root root 6 Apr 11 2018 games
drwxr-xr-x. 2 root root 6 Apr 11 2018 include
drwxr-xr-x. 2 root root 6 Apr 11 2018 lib
drwxr-xr-x. 2 root root 6 Apr 11 2018 lib64
drwxr-xr-x. 2 root root 6 Apr 11 2018 libexec
drwxr-xr-x 36 7161 31415 4096 Mar 24 2020 mysql-5.7.30
drwxr-xr-x. 2 root root 6 Apr 11 2018 sbin
drwxr-xr-x. 5 root root 49 Sep 27 19:52 share
drwxr-xr-x. 2 root root 6 Apr 11 2018 src
[root@Centos7 local]# mv mysql-5.7.30 mysql
[root@Centos7 local]# ll
total 4
drwxr-xr-x. 2 root root 6 Apr 11 2018 bin
drwxr-xr-x. 2 root root 6 Apr 11 2018 etc
drwxr-xr-x. 2 root root 6 Apr 11 2018 games
drwxr-xr-x. 2 root root 6 Apr 11 2018 include
drwxr-xr-x. 2 root root 6 Apr 11 2018 lib
drwxr-xr-x. 2 root root 6 Apr 11 2018 lib64
drwxr-xr-x. 2 root root 6 Apr 11 2018 libexec
drwxr-xr-x 36 7161 31415 4096 Mar 24 2020 mysql
drwxr-xr-x. 2 root root 6 Apr 11 2018 sbin
drwxr-xr-x. 5 root root 49 Sep 27 19:52 share
drwxr-xr-x. 2 root root 6 Apr 11 2018 src
#编译过程
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql #MySQL安装目录-DMYSQL_DATADIR=/data/mysql #MySQL数据库目录-DSYSCONFDIR=/etc #MySQL配置文件目录 -DMYSQL_USER=mysql #运行mysql进行的用户,如果没有需要提前添加用户和用户组-DDEFAULT_CHARSET=utf8 #默认字符集-DDEFAULT_COLLATION=utf8_general_ci #默认校对规则-DWITH_SSL=system-DWITH_BOOST=boost #boost库,带boost的源码包只需要这样即可,不带boost源码包需要指明boost目录
[root@Centos7 mysql]# make && make install #编译安装,时间较长,安装时建议加大内存和增加cpu数量。
#配置文件
[root@Centos7 ~]# cat /etc/my.cnf
[mysqld]
datadir=/data/mysql/
basedir=/usr/local/mysql/
port=3306
pid-file=/data/mysql/mysql.pid
socket=/data/mysql/mysql.socket
symbolic-links=0
character_set_server=utf8
user=mysql[mysqld_safe]
log-error=/data/mysql/mysql.log[client]
port=3306
socket=/data/mysql/mysql.socket
default-character-set=utf8
#初始化数据库
[root@Centos7 bin]# pwd
/usr/local/mysql/bin
[root@Centos7 bin]# pwd
/usr/local/mysql/bin
./mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql
#创建服务文件
[root@Centos7 support-files]# pwd
/usr/local/mysql/support-file
cp mysql.server /etc/init.d/mysqld
#软件登录/etc/init.d/mysqld stop/etc/init.d/mysqld start
#修改环境变量
echo 'PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
. /etc/profile.d/mysql.sh
登录测试MySQL软件
[root@Centos7 bin]# mysql -uroot -p -S /data/mysql/mysql.socket
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
[root@Centos7 bin]# mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
#尝试任何办法都无法登录
[root@Centos7 bin]# vim /etc/my.cnf #修改配置文件,skip-grant-tables,跳过表校验
#重启服务
[root@Centos7 bin]# /etc/init.d/mysqld stop
Shutting down MySQL.. SUCCESS!
[root@Centos7 bin]# /etc/init.d/mysqld start
Starting MySQL. SUCCESS!
#成功登录并修改密码
[root@Centos7 bin]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.30 Source distributionCopyright (c) 2000, 2020, 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 databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)mysql> alter user root@'localhost' identified by '123456'; #设置了跳过MySQL表查询,不允许修改密码
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statementmysql> flush privileges; #刷新权限
Query OK, 0 rows affected (0.01 sec)mysql> alter user root@'localhost' identified by '123456'; #修改密码成功
Query OK, 0 rows affected (0.00 sec)mysql> exit
Bye[root@Centos7 bin]# vim /etc/my.cnf #删去skip-grant-tables,重启服务。
[root@Centos7 bin]# /etc/init.d/mysqld stop
Shutting down MySQL.. SUCCESS!
[root@Centos7 bin]# /etc/init.d/mysqld start
Starting MySQL. SUCCESS!
[root@Centos7 bin]# mysql -uroot -p123456 #MySQL数据库,用户名密码登录成功。
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.30 Source distributionCopyright (c) 2000, 2020, 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>
2.2 二进制安装
安装 libaio, libaio 包的作用是为了支持同步I/O,对于数据库之类的系统特别重要。
yum install -y libaio numactl-libs
创建用户及组
groupadd mysql
useradd -r -g mysql -s /bin/false mysql
准备程序及文件
wget http://mirrors.163.com/mysql/Downloads/MySQL-5.7/mysql-5.7.31-linuxglibc2.12-x86_64.tar.gz
tar xf mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz -C /usr/local
cd /usr/local/
ln -s mysql-5.7.31-linux-glibc2.12-x86_64/ mysql
chown -R root.root /usr/local/mysql/
准备环境变量
echo 'PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
. /etc/profile.d/mysql.sh
准备配置文件
[root@Centos7 local]# cat /etc/my.cnf
[mysqld]
datadir=/data/mysql
skip_name_resolve=1
socket=/data/mysql/mysql.sock
log-error=/data/mysql/mysql.log
pid-file=/data/mysql/mysql.pid[client]
socket=/data/mysql/mysql.sock[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
初始化数据库文件
[root@Centos7 local]# mysqld --initialize-insecure --user=mysql --datadir=/data/mysq #生成空密码
准备服务脚本和启动
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
chkconfig --add mysqld
service mysqld start
启动过程报错解决
[root@Centos7 my.cnf.d]# service mysqld start
Starting MySQL.2021-11-06T11:39:46.698686Z mysqld_safe error: log-error set to '/var/log/mariadb/mariadb.log', however file don't exists. Create writable for user 'mysql'. #缺少该文件,需自行创建,mysql具有写权限ERROR! The server quit without updating PID file (/data/mysql/mysql.pid).
[root@Centos7 my.cnf.d]# rpm -ql mariadb
package mariadb is not installed
[root@Centos7 my.cnf.d]# rpm -ql mariadb.service
package mariadb.service is not installed
[root@Centos7 my.cnf.d]# mkdir /var/log/mariadb
[root@Centos7 my.cnf.d]# touch /var/log/mariadb/mariadb.log
[root@Centos7 my.cnf.d]# chown mysql.mysql /var/log/mariadb/mariadb.log
[root@Centos7 my.cnf.d]# service mysqld start
Starting MySQL. SUCCESS!
[root@Centos7 my.cnf.d]# ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 100 [::1]:25 [::]:*
LISTEN 0 80 [::]:3306 [::]:*
登录修改密码
[root@Centos7 local]# mysql -uroot -p
Enter password: #回车
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.31 MySQL Community Server (GPL)Copyright (c) 2000, 2020, 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>
mysql> alter user root@'localhost'identified by '123456'-> ;
Query OK, 0 rows affected (0.00 sec)
[root@Centos7 local]# mysql -uroot -p123456 #密码验证登录
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.31 MySQL Community Server (GPL)Copyright (c) 2000, 2020, 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>
三、二进制安装mariadb10.4
Mariadb下载地址
将下载好的安装包传至服务器中
解压至 /usr/local/(官方默认安装路径)
[root@Centos7 local]# tar zxf mariadb-10.4.21-linux-glibc_214-x86_64.tar.gz -C /usr/local
为了方便管理创建软链接
[root@Centos7 local]# ln -s mariadb-10.4.21-linux-glibc_214-x86_64 mysql
创建数据库软件运行所需的账户及组
[root@Centos7 local]# groupadd -r mysql # -r指定为系统组
[root@Centos7 local]# useradd -r -s /sbin/nologin -g mysql mysql
创建数据库数据文件
[root@Centos7 local]# mkdir /data/mysql;chown -R mysql:mysql #数据库文件所属者,所属主必须是mysql,否则将导致数据库无法启动登录。
准备数据库配置文件
[root@Centos7 local]# cat /etc/my.cnf
[mysqld]
datadir=/data/mysql/
basedir=/usr/local/src/mysql/
port=3306
pid-file=/data/mysql/mysql.pid
socket=/tmp/mysql.socket
symbolic-links=0[mysqld_safe]
log-error=/data/mysql/mysql.log[client]
port=3306
socket=/tmp/mysql.socket
default-character-set=utf8!includedir /etc/my.cnf.d
准备服务脚本
[root@Centos7 local]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld #查看是否具有执行权限
PATH路径
echo 'PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
. /etc/profile.d/mysql.sh
创建初始化数据库文件
[root@Centos7 local]# mysql/scripts/mysql_install_db --defaults-file=/etc/my.cnf --datadir=/data/mysql --basedir=/usr/local/mysql # 分别对应的写上配置文件路径,数据库数据存放目录及数据库软件目录。
在生成初始化数据库的时候必须检查所属者,所属组是否为mysql
数据库启动
[root@Centos7 local]# /etc/init.d/mysqld start
Starting mysqld (via systemctl): [ OK ]
第一次启动启动失败,需按照安全模式启动并修改密码
[root@Centos7 local]# mysqld_safe --defaults-file=/etc/my.cnf & #后台执行
[1] 13304
[root@Centos7 local]# 211106 23:47:51 mysqld_safe Logging to '/data/mysql/mysql.log'.
211106 23:47:51 mysqld_safe Starting mysqld daemon with databases from /data/mysql/[root@Centos7 local]# ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 100 [::1]:25 [::]:*
LISTEN 0 80 [::]:3306 [::]:*
[root@Centos7 local]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 10.4.21-MariaDB MariaDB ServerCopyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.MariaDB [(none)]> alter user root@'localhost' identified by '123456';
Query OK, 0 rows affected (0.001 sec)
进入安全配置模式
[root@Centos7 local]# mysql_secure_installation --baserdir=/usr/local/mysql/
第一项:输入root密码 回车即可。
第二项:是否设置root密码。
第三项:是否删除空账号用户。
第四项:是否禁止root用户远程登入。
第五项:是否删除test测试数据。
第六项:是否现在重新加载权限表吗。