mysql5.7.32
源码编译安装
MySQL 5.7主要特性:
原生支持Systemd
更好的性能:对于多核CPU、固态硬盘、锁有着更好的优化
更好的InnoDB存储引擎
更为健壮的复制功能:复制带来了数据完全不丢失的方案,传统金融客户也可以选择使用MySQL数据库。
注:mysql-5.6.3已经支持了多线程的主从复制新增sys库:以后这会是DBA访问最频繁的库
安装前提环境
[root@mysql ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
[root@mysql ~]# yum grouplist
[root@mysql ~]# yum groupinstall Development Tools -y
[root@mysql ~]# yum -y groupinstall base
[root@mysql ~]# yum -y install gcc-c++ gcc m4 openssl-devel
因为centos7.2默认安装了mariadb-libs,所以先要卸载掉
查看是否安装mariadb
[root@mysql ~]# rpm -qa | grep mariadb
mariadb-libs-5.5.60-1.el7_5.x86_64
卸载mariadb
[root@mysql ~]# rpm -e mariadb-libs
error: Failed dependencies:
libmysqlclient.so.18()(64bit) is needed by (installed) postfix-2:2.10.1-7.el7.x86_64
libmysqlclient.so.18(libmysqlclient_18)(64bit) is needed by (installed) postfix-2:2.10.1-7.el7.x86_64
[root@mysql ~]# rpm -e mariadb-libs --nodeps
安装依赖包
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:字符终端处理库
文件准备:
ncurses-6.2.tar.gz ftp://ftp.gnu.org/gnu/ncurses/
bison-3.6.3.tar.gz http://ftp.gnu.org/gnu/bison/
cmake-3.18.1.tar.gz http://www.cmake.org/download/
boost_1_59_0.tar.gz wget http://nchc.dl.sourceforge.net/project/boost/boost/1.59.0/boost_1_59_0.tar.gz
mysql-5.7.32.tar.gz wget http://cdn.mysql.com/Downloads/MySQL-5.7/mysql- 5.7.32.tar.gz
安装CMAKE及必要的软件:
[root@mysql ~]# tar zxf cmake-3.18.1.tar.gz
[root@mysql cmake-3.18.1]# cd cmake-3.18.1/
[root@mysql cmake-3.18.1]# ./bootstrap
[root@mysql cmake-3.18.1]# gmake && gmake install
[root@mysql ~]# tar zxf bison-3.6.3.tar.gz
[root@mysql ~]# cd bison-3.6.3/
[root@mysql bison-3.6.3]# ./configure && make && make install
[root@mysql ~]# tar zxf ncurses-6.2.tar.gz
[root@mysql ~]# cd ncurses-6.2/
[root@mysql ncurses-6.2]# ./configure && make && make insatll
[root@mysql ~]# tar zxf boost_1_59_0.tar.gz
[root@mysql ~]# mv boost_1_59_0 /usr/local/boost
创建mysql用户和用户组及目录
[root@mysql ~]# groupadd -r mysql && useradd -r -g mysql -s /bin/fslse -M mysql
[root@mysql ~]# mkdir /usr/local/mysql
[root@mysql ~]# mkdir /usr/local/mysql/data
编译安装mysq
**解压mysql源码包:**
[root@mysql ~]# tar zxf mysql-5.7.32.tar.gz
[root@mysql mysql-5.7.32]# cd mysql-5.7.32/
**执行cmake命令进行编译前的配置:**
[root@mysql 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
**查看cpu**
[root@mysql mysql-5.7.32]# cat /proc/cpuinfo |grep processor
processor : 0
processor : 1
processor : 2
processor : 3
[root@mysql mysql-5.7.32]# cat /proc/cpuinfo |grep processor | wc -l
4
为了加快编译速度可以按下面的方式编译安装
**开始编译、编译安装:**
[root@mysql mysql-5.7.32]# make -j 4 && make install (等同于make && make install)
-j参数表示根据CPU核数指定编译时的线程数,可以加快编译速度。默认为1个线程编译。注3:若要重新运行cmake配置,需要删除CMakeCache.txt文件
#make clean
#rm -f CMakeCache.txt
配置解释:
-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=/usr/local/mysql /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@mysql mysql-5.7.32]# cmake . -LH
设置权限
[root@mysql ~]# chown -R mysql:mysql /usr/local/mysql/
创建配置文件
[root@mysql ~]# 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/mysql.err
socket=/tmp/mysql.sock
修改配置文件
[root@mysql ~]# vim /etc/profile
export PATH=$PATH:/usr/local/mysql/bin //最后一行添加
[root@mysql ~]# . /etc/profile
初始化
[root@mysql ~]# mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
设置mysql自动启动
[root@mysql ~]# cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/
配置文件修改pid路径
[root@mysql ~]# 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
启动mysql
[root@mysql ~]# systemctl daemon-reload )
[root@mysql ~]# systemctl start mysqld.service
[root@mysql ~]# ss -anput |grep mysql
tcp LISTEN 0 80 :::3306 :::* users:(("mysqld",pid=20077,fd=21))
查看mysql默认密码
[root@mysql ~]# grep password /usr/local/mysql/data/mysql.err //查看临时密码
2020-11-19T19:10:40.341168Z 1 [Note] A temporary password is generated for root@localhost: rg#2g>t<okC2
登录mysql数据库
[root@mysql ~]# mysql -uroot -p
Enter password: rg#2g>t<okC2
mysql> alter user root@localhost identified by '123.com'; //修改密码
mysql> exit
[root@mysql ~]# mysql -uroot -p123.com //登录
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 Source distribution
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)
mysql> create database mytest; //创建数据库
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| mytest |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
mysql> use mytest; //进入数据库
Database changed
mysql> create table test (id int,name varchar(20)); //创建表格
Query OK, 0 rows affected (0.03 sec)
mysql> insert into test values (1,'zhsan'),(2,'lisi'); //向表格里添加数据多个数据
Query OK, 2 rows affected (0.01 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from test; //查看表格里所有的数据
+------+-------+
| id | name |
+------+-------+
| 1 | zhsan |
| 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> drop database mytest; //删除数据库
Query OK, 1 row affected (0.01 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
mysql> exit (quit) //退出数据库
Bye
总结:
SSL(Secure Sockets Layer 安全套接层),及其继任者传输层安全(Transport Layer Security,TLS)是为网络通信提供安全及数据完整性的一种安全协议。复制默认是明文进行传输的,通过SSL加密可以大大提高数据的安全性。
yum安装mysql
下载mysql的yum资源库·
https://repo.mysql.com//mysql80-community-release-el7-3.noarch.rpm
yum源解压
[root@yummysql ~]# ls
mysql80-community-release-el7-3.noarch.rpm
[root@yummysql ~]# rpm -ivh mysql80-community-release-el7-3.noarch.rpm
warning: mysql80-community-release-el7-3.noarch.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing... ################################# [100%]
Updating / installing...
1:mysql80-community-release-el7-3 ################################# [100%]
修改yum源默认下载5.7
[root@yummysql ~]# vim /etc/yum.repos.d/mysql-community.repo
name=MySQL 5.7 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/$basearch/
enabled=1 //0改成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 //1改成0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
安装mysql
[root@yummysql ~]# yum -y install mysql-community-server
启动mysql
[root@yummysql ~]# systemctl start mysqld
[root@yummysql ~]# systemctl enable mysqld
查看mysql临时密码
[root@yummysql ~]# grep password /var/log/mysqld.log
2020-11-19T21:15:56.796283Z 1 [Note] A temporary password is generated for root@localhost: PW-oN_jpp4Kp
登录mysql数据库
[root@yummysql ~]# mysql -uroot -p
Enter password: PW-oN_jpp4Kp
mysql> alter user root@localhost identified by 'Test123.'; //修改密码
[root@mysql ~]# mysql -uroot -p123.com //登录
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 Source distribution
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)
mysql> create database mytest; //创建数据库
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| mytest |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
mysql> use mytest; //进入数据库
Database changed
mysql> create table test (id int,name varchar(20)); //创建表格
Query OK, 0 rows affected (0.03 sec)
mysql> insert into test values (1,'zhsan'),(2,'lisi'); //向表格里添加数据多个数据
Query OK, 2 rows affected (0.01 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from test; //查看表格里所有的数据
+------+-------+
| id | name |
+------+-------+
| 1 | zhsan |
| 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> drop database mytest; //删除数据库
Query OK, 1 row affected (0.01 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
mysql> exit (quit) //退出数据库
Bye
二进制安装mysql
下载二进制包
这里我安装5.7.32版本
把二进制包进去
[root@mysql ~]# ll
-rw-r--r--. 1 root root 661214270 Nov 21 11:04 mysql-5.7.32-linux-glibc2.12-x86_64.tar.gz
解压二进制包
[root@mysql ~]# tar zxf mysql-5.7.32-linux-glibc2.12-x86_64.tar.gz
解好的包传到/usr/local/并改名mysql
[root@mysql ~]# mv mysql-5.7.32-linux-glibc2.12-x86_64 /usr/local/mysql
创建mysql用户和用户组及目录
[root@mysql ~]# groupadd -r mysql && useradd -r -g mysql -s /bin/fslse -M mysql
[root@mysql ~]# mkdir /usr/local/mysql/data
设置权限
[root@mysql ~]# chown -R mysql:mysql /usr/local/mysql/
创建配置文件
[root@mysql ~]# vim /etc/my.cnf
[mysqld]
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
pid-file=/usr/local/mysql/data/mysqld.pidlog-error=/usr/local/mysql/data/mysql.err
socket=/tmp/mysql.sock
优化路径
[root@mysql ~]# vim /etc/profile
...
export PATH=$PATH:/usr/local/mysql/bin //最后一行添加
[root@mysql ~]# . /etc/profile
初始化
[root@mysql ~]# mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
设置mysql自动启动
[root@mysql ~]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@mysql ~]# /etc/init.d/mysqld start
Starting MySQL. SUCCESS!
[root@mysql ~]# ss -anput |grep mysql
tcp LISTEN 0 80 :::3306 :::* users:(("mysqld",pid=16931,fd=20))
设置开机自启
[root@mysql ~]# chkconfig --add mysqld
[root@mysql ~]# chkconfig --level 345 mysqld on
[root@mysql ~]# chkconfig --list
Note: This output shows SysV services only and does not include native
systemd services. SysV configuration data might be overridden by native
systemd configuration.
If you want to list systemd services use 'systemctl list-unit-files'.
To see services enabled on particular target use
'systemctl list-dependencies [target]'.
mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off
netconsole 0:off 1:off 2:off 3:off 4:off 5:off 6:off
network 0:off 1:off 2:on 3:on 4:on 5:on 6:off
查看mysql临时密码
[root@mysql ~]# grep password /usr/local/mysql/data/mysql.err
2020-11-21T21:27:20.134821Z 1 [Note] A temporary password is generated for root@localhost: XfWr_qtw?5Dd
登录数据库修改密码
[root@mysql ~]# grep password /usr/local/mysql/data/mysql.err
2020-11-21T21:27:20.134821Z 1 [Note] A temporary password is generated for root@localhost: XfWr_qtw?5Dd
[root@mysql ~]# mysql -uroot -p
Enter password: XfWr_qtw?5Dd
mysql> alter user root@localhost identified by '123.com';
Query OK, 0 rows affected (0.00 sec)
mysql> exit
Bye
用修改的密码登录
[root@mysql ~]# mysql -uroot -p123.com
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>
shell脚本自动安装mysql(二进制)
传二进制包
mysql-5.7.32-linux-glibc2.12-x86_64.tar.gz
脚本
[root@localhost ~]# vim mysql.sh
#!/bin/bash
rpm -e mariadb-libs --nodeps
tar zxf mysql-5.7.32-linux-glibc2.12-x86_64.tar.gz
mv mysql-5.7.32-linux-glibc2.12-x86_64 /usr/local/mysql
groupadd -r mysql && useradd -r -g mysql -s /bin/false -M mysql
mkdir /usr/local/mysql/data
chown -R mysql:mysql /usr/local/mysql
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/mysql.err
EOF
echo export PATH=$PATH:/usr/local/mysql/bin >> /etc/profile
source /etc/profile
mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
sed -i 's/^basedir=/basedir=\/usr\/local\/mysql/' /etc/init.d/mysqld
sed -i 's/^datadir=/datadir=\/usr\/local\/mysql\/data/' /etc/init.d/mysqld
chmod 755 /etc/init.d/mysqld
/etc/init.d/mysqld start
/etc/init.d/mysqld stop
b=`grep 'temporary password' /usr/local/mysql/data/mysql.err`
a=`echo ${
b##*localhost:}`
echo $a
/etc/init.d/mysqld start
mysql -uroot -p"${a}" -e "ALTER USER 'root'@'localhost' IDENTIFIED BY '123.com'" --connect-expired-password
mysql -uroot -p123.com
这个脚本是自动修改密码 直接进入mysql的
[root@localhost ~]# sh mysql.sh
Starting MySQL... SUCCESS!
Shutting down MySQL.. SUCCESS!
3fGOJK>C8Y;C
Starting MySQL. SUCCESS!
mysql: [Warning] Using a password on the command line interface can be insecure.
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> exit
Bye