mysql 的编译安装:
一:确保环境的干净
1.卸载系统原先自带的mysql版本

[root@www ~]# rpm -q mysql
[root@www ~]# rpm -e  
[root@www ~]# rpm -q mysql
package mysql is not installed

小结:卸载的时候如果卸载不掉,系统一般会提示包的依赖关系,并且列出依赖的包的名称,先卸载提示依赖的包就可以了

2.卸载之前的mysql

[root@dbmaster ~]#  ps aux | grep mysql
mysql    105124  0.0  0.0 113308  1628 ?        Ss   3月15   0:00 /bin/sh /usr/bin/mysqld_safe --basedir=/usr
mysql    105310  0.0  0.0 1186852 199088 ?      Sl   3月15  26:43 /usr/libexec/mysqld --basedir=/usr --datadir=/data/mysql --plugin-dir=/usr/lib64/mysql/plugin --log-error=/data/log/mariadb/mariadb.log --pid-file=/var/run/mariadb/mariadb.pid --socket=/var/lib/mysql/mysql.sock
root     158731  0.0  0.0 112728   984 pts/0    S+   10:04   0:00 grep --color=auto mysql
[root@dbmaster ~]# ps aux | grep mysql | awk  '{print $2}'
105124
105310
158761
[root@dbmaster ~]#  ps aux | grep mysql | awk  '{print $2}' | xargs kill -9

3.删除之前mysql安装的文件或目录

[root@dbmaster ~]# find /  -name "mysql*"
find: ‘/run/user/1000/gvfs’: 权限不够
/sys/fs/selinux/booleans/mysql_connect_any
/etc/my.cnf.d/mysql-clients.cnf
............................
[root@dbmaster ~]# find /  -name "mysql*"  -exec  rm -rf {} \;

[root@dbmaster ~]# find /  -name "my.*"  -exec  rm -rf {} \;

二、安装mysql5.7版本
1.确定自己的版本系统
[root@dbmaster ~]# cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)

2.安装依赖包
yum -y install wget cmake gcc gcc-c++ ncurses ncurses-devel libaio-devel openssl openssl-devel
rpm -qa |grep libaio
yum install libaio libaio-devel

3.下载源码包

https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.25.tar.gz
wget http://sourceforge.net/projects/boost/files/boost/1.59.0/boost_1_59_0.tar.gz
注意:
Boost库是一个可移植、提供源代码的C++库,作为标准库的后备,是C++标准化进程的开发引擎之一。

4.创建用户和建立数据目录并赋予用户目录权限

groupadd mysql
useradd -r -g mysql -s /sbin/nologin mysql
mkdir -p /usr/local/mysql
mkdir -p /data/mysql
chown -R mysql.mysql /usr/local/mysql
chown -R mysql.mysql /data/mysql
chmod -R 755 /usr/local/mysql/ 
chmod -R 755 /data/mysql/

5.解压并开始安装

[root@dbslave ~]# tar -xf mysql-5.7.25.tar.gz
[root@dbslave ~]# cd mysql-5.7.25/
[root@dbslave mysql-5.7.25]# pwd
/root/mysql-5.7.25
[root@dbslave mysql-5.7.25]# tar -xf /root/boost_1_59_0.tar.gz  -C /root/mysql-5.7.25
[root@dbslave mysql-5.7.25]# ls
boost_1_59_0  cmake           config.h.cmake   dbug                 extra    libbinlogevents      libmysql     man         mysys_ssl  rapid   scripts     storage        testclients  vio
BUILD         CMakeLists.txt  configure.cmake  Docs                 include  libbinlogstandalone  libmysqld    mysql-test  packaging  README  sql         strings        unittest     win
client        cmd-line-utils  COPYING          Doxyfile-perfschema  INSTALL  libevent             libservices  mysys       plugin     regex   sql-common  support-files  VERSION      zlib
[root@dbslave mysql-5.7.25]#cmake . \
-DWITH_BOOST=boost_1_59_0/ \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DSYSCONFDIR=/etc \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DINSTALL_MANDIR=/usr/share/man \
-DMYSQL_TCP_PORT=3306 \
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock \
-DDEFAULT_CHARSET=utf8 \
-DEXTRA_CHARSETS=all \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_READLINE=1 \
-DWITH_SSL=system \
-DWITH_EMBEDDED_SERVER=1 \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1

