数据库官网下载地址 https://downloads.mysql.com/archives/community/
1.源码安装数据库(自定义目录安装)
1)上传或下载代码包
[root@db01 ~]# rz mysql-5.6.46.tar.gz
2)安装依赖
[root@db01 ~]# yum install -y ncurses-devel libaio-devel gcc gcc-c++ glibc cmake autoconf openssl openssl-devel
3)解压
[root@db01 ~]# tar xf mysql-5.6.46.tar.gz
4)创建目录
[root@db01 ~]# mkdir /service
5)生成cmake
[root@db01 ~]# cd mysql-5.6.46/
[root@db01 ~]# cmake . -DCMAKE_INSTALL_PREFIX=/service/mysql-5.6.46
-DMYSQL_DATADIR=/service/mysql-5.6.46/data
-DMYSQL_UNIX_ADDR=/service/mysql-5.6.46/tmp/mysql.sock
-DDEFAULT_CHARSET=utf8
-DDEFAULT_COLLATION=utf8_general_ci
-DWITH_EXTRA_CHARSETS=all
-DWITH_INNOBASE_STORAGE_ENGINE=1
-DWITH_FEDERATED_STORAGE_ENGINE=1
-DWITH_BLACKHOLE_STORAGE_ENGINE=1
-DWITHOUT_EXAMPLE_STORAGE_ENGINE=1
-DWITH_ZLIB=bundled
-DWITH_SSL=system
-DENABLED_LOCAL_INFILE=1
-DWITH_EMBEDDED_SERVER=1
-DENABLE_DOWNLOADS=1
-DWITH_DEBUG=0
#程序存放位置
cmake . -DCMAKE_INSTALL_PREFIX=/service/mysql-5.6.46
#数据存放位置
-DMYSQL_DATADIR=/service/mysql-5.6.46/data
#socket文件存放位置
-DMYSQL_UNIX_ADDR=/service/mysql-5.6.46/tmp/mysql.sock
#使用utf8字符集
-DDEFAULT_CHARSET=utf8
#校验规则
-DDEFAULT_COLLATION=utf8_general_ci
#使用其他额外的字符集
-DWITH_EXTRA_CHARSETS=all
#支持的存储引擎
-DWITH_INNOBASE_STORAGE_ENGINE=1
-DWITH_FEDERATED_STORAGE_ENGINE=1
-DWITH_BLACKHOLE_STORAGE_ENGINE=1
#禁用的存储引擎
-DWITHOUT_EXAMPLE_STORAGE_ENGINE=1
#启用zlib库支持(zib、gzib相关)
-DWITH_ZLIB=bundled
#启用SSL库支持(安全套接层)
-DWITH_SSL=bundled
#启用本地数据导入支持
-DENABLED_LOCAL_INFILE=1
#编译嵌入式服务器支持
-DWITH_EMBEDDED_SERVER=1
# mysql5.6支持了google的c++mock框架了,允许下载,否则会安装报错。
-DENABLE_DOWNLOADS=1
#禁用debug(默认为禁用)
-DWITH_DEBUG=0
#编译
5)编译 make
[root@db01 mysql-5.6.46]# make
6)安装 make install
[root@db01 mysql-5.6.46]# make install
7)做软连接
[root@db01 ~]# ln -s /service/mysql-5.6.46 /service/mysql
8)创建数据库用户
[root@db01 ~]# useradd mysql -s /sbin/nologin -M
9)拷贝配置文件和启动文件
#1.进入配置文件和脚本的目录
[root@db01 ~]# cd /service/mysql/support-files/
#2.拷贝配置文件
[root@db01 support-files]# cp my-default.cnf /etc/my.cnf
cp: overwrite ‘/etc/my.cnf’? 输入y
10)配置system管理MySQL服务
#1.配置system管理MySQL
[root@db01 ~]# vim /usr/lib/systemd/system/mysql.service
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=https://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
ExecStart=/service/mysql/bin/mysqld --defaults-file=/etc/my.cnf
LimitNOFILE = 5000
#2.重新加载启动文件列表
[root@db01 ~]# systemctl daemon-reload
11)初始化数据库
#1.进入初始化目录
[root@db01 support-files]# cd /service/mysql/scripts/
#2.执行初始化命令
[root@db01 scripts]# ./mysql_install_db --user=mysql --basedir=/service/mysql --datadir=/service/mysql/data
12)创建socket文件目录(二进制安装没有)
#先创建tmp目录
[root@db01 ~]# mkdir /service/mysql/tmp
13)授权数据库目录
[root@db01 ~]# chown -R mysql.mysql /service/mysql
[root@db01 ~]# chown -R mysql.mysql /service/mysql-5.6.46
14)system管理启动MySQL
[root@db01 ~]# systemctl start mysql
[root@db01 ~]# ps -ef | grep [m]ysql
mysql 23509 1 3 22:36 ? 00:00:01 /usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf
[root@db01 ~]# netstat -lntp | grep 3306
tcp6 0 0 :::3306 :::* LISTEN 23509/mysqld
15)配置环境变量直接使用mysql
[root@db01 ~]# vim /etc/profile.d/mysql.sh
export PATH=/service/mysql/bin:$PATH
[root@db01 ~]# source /etc/profile
16)修改root用户密码
[root@db01 ~]# mysql -u root -p #默认root用户密码为空,有多种方式重置root密码
第一种:
MySQL > SET PASSWORD=PASSWORD('123456');
Query OK, 0 rows affected (0.00 sec)
第二种:
MySQL > grant all privileges on *.* to root@'%' identified by 'oldboy';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
注意:两种重置密码方式有区别。
2.二进制安装(自定义安装目录)
1)上传二进制包
[root@db01 ~]# rz mysql-5.6.46-linux-glibc2.12-x86_64.tar.gz
2)安装依赖
[root@db01 ~]# yum install -y ncurses-devel libaio-devel gcc gcc-c++ glibc cmake autoconf
3)解压安装包
[root@db01 ~]# tar xf mysql-5.6.46-linux-glibc2.12-x86_64.tar.gz
4)创建自定义目录
[root@db01 ~]# mkdir /service
5)移动目录并改名
[root@db01 ~]# mv mysql-5.6.46-linux-glibc2.12-x86_64 /service/mysql-5.6.46
6)做软连接
[root@db01 ~]# ln -s /service/mysql-5.6.46 /service/mysql
7)创建数据库用户
[root@db01 ~]# useradd mysql -s /sbin/nologin -M
8)拷贝配置文件和启动文件
#1.进入配置文件和脚本的目录
[root@db01 ~]# cd /service/mysql/support-files/
#2.拷贝配置文件
[root@db01 support-files]# cp my-default.cnf /etc/my.cnf
cp: overwrite ‘/etc/my.cnf’? y
#3.拷贝启动脚本
[root@db01 support-files]# cp mysql.server /etc/init.d/mysqld
9)初始化数据库
#1.进入初始化目录
[root@db01 support-files]# cd /service/mysql/scripts/
#2.执行初始化命令
[root@db01 scripts]# ./mysql_install_db --user=mysql --basedir=/service/mysql --datadir=/service/mysql/data
10)启动数据库
[root@db01 scripts]# /etc/init.d/mysqld start
/etc/init.d/mysqld: line 244: my_print_defaults: command not found
/etc/init.d/mysqld: line 264: cd: /usr/local/mysql: No such file or directory
Starting MySQL ERROR! Couldn't find MySQL server (/usr/local/mysql/bin/mysqld_safe)
11)如果是二进制安装到自定义目录下需要修改脚本目录
[root@db01 scripts]# sed -i 's#/usr/local#/service#g' /etc/init.d/mysqld /service/mysql/bin/mysqld_safe
12)授权数据库目录
[root@db01 ~]# chown -R mysql.mysql /service/mysql
[root@db01 ~]# chown -R mysql.mysql /service/mysql-5.6.46
13)再次启动
[root@db01 scripts]# /etc/init.d/mysqld start
Starting MySQL.Logging to '/service/mysql/data/db04.err'.
SUCCESS!
[root@db01 scripts]#
14)检查进程和端口
[root@db01 scripts]# ps -ef | grep [m]ysql
root 23083 1 0 20:36 pts/0 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/usr/local/mysql/data --pid-file=/usr/local/mysql/data/db02.pid
mysql 23191 23083 0 20:36 pts/0 00:00: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=db02.err --pid-file=/usr/local/mysql/data/db02.pid
[root@db01 scripts]# netstat -lntp | grep 3306
tcp6 0 0 :::3306 :::* LISTEN 23191/mysqld
[root@db01 scripts]#
15)配置system管理MySQL服务
#1.配置system管理MySQL
[root@db01 ~]# vim /usr/lib/systemd/system/mysql.service
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=https://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
ExecStart=/service/mysql/bin/mysqld --defaults-file=/etc/my.cnf
LimitNOFILE = 5000
#2.重新加载启动文件列表
[root@db01 ~]# systemctl daemon-reload
16)system管理启动mysql
#1.system管理启动MySQL
[root@db01 ~]# systemctl start mysql
[root@db01 ~]# ps -ef | grep [m]ysql
#一定是启动失败的,因为不只有启动脚本里面指定了/usr/local,很多都有
[root@db01 scripts]# grep -r '/usr/local' /service/mysql/*
#怎么办?一个一个修改吗?
17)查看MySQL配置文件检索顺序
[root@db01 mysql]# /service/mysql/bin/mysql --help|grep 'my.cnf'
order of preference, my.cnf, $MYSQL_TCP_PORT,
/etc/my.cnf /etc/mysql/my.cnf /usr/local/mysql/etc/my.cnf ~/.my.cnf
18)配置my.cnf
#1.配置指定数据库安装目录与数据目录
[root@db01 scripts]# vim /etc/my.cnf
[mysqld]
basedir=/service/mysql
datadir=/service/mysql/data
#2.再次启动
[root@db01 scripts]# systemctl start mysql
#3.一定成功
19)配置环境变量直接使用mysql
[root@db01 ~]# vim /etc/profile.d/mysql.sh
export PATH=/service/mysql/bin:$PATH
[root@db01 ~]# source /etc/profile
3.YUM安装
1)配置MySQL的yum源
[root@db01 ~]# cat /etc/yum.repos.d/mysql-community.repo
# Enable to use MySQL 5.6
[mysql56-community]
name=MySQL 5.6 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.6-community/el/6/$basearch/
enabled=1
gpgcheck=0
2)检查存储库是否正确配置
[root@db01 ~]# yum repolist enabled | grep mysql
3)禁用默认的MySQL模块
[root@db01 ~]# sudo yum module disable mysql
4)安装MySQL
[root@db01 ~]# sudo yum install mysql-community-server -y
5)启动MySQL
[root@db01 ~]# sudo service mysqld start
Starting mysqld (via systemctl): [ OK ]
6)检查MySQL运行状态
[root@db01 ~]# sudo service mysqld status
7)保护运行MySQL
[root@db01 ~]# mysql_secure_installation
yum安装mysql7
[root@db01 ~]# vim /etc/yum.repos.d/mysql.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=0
[root@db01 ~]# yum install -y mysql-* mysql-server*
[root@db01 ~]# systemctl start mysqld
[root@db01 ~]# ls /var/lib/mysql
auto.cnf ib_buffer_pool mysql public_key.pem
ca-key.pem ibdata1 mysql.sock server-cert.pem
ca.pem ib_logfile0 mysql.sock.lock server-key.pem
client-cert.pem ib_logfile1 performance_schema sys
client-key.pem ibtmp1 private_key.pem
[root@db01 ~]# grep "temp" /var/log/mysqld.log
2021-07-11T05:01:22.705453Z 1 [Note] A temporary password is generated for root@localhost: qlxo?:s019Xi #mysql7自动生成新密码
2021-07-11T05:01:25.426087Z 0 [Note] InnoDB: Creating shared tablespace for temporary tables
[root@db01 ~]# mysql -uroot -p'qlxo?:s019Xi' #登录
mysql> set password=password("Caodan@111"); #修改密码,密码不能用弱密码
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql> flush privileges;
查看版本
[root@db02 ~]# mysql -V
mysql Ver 14.14 Distrib 5.6.46, for linux-glibc2.12 (x86_64) using EditLine wrapper