暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

MySQL 5.7 二进制安装

原创 learnONE 2022-08-12
642


部署环境

环境配置
操作系统Redhat Linux 7.9
MySQL软件MySQL 5.7.36
软件安装路径/usr/local/mysql
数据存储路径/data/mysql


官方操作手册里的操作步骤

 $> groupadd mysql
 $> useradd -r -g mysql -s /bin/false mysql
 $> cd /usr/local
 $> tar zxvf /path/to/mysql-VERSION-OS.tar.gz
 $> ln -s full-path-to-mysql-VERSION-OS mysql
 $> cd mysql
 $> mkdir mysql-files
 $> chown mysql:mysql mysql-files
 $> chmod 750 mysql-files
 $> bin/mysqld --initialize --user=mysql
 $> bin/mysql_ssl_rsa_setup
 $> bin/mysqld_safe --user=mysql &
 # Next command is optional
 $> cp support-files/mysql.server /etc/init.d/mysql.server
复制


1. 配置yum源

 ##挂载镜像源
 mount /dev/cdrom /mnt
 
 ##配置yum源
 cat <<EOF>>/etc/yum.repos.d/local.repo
 [local]
 name=local
 baseurl=file:///mnt
 gpgcheck=0
 enabled=1
 EOF
 
 ##查看yum源
 yum clean all
 yum makecache
 yum repolist all
 
 ##设置开机启动
 cat <<EOF>>/etc/fstab
 /dev/cdrom   /mnt   iso9660 defaults  0  0
 EOF
 ##测试挂载是否正常
 umount /mnt
 mount -a
 
 ##查看是否挂载
 df -Th
复制


2. 清除旧版MySQL

 rpm -qa | grep mysql
 rpm -qa | grep mariadb
 rpm -qa | grep percona
 rpm -e 卸载
复制


3. 关闭防火墙

 systemctl stop firewalld
 systemctl disable firewalld
 systemctl status firewalld
复制


4. 关闭selinux

 setenforce 0
 sed -i "/^SELINUX=/s#enforcing#disabled#" /etc/selinux/config
 cat /etc/selinux/config
复制


5. 安装依赖包

 yum install -y libaio
复制


6. 上传文件解压并改名mysql

 [root@mysql57 local]# cd /usr/local
 [root@mysql57 local]# ls
 mysql-5.7.36-linux-glibc2.12-x86_64.tar.gz
 [root@mysql57 local]#
 [root@mysql57 local]# pwd
 /usr/local
 [root@mysql57 local]# tar -zxvf mysql-5.7.36-linux-glibc2.12-x86_64.tar.gz
 [root@mysql57 local]# mv mysql-5.7.36-linux-glibc2.12-x86_64 mysql
 [root@mysql57 local]# ls
 mysql mysql-5.7.36-linux-glibc2.12-x86_64.tar.gz
 [root@mysql57 local]#
 [root@mysql57 local]# ls -l /usr/local/mysql
 total 272
 drwxr-xr-x  2 root root    4096 Mar  8 10:52 bin
 drwxr-xr-x  2 root root      55 Mar  8 10:52 docs
 drwxr-xr-x  3 root root    4096 Mar  8 10:52 include
 drwxr-xr-x  5 root root     230 Mar  8 10:52 lib
 -rw-r--r--  1 7161 31415 259199 Sep  7 13:26 LICENSE
 drwxr-xr-x  4 root root      30 Mar  8 10:52 man
 -rw-r--r--  1 7161 31415    566 Sep  7 13:26 README
 drwxr-xr-x 28 root root    4096 Mar  8 10:52 share
 drwxr-xr-x  2 root root      90 Mar  8 10:52 support-files
复制


7. 修改环境变量

 vim /etc/profile
 export PATH=/usr/local/mysql/bin:$PATH
 #生效
 source /etc/profile
复制


8. 创建mysql用户和组

groupadd mysql
useradd -M -s /sbin/nologin mysql -g mysql
-g 指定用户所属组
-s 指定用户登录shell
-M 不创建家目录
/sbin/nologin 表示禁止该用户登录,由于mysql数据库默认由root用户创建,创建mysql用户用于执行,为加强安全,禁止使用mysql用户登录
复制


9. 创建目录并修改权限

 mkdir /data/mysql -p
