前提环境
[root@localhost ~]# yum -y install gcc gcc-c++ m4 openssl-devel
[root@localhost ~]# yum grouplist
[root@localhost ~]# yum groupinstall -y development tools
[root@localhost ~]# yum groupinstall -y base
[root@localhost ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
[root@localhost ~]# yum makecache fast
1.MySQL-5.7.32主要特性
原生支持Systemd;
更好的性能:对于多核CPU、固态硬盘、锁有着更好的优化;
更好的InnoDB存储引擎;
更为健壮的复制功能:复制带来了数据完全不丢失的方案,传统金融客户也可以选择使用;
MySQL数据库
注:mysql-5.6.3已经支持了多线程的主从复制;
新增sys库:以后这会是DBA访问最频繁的库;
2.安装mysql-5.7.32
(1)系统环境
[root@localhost ~]# uname -r
3.10.0-1160.6.1.el7.x86_64
[root@localhost ~]# cat /etc/redhat-release
CentOS Linux release 7.7.1908 (Core)
因为centos7.7默认安装了mariadb-libs,所以先要卸载掉。
[root@localhost ~]# rpm -qa | grep mariadb
mariadb-libs-5.5.64-1.el7.x86_64
[root@localhost ~]# rpm -e mariadb-libs --nodeps
(2)安装依赖包
cmake:由于从MySQL5.5版本开始弃用了常规的configure编译方法,所以需要CMake编译器,用于设置mysql的编译参数。如:安装目录、数据存放目录、字符编码、排序规则等。
Boost:从MySQL 5.7.5开始Boost库是必需的,mysql源码中用到了C++的Boost库,要求必须安装boost1.59.0或以上版本。
GCC:是Linux下的C语言编译工具,mysql源码编译完全由C和C++编写,要求必须安装GCC。
bison:Linux下C/C++语法分析器。
ncurses:字符终端处理库。
提前准备好所需安装包
下载cmake-3.18.1.tar.gz http://www.cmake.org/download/
下载ncurses-6.2.tar.gz http://ftp.gnu.org/pub/gnu/ncurses/
下载bison-3.6.3.tar.gz http://ftp.gnu.org/gnu/bison/
下载mysql-5.7.32.tar.gz
https://dev.mysql.com/downloads/mysql/
下载 Boost_1_59_0.tar.gz
wget http://nchc.dl.sourceforge.net/project/boost/boost/1.59.0/boost_1_59_0.tar.gz
(3)安装CMAKE及必要的软件
[root@localhost ~]# ls
boost_1_59_0.tar.gz mysql-5.7.32.tar.gz
bison-3.6.3.tar.gz cmake-3.18.1.tar.gz ncurses-6.2.tar.gz
安装cmake
[root@localhost ~]# tar zxf cmake-3.18.1.tar.gz
[root@localhost ~]# cd cmake-3.18.1/
[root@localhost cmake-3.18.1]# ./bootstrap
[root@localhost cmake-3.18.1]# gmake && gmake install
[root@localhost ~]# cmake -version
cmake version 3.18.1
CMake suite maintained and supported by Kitware (kitware.com/cmake).
安装ncurses
[root@localhost cmake-3.18.1]# cd ..
[root@localhost ~]# tar zxf ncurses-6.2.tar.gz
[root@localhost ~]# cd ncurses-6.2/
[root@localhost ncurses-6.2]# ./configure && make && make install
安装bison
[root@localhost ncurses-6.2]# cd ..
[root@localhost ~]# tar zxf bison-3.6.3.tar.gz
[root@localhost ~]# cd bison-3.6.3/
[root@localhost bison-3.6.3]# ./configure && make && make install
安装bootst
[root@localhost bison-3.6.3]# cd ..
[root@localhost ~]# tar zxf boost_1_59_0.tar.gz
[root@localhost ~]# mv boost_1_59_0 /usr/local/boost
(4)创建mysql用户和用户组及目录
[root@localhost ~]# groupadd -r mysql && useradd -r -g mysql -s /bin/false -M mysql #新建msyql组和msyql用户禁止登录shell,/bin/false比/sbin/nologin更加严格。
[root@localhost ~]# mkdir /usr/local/mysql #创建目录
[root@localhost ~]# mkdir /usr/local/mysql/data #数据库目录
(5)编译安装mysql-5.7.32
[root@localhost ~]# tar zxf mysql-5.7.32.tar.gz
[root@localhost ~]# cd mysql-5.7.32/
[root@localhost mysql-5.7.32]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/usr/local/mysql/data -DSYSCONFDIR=/etc -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DEXTRA_CHARSETS=all -DMYSQL_UNIX_ADDR=/tmp/mysql.sock -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DWITH_SYSTEMD=1 -DWITH_BOOST=/usr/local/boost
[root@localhost mysql-5.7.32]# cat /proc/cpuinfo | grep processor | wc -l
2
[root@localhost mysql-5.7.32]# make -j 2 && make install
注意:-j参数表示根据CPU核数指定编译时的线程数,可以加快编译速度。默认为1个线程编译。
配置参数解释
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql #[MySQL安装的根目录]
-DMYSQL_DATADIR=/usr/local/mysql/data #[MySQL数据库文件存放目录]
-DSYSCONFDIR=/etc #[MySQL配置文件所在目录]
-DWITH_MYISAM_STORAGE_ENGINE=1 #[添加MYISAM引擎支持]
-DWITH_INNOBASE_STORAGE_ENGINE=1 #[添加InnoDB引擎支持]
-DWITH_ARCHIVE_STORAGE_ENGINE=1 #[添加ARCHIVE引擎支持]
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock #[指定mysql.sock位置]
-DWITH_PARTITION_STORAGE_ENGINE=1 #[安装支持数据库分区]
-DEXTRA_CHARSETS=all #[使MySQL支持所有的扩展字符]
-DDEFAULT_CHARSET=utf8 #[设置MySQL的默认字符集为utf8]
-DDEFAULT_COLLATION=utf8_general_ci #[设置默认字符集校对规则]
-DWITH-SYSTEMD=1 #[可以使用systemd控制mysql服务]
-DWITH_BOOST=/usr/local/boost #[指向boost库所在目录]
更多参数执行下面的命令
[root@localhost mysql-5.7.32]# cmake . –LH
若要重新运行cmake配置,需要删除CMakeCache.txt文件。
[root@localhost mysql-5.7.32]# make clean
[root@localhost mysql-5.7.32]# rm -f CMakeCache.txt
优化mysql的执行路径
[root@localhost mysql-5.7.32]# vim /etc/profile
末尾添加
export PATH=$PATH:/usr/local/mysql/bin
[root@localhost mysql-5.7.32]# source /etc/profile
(6)设置权限
[root@localhost mysql-5.7.32]# chown -R mysql:mysql /usr/local/mysql/
(7)创建配置文件
[root@localhost mysql-5.7.32]# vim /etc/my.cnf
[mysqld]
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
pid-file=/usr/local/mysql/data/mysqld.pid
log-error=/usr/local/mysql/data/mysqld.err
socket=/tmp/mysql.sock
(8)初始化MySQL系统授权表
[root@localhost mysql-5.7.32]# mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
注:以root初始化操作时要加--user=mysql参数,生成一个随机密码(注意保存登录时用);
(9)配置mysql自动启动
[root@localhost mysql-5.7.32]# cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/
[root@localhost mysql-5.7.32]# vim /usr/lib/systemd/system/mysqld.service
修改:
PIDFile=/usr/local/mysql/data/mysqld.pid
ExecStart=/usr/local/mysql/bin/mysqld --daemonize --pid-file=/usr/local/mysql/data/mysqld.pid $MYSQLD_OPTS
[root@localhost mysql-5.7.32]# systemctl daemon-reload
[root@localhost mysql-5.7.32]# systemctl start mysqld
[root@localhost mysql-5.7.32]# systemctl enable mysqld
[root@localhost mysql-5.7.32]# ss -anput |grep mysql
tcp LISTEN 0 80 [::]:3306 [::]:* users:(("mysqld",pid=8768,fd=21))
(10)访问MySQL数据库
查看临时密码
[root@localhost mysql-5.7.32]# grep password /usr/local/mysql/data/mysqld.err
2020-11-19T10:45:03.606312Z 1 [Note] A temporary password is generated for root@localhost: JEg!jgpa3#9j
[root@localhost mysql-5.7.32]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.32
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> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
或者直接修改密码
[root@localhost mysql-5.7.32]# mysql_secure_installation
Enter password: asd123
提前准备好所需包
可以去MySQL官网下载
https://repo.mysql.com//mysql80-community-release-el7-3.noarch.rpm
[root@snow ~]# ls
mysql80-community-release-el7-3.noarch.rpm
[root@snow ~]# rpm -ivh mysql80-community-release-el7-3.noarch.rpm
警告:mysql80-community-release-el7-3.noarch.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY
准备中... ################################# [100%]
正在升级/安装...
1:mysql80-community-release-el7-3 ################################# [100%]
[root@snow ~]# cd /etc/yum.repos.d/
[root@snow yum.repos.d]# ls
CentOS-Base.repo CentOS-fasttrack.repo CentOS-Vault.repo mysql-community-source.repo
CentOS-CR.repo CentOS-Media.repo CentOS-x86_64-kernel.repo
CentOS-Debuginfo.repo CentOS-Sources.repo mysql-community.repo
1.修改yum源,使之默认下载mysql-5.7版本。
[root@snow yum.repos.d]# vim mysql-community.repo
[mysql57-community]
name=MySQL 5.7 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/$basearch/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
[mysql80-community]
name=MySQL 8.0 Community Server
baseurl=http://repo.mysql.com/yum/mysql-8.0-community/el/7/$basearch/
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
2.yum安装mysql-community-server
[root@snow yum.repos.d]# yum -y install mysql-community-server
3.可以看到总共下载的包包括这些,我们也可以先下载这些包,然后离线安装。
mysql-community-libs
mysql-community-libs-compat
mysql-community-server
mysql-community-client
mysql-community-common
4.访问mysql数据库
[root@snow ~]# systemctl start mysqld
[root@snow ~]# systemctl enable mysqld
[root@snow ~]# ss -anput | grep mysql
tcp LISTEN 0 80 [::]:3306 [::]:* users:(("mysqld",pid=44711,fd=21))
[root@snow ~]# grep password /var/log/mysqld.log
2020-11-19T12:44:19.427898Z 1 [Note] A temporary password is generated for root@localhost: %IU,gn%H183p
[root@snow ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.32
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> alter user root@localhost identified by 'asd123';
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
mysql> alter user root@localhost identified by 'Test123!';
Query OK, 0 rows affected (0.00 sec)
#第一次进去需要修改密码且密码需要满足规则要求
mysql> show databases; #查看数据库;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
mysql> create database test; #创建数据库;
Query OK, 1 row affected (0.00 sec)
mysql> use test; #进入数据库;
Database changed
mysql> create table test (id int,name varchar(20)); #创建数据表;
Query OK, 0 rows affected (0.00 sec)
mysql> insert into test values (1,'zhangsan'),(2,'lisi'); #向数据表中添加数据;
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> show tables; #查看表格;
+----------------+
| Tables_in_test |
+----------------+
| test |
+----------------+
1 row in set (0.00 sec)
mysql> select * from test; #查看表格中的数据;
+------+----------+
| id | name |
+------+----------+
| 1 | zhangsan |
| 2 | lisi |
+------+----------+
2 rows in set (0.00 sec)
mysql> delete from test where id=2; #删除表格中的数据;
Query OK, 1 row affected (0.00 sec)
mysql> select * from test;
+------+----------+
| id | name |
+------+----------+
| 1 | zhangsan |
+------+----------+
1 row in set (0.01 sec)
mysql> drop database test; #删除数据库;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
首先在官网下载所需二进制包
https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.32-linux-glibc2.12-x86_64.tar.gz
前提:把需要的二进制包上传到虚拟机/usr/local/src下。
1.编写一键安装脚本
[root@mysql ~]# vim mysql.sh
#!/bin/bash
rpm -e mariadb-libs --nodeps
cd /usr/local/src
tar zxf mysql-5.7.32-linux-glibc2.12-x86_64.tar.gz
echo "二进制包解压成功..."
groupadd -r mysql
useradd -r -g mysql -s /bin/false -M mysql
echo "组和用户创建成功..."
mv mysql-5.7.32-linux-glibc2.12-x86_64 /usr/local/mysql
mkdir /usr/local/mysql/data
echo "目录创建成功..."
echo "export PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
source /etc/profile
echo "路径优化成功..."
chown -R mysql:mysql /usr/local/mysql/
echo "属组和主权限修改完成..."
cat > /etc/my.cnf << EOF
[mysqld]
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
pid-file=/usr/local/mysql/data/mysqld.pid
log-error=/usr/local/mysql/data/mysqld.err
socket=/tmp/mysql.sock
EOF
echo ”配置文添加成功...“
echo "正在初始化MySQL..."
mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
echo "MySQL初始化成功..."
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld
service mysqld start
echo "成功配置开机自启动..."
ss -lnt | grep 3306
a=`grep password /usr/local/mysql/data/mysqld.err | awk 'NR < 2 {print $NF}'`
echo ${a}
mysql -uroot -p"${a}" -e "alter user root@localhost identified by 'asd123'" --connect-expired-password
echo "数据库密码已修改..."
mysql -u root -pasd123
2.执行脚本进行安装
[root@mysql ~]# chmod +x mysql.sh
[root@mysql ~]# . mysql.sh
二进制包解压成功...
组和用户创建成功...
目录创建成功...
路径优化成功...
属组和主权限修改完成...
”配置文添加成功...“
正在初始化MySQL...
MySQL初始化成功...
Starting MySQL. SUCCESS!
成功配置开机自启动...
LISTEN 0 80 [::]:3306 [::]:*
e85aDn8_Ok<X
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 3
Server version: 5.7.32 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>