#############################
配置参数说明:
CMAKE_INSTALL_PREFIX :指定基础安装目录
MYSQL_DATADIR :指定数据目录
SYSCONFDIR=/etc :指定配置文件扫描目录
MYSQL_TCP_PORT :指定端口号
WITH_BOOST :指定boost所在目录
DEFAULT_CHARSET :指定默认字符集
DEFAULT_COLLATION :指定默认排序规则
ENABLED_LOCAL_INFILE=ON :是否为load data infile启用local
WITH_INNODB_MEMCACHED=ON :是否生成memcached共享库
提示:boost也可以使用如下指令自动下载,把下面参数添加到上面的命令里面
-DDOWNLOAD_BOOST=1

注:
-DWITH_EMBEDDED_SERVER=1   #支持嵌入式mysql服务器
-DWITH_SSL=system                    #使mysql支持证书登陆

############################

7.编译&安装
[root@dbslave mysql-5.7.25]# make && make install

8.初始化数据库

[root@dbmaster mysql-5.7.25]# cd /usr/local/mysql/
[root@dbmaster mysql]# mkdir mysql-files
[root@dbmaster mysql]# chown -R mysql.mysql .
[root@dbmaster mysql]# bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data

建立MySQL配置文件my.cnf

注意:源文件要备份一份,然后清空一份文件,写入以下内容

[root@dbslave mysql-5.7.25]# vim /etc/my.cnf
[mysqld]
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data

9.启动mysql
方法一:使用mysqld_safe

[root@dbmaster mysql]# bin/mysqld_safe --user=mysql &
[root@dbslave mysql]# bin/mysqld_safe --user=mysql &
[1] 157556
[root@dbslave mysql]# Logging to '/usr/local/mysql/data/dbslave.err'.
2019-04-16T11:40:08.740986Z mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/data
^C
[root@dbslave mysql]# ps -aux | grep "mysql"
root     157556  0.0  0.0 113312  1644 pts/0    S    19:40   0:00 /bin/sh bin/mysqld_safe --user=mysql
mysql    157653  1.4  0.0 1156836 174616 pts/0  Sl   19:40   0:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=dbslave.err --pid-file=dbslave.pid
root     157703  0.0  0.0 112724   988 pts/0    S+   19:40   0:00 grep --color=auto mysql

方法二:使用 mysql.server脚本(system V)

[root@dbmaster mysql]# cp support-files/mysql.server /etc/init.d/mysqld
[root@dbmaster mysql]# chkconfig --add mysqld
[root@dbmaster mysql]# chkconfig mysqld on
[root@dbmaster mysql]#  systemctl start  mysqld
[root@dbmaster mysql]# ps aux |grep mysqld
root     274965  0.0  0.0 113312  1644 pts/2    S    19:31   0:00 /bin/sh bin/mysqld_safe --user=mysql
mysql    275062  0.1  0.0 1156836 178368 pts/2  Sl   19:31   0:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=dbmaster.err --pid-file=dbmaster.pid
root     275582  0.0  0.0 112724   988 pts/2    S+   19:34   0:00 grep --color=auto mysqld

忘记MySQL密码:
MySQL 5.7.5 and earlier:

# vim /etc/my.cnf                    
[mysqld]
skip-grant-tables

# service mysqld restart
# mysql
mysql> update mysql.user set password=password("456") where user="root" and host="localhost";
mysql> flush privileges;
mysql> \q

# vim /etc/my.cnf                        
[mysqld]
#skip-grant-table
# service mysqld restart

MySQL 5.7.6 and later:              
[root@slave1 ~]# vim /etc/my.cnf
[mysqld]
skip-grant-tables

#免密登录

skip-grant-tables
[root@slave1 ~]# systemctl restart mysqld

[root@dbslave mysql]# mysql
bash: mysql: 未找到命令...
[root@dbslave mysql]# echo "export PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
[root@dbslave mysql]# source /etc/profile

####
更改密码:

mysql> update mysql.user set authentication_string=password('Cfgdcwhpmq2014') where user='root'
    -> ;
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 1

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

######
创建新用户

 grant all privileges on *.* to vnum@'%' identified by 'vnum@123';

更多细节请参考官方文档:https://dev.mysql.com/doc/refman/8.0/en/updating-yum-repo.html