chown -R mysql.mysql /usr/local/mysql/*
chown -R mysql.mysql /data/*
复制


10. 初始化数据

方法一:
#初始化数据,产生临时密码
mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql

[root@mysql57 bin]# mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql
2022-06-23T02:28:23.342034Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-06-23T02:28:23.836803Z 0 [Warning] InnoDB: New log files created, LSN=45790
2022-06-23T02:28:23.937064Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2022-06-23T02:28:24.005362Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 27e92208-f29c-11ec-bf0a-000c29c91e4a.
2022-06-23T02:28:24.008628Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2022-06-23T02:28:24.693177Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2022-06-23T02:28:24.693191Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2022-06-23T02:28:24.693765Z 0 [Warning] CA certificate ca.pem is self signed.
2022-06-23T02:28:24.958983Z 1 [Note] A temporary password is generated for root@localhost: kLH)o3mUd:ln

grep "password" /var/log/mysqld.log
复制


方法二 :(推荐)
#初始化数据,密码为空
\rm -rf /data/mysql/*
mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql

[root@mysql57 /]# mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql
2022-03-08T03:12:18.165776Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-03-08T03:12:18.335883Z 0 [Warning] InnoDB: New log files created, LSN=45790
2022-03-08T03:12:18.371891Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2022-03-08T03:12:18.429939Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 8ff32dc0-9e8d-11ec-8dd9-000c29c91e4a.
2022-03-08T03:12:18.433036Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2022-03-08T03:12:19.280200Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2022-03-08T03:12:19.280216Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2022-03-08T03:12:19.280751Z 0 [Warning] CA certificate ca.pem is self signed.
2022-03-08T03:12:19.536401Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.

#查看生成的明细文件
[root@mysql57 mysql]# ll -h /data/mysql
total 109M
-rw-r----- 1 mysql mysql 56 Mar 8 11:12 auto.cnf
-rw------- 1 mysql mysql 1.7K Mar 8 11:12 ca-key.pem
-rw-r--r-- 1 mysql mysql 1.1K Mar 8 11:12 ca.pem
-rw-r--r-- 1 mysql mysql 1.1K Mar 8 11:12 client-cert.pem
-rw------- 1 mysql mysql 1.7K Mar 8 11:12 client-key.pem
-rw-r----- 1 mysql mysql 436 Mar 8 11:12 ib_buffer_pool
-rw-r----- 1 mysql mysql 12M Mar 8 11:12 ibdata1
-rw-r----- 1 mysql mysql 48M Mar 8 11:12 ib_logfile0
-rw-r----- 1 mysql mysql 48M Mar 8 11:12 ib_logfile1
drwxr-x--- 2 mysql mysql 4.0K Mar 8 11:12 mysql
drwxr-x--- 2 mysql mysql 8.0K Mar 8 11:12 performance_schema
-rw------- 1 mysql mysql 1.7K Mar 8 11:12 private_key.pem
-rw-r--r-- 1 mysql mysql 452 Mar 8 11:12 public_key.pem
-rw-r--r-- 1 mysql mysql 1.1K Mar 8 11:12 server-cert.pem
-rw------- 1 mysql mysql 1.7K Mar 8 11:12 server-key.pem
drwxr-x--- 2 mysql mysql 8.0K Mar 8 11:12 sys
复制


11. 创建默认配置文件

vim /etc/my.cnf
[mysqld]
user = mysql
basedir = /usr/local/mysql
datadir = /data/mysql
server_id = 1
port = 3306
socket = /tmp/mysql.sock
character-set-server = utf8
collation-server = utf8_general_ci

[mysql]
socket = /tmp/mysql.sock
prompt = 3306 (\\u@\\h) [\\d]>\\_

# prompt=3306 端口号根据实际情况更改
# [\\d]> 命令行显示 [数据库]>
复制


12. 复制启动文件并修改相关参数

复制启动脚本文件到/etc/init.d目录下
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld

修改启动脚本相关参数
vim /etc/init.d/mysqld
basedir=/usr/local/mysql //指定程序路径
datadir=/data/mysql //指定数据存放路径
复制


13. 启动MySQL服务并查看状态

chkconfig --add mysqld 												 //加入开机启动
service mysqld start/stop/status/restart //启动/停止/状态/重启mysql服务
ps aux |grep mysqld //查看mysql进程
netstat -ntlp | grep 3306 //查看3306端口监听情况
复制


14. 修改用户密码

##修改密码
##mysqladmin -uroot -p password
[root@mysql57 system]# mysqladmin -uroot -p password
Enter password:
New password:
Confirm new password:
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.

3306 [mysql]>select host,user,authentication_string from user;
+-----------+---------------+-------------------------------------------+
| host | user | authentication_string |
+-----------+---------------+-------------------------------------------+
| localhost | root | |
| localhost | mysql.session | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| localhost | mysql.sys | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
+-----------+---------------+-------------------------------------------+
3 rows in set (0.00 sec)
复制


15. 创建远程运维用户

CREATE USER 'root'@'%' IDENTIFIED BY '123';
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%';
flush privileges;
复制


16. 参考链接

简书

CSDN

墨天轮

「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论