文章目录
- Linux——MySQL5.7编译安装、RPM安装、yum安装
- 一、编译安装
- 二、RPM安装
- 三、yum安装
Linux——MySQL5.7编译安装、RPM安装、yum安装
卸载mysql
# 查看是否安装了mysql
[root@csq ~]# rpm -qa |grep mysql
mysql-community-server-5.7.36-1.el7.x86_64
mysql-community-common-5.7.36-1.el7.x86_64
mysql-community-client-5.7.36-1.el7.x86_64
mysql-community-libs-5.7.36-1.el7.x86_64
# 停止mysql服务器并删除mysql
[root@csq ~]# systemctl stop mysqld
# 卸载
[root@csq ~]# rpm -ev mysql-community-server-5.7.36-1.el7.x86_64
[root@csq ~]# rpm -ev mysql-community-common-5.7.36-1.el7.x86_64 --nodeps
[root@csq ~]# rpm -ev mysql-community-client-5.7.36-1.el7.x86_64
[root@csq ~]# rpm -ev mysql-community-libs-5.7.36-1.el7.x86_64 --nodeps
# 删除mysql目录文件和库
[root@csq ~]# find / -name mysql
[root@csq ~]# rm -rf /var/lib/mysql
[root@csq ~]# rm -rf /etc/my.cnf
# 删除mysql用户及组
[root@csq ~]# id mysql
uid=27(mysql) gid=27(mysql) groups=27(mysql)
[root@csq ~]# userdel mysql
[root@csq ~]# groupdel mysql
[root@csq ~]# cat /etc/passwd |grep mysql
[root@csq ~]# cat /etc/group |grep mysql
# 再次查询
[root@csq ~]# whereis mysql
一、编译安装
安装依赖包
[root@csq ~]# yum -y install ncurses ncurses-devel openssl-devel bison gcc gcc-c++ make cmake wget
下载
# 下载
[root@csq ~]# cd /opt/mytools/
[root@csq mytools]# wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-boost-5.7.36.tar.gz
# 解压
[root@csq mysql-5.7.36]# tar -zxvf mysql-boost-5.7.36.tar.gz
# 创建mysql用户
[root@csq ~]# useradd -r mysql -M -s /sbin/nologin
# 执行如下命令
[root@csq mytools]# cd mysql-5.7.36/
# 编译安装
cmake . \
-DWITH_BOOST=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=1make -j 2 && make install
第二步:配置环境变量+初始化
[root@csq mysql-5.7.36]# echo "export PATH=${PATH}:/usr/local/mysql/bin/" >> /etc/profile
[root@csq mysql-5.7.36]# source /etc/profile
[root@csq mysql-5.7.36]# chown -R mysql:mysql /usr/local/mysql/
[root@csq mysql-5.7.36]# mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
# 初始化完成要记住后面MySQL的初始密码,用于后续的登陆修改密码
LpZ&*(*hh89u
第三步:编辑mysql配置文件
[root@csq mysql]# vim /etc/my.cnf
[client]
port = 3306
socket = /tmp/mysql.sock
default-character-set = utf8[mysqld]
port = 3306
user = mysql
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
socket = /tmp/mysql.sock
character_set_server = utf8
log-error=/usr/local/mysql/data/mysql.err
pid-file=/usr/local/mysql/data/mysqld.pid
[root@csq mysql-5.7.36]# chown mysql:mysql /etc/my.cnf
# 参数详解
[client] # 设置客户端连接 MySQL 服务器时的默认参数
port = 3306 # :设置客户端连接 MySQL 服务器时使用的端口号为 3306
socket = /tmp/mysql.sock # 指定客户端连接 MySQL 服务器时使用的套接字文件路径为 /tmp/mysql.sock。
default-character-set = utf8 # 设置客户端连接 MySQL 服务器时使用的默认字符集为 utf8[mysqld] # 这是 MySQL 服务器部分的配置段,用于设置 MySQL 服务器的参数
port = 3306 # 设置 MySQL 服务器监听的端口号为 3306
user = mysql # 指定运行 MySQL 服务器进程的用户为 mysql
basedir = /usr/local/mysql # 设置 MySQL 服务器的基础目录为 /usr/local/mysql
datadir = /usr/local/mysql/data # 设置 MySQL 服务器的数据文件存储目录为 /usr/local/mysql/data
socket = /tmp/mysql.sock # 指定 MySQL 服务器监听的套接字文件路径为 /tmp/mysql.sock
character_set_server = utf8 # 设置 MySQL 服务器使用的字符集为 utf8
log-error=/usr/local/mysql/data/mysql.err # 设置MySQL服务器的错误日志存放位置
pid-file=/usr/local/mysql/data/mysqld.pid # 设置MySQL服务器PID进程存放位置
第四步:启动mysql
[root@csq mysql]# mysqld_safe --user=mysql &
[root@csq mysql]# netstat -tlnp |grep mysql
tcp6 0 0 :::3306 :::* LISTEN 24584/mysqld
第五步:修改密码登陆sql
[root@csq mysql]# mysql -uroot -p'IWFXDuyi(1fg'
mysql> alter user 'root'@'localhost' identified by '000000';
mysql> exit
[root@csq mysql-5.7.36]# mysql -uroot -p
Enter password:
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
第六步:配置mysql服务
[root@csq mysql-5.7.36]# cp -rf /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@csq mysql-5.7.36]# chkconfig --add mysqld
[root@csq mysql-5.7.36]# chkconfig mysqld on
[root@csq mysql-5.7.36]# service mysqld status
MySQL running (19985) [ OK ]
# 或者
[root@csq mysql-5.7.36]# systemctl status mysqld
● mysqld.service - LSB: start and stop MySQLLoaded: loaded (/etc/rc.d/init.d/mysqld; bad; vendor preset: disabled)Active: active (exited) since Wed 2024-05-08 16:32:36 CST; 56s agoDocs: man:systemd-sysv-generator(8)
第七步:防火墙放行3306端口
[root@lnmp-server ~]# firewall-cmd --add-port=3306/tcp --permanent
success
[root@lnmp-server ~]# firewall-cmd --reload
success[root@csq mysql]# netstat -tlnp |grep mysql
tcp6 0 0 :::3306 :::* LISTEN 24584/mysqld
二、RPM安装
下载地址:https://downloads.mysql.com/archives/community/
# 下载
[root@csq ~]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.36-1.el7.x86_64.rpm-bundle.tar
# 解压
按照顺序安装rpm软件包
[root@csq ~]# rpm -ivh mysql-community-common-5.7.36-1.el7.x86_64.rpm
[root@csq ~]# rpm -ivh mysql-community-libs-5.7.36-1.el7.x86_64.rpm
[root@csq ~]# rpm -ivh mysql-community-devel-5.7.36-1.el7.x86_64.rpm
[root@csq ~]# rpm -ivh mysql-community-libs-compat-5.7.36-1.el7.x86_64.rpm
[root@csq ~]# rpm -ivh mysql-community-client-5.7.36-1.el7.x86_64.rpm
[root@csq ~]# yum install -y net-tools
[root@csq ~]# rpm -ivh mysql-community-server-5.7.36-1.el7.x86_64.rpm
# 验证
[root@csq ~]# whereis mysql
mysql: /usr/bin/mysql /usr/lib64/mysql /usr/include/mysql /usr/share/mysql /usr/share/man/man1/mysql.1.gz
启动mysql
# 查看服务状态
[root@csq ~]# systemctl status mysqld
● mysqld.service - MySQL ServerLoaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)Active: inactive (dead) since Tue 2024-05-07 14:50:20 CST; 20s agoDocs: man:mysqld(8)http://dev.mysql.com/doc/refman/en/using-systemd.html
# 初始化数据库
[root@csq ~]# mysqld --initalize --console
# 授权
[root@csq ~]# chown -R mysql:mysql /var/lib/mysql
# 启动
[root@csq ~]# systemctl start mysqld
[root@csq ~]# systemctl status mysqld
● mysqld.service - MySQL ServerLoaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)Active: active (running) since Tue 2024-05-07 14:55:09 CST; 3s agoDocs: man:mysqld(8)http://dev.mysql.com/doc/refman/en/using-systemd.htmlProcess: 24825 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)Process: 24807 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)Main PID: 24830 (mysqld)CGroup: /system.slice/mysqld.service└─24830 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pidMay 07 14:55:09 csq systemd[1]: Starting MySQL Server...
May 07 14:55:09 csq systemd[1]: Started MySQL Server.
查看临时密码,登陆mysql,改密码
[root@csq ~]# cat /var/log/mysqld.log |grep password 2024-05-07T06:29:54.096170Z 1 [Note] A temporary password is generated for root@localhost: <enk8:(8wlWf
[root@csq ~]# mysql -u root -p'<enk8:(8wlWf'
mysql> alter user 'root'@'localhost' identified by '123456';
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
# 设置密码的验证强度等级,设置为low
mysql> set global validate_password_policy=LOW;
Query OK, 0 rows affected (0.00 sec)
# 设置密码长度为6
mysql> set global validate_password_length=6;
Query OK, 0 rows affected (0.00 sec)
# 设置密码,只要满足6位数长度即可
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
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> exit
三、yum安装
安装依赖包
[root@csq ~]# yum install -y perl net-tools
下载地址:http://repo.mysql.com/
[root@csq ~]# wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm
[root@csq ~]# rpm -ivh mysql-community-release-el7-5.noarch.rpm
如果要安装mysql5.7版本,可以设置一下yum仓库
vim /etc/yum.repos.d/mysql-community.repo
# 文件中会显示5.5,5.6,5.7的版本,把5.7版本下的enabled=0改成=1
# 把默认5.6版本下的enabled=1改成0,然后再进行安装就可以了
yum安装mysql-5.7
[root@csq ~]# yum install -y mysql-server
[root@csq ~]# mysql --version
mysql Ver 14.14 Distrib 5.7.44, for Linux (x86_64) using EditLine wrapper
开启mysql服务
[root@csq ~]# systemctl start mysqld
[root@csq ~]# systemctl status mysqld
● mysqld.service - MySQL ServerLoaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)Active: active (running) since Tue 2024-05-07 15:37:13 CST; 4s agoDocs: man:mysqld(8)
登陆mysql,修改密码
[root@csq ~]# cat /var/log/mysqld.log |grep password
[root@csq ~]# mysql -uroot -p'iywr=#:Wo3yZ'
mysql> alter user 'root'@'localhost' identified by '123456';
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
mysql> set global validate_password_policy=LOW;
Query OK, 0 rows affected (0.00 sec)mysql>
mysql> set global validate_password_length=6;
Query OK, 0 rows affected (0.00 sec)mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
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)