当前位置: 首页 > news >正文

linux源码安装mysql8.0的小白教程

1.下载8.x版本的mysql

MySQL :: Download MySQL Community Server (Archived Versions)

2.安装linux

我安装的是Rocky Linux8.6

3.设置ip地址,方便远程连接

使用nmcli或者nmtui设置或修改ip地址

4.使用远程连接工具MobaXterm操作:

(1)将mysql8版本的压缩包上传到mybaxterm

(2)解压缩

tar -Jxvf mysql-8.0.25-linux-glibc2.12-x86_64.tar.xz

(3)移动压缩后的目录到/usr/local/,并改名为mysql

注意:修改权限mysql:mysql

mv mysql-8.0.25-linux-glibc2.12-x86_64 /usr/local/mysql
useradd mysql -s /sbin/nologin
chown -R mysql.mysql /usr/local/mysql

(4)cd /usr/local/mysql

创建配置文件/etc/my.cnf

[root@localhost bin]# vim /etc/my.cnf
[mysqld]
log-error=/usr/local/mysql/log/mysql.err
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
socket=/tmp/mysql.sock
character-set-server=UTF8MB4

创建必要的文件目录:

mkdir /usr/local/mysql/{binlog,data,log,tmpdir,conf} -p

利用配置文件初始化mysql

[root@localhost mysql]# bin/mysqld \
--defaults-file=/etc/my.cnf \
--user=mysql \
--initialize

初始化数据库,它会给出一个临时密码,记住它,后面要用,如果找不到了,需要走忘记密码流程

(5)启动mysql

[root@localhost mysql]# support-files/mysql.server start

如果是开启中的,启动需要重启mysql

[root@localhost mysql]# support-files/mysql.server restart

(6)处理报错

当执行bin/mysql -u root -p命令时,出现以下报错:

mysql: error while loading shared libraries: libtinfo.so.5: cannot open shared object file: No such file or directory


解决办法:

[root@localhost mysql]# find  / -name 'libtinfo.*'
/usr/lib64/libtinfo.so.6
/usr/lib64/libtinfo.so.6.1[root@localhost mysql]# ln -s /usr/lib64/libtinfo.so.6.1 /usr/lib64/libtinfo.so.5

(7)如果想要service控制它的开启和关闭

[root@localhost mysql]# ls
bin  data  docs  include  lib  LICENSE  man  README  share  support-files
[root@localhost mysql]# cp -a ./support-files/mysql.server /etc/init.d/mysql
#chkconfig --add mysql 这条命令的作用是将 mysql 服务添加到系统的服务管理列表中,使得系统能够通过 chkconfig 工具来管理该服务的启动级别。
[root@localhost mysql]# chkconfig --add mysql
[root@localhost mysql]# service mysql restart
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!
[root@localhost mysql]# service mysql statusSUCCESS! MySQL running (26540)
[root@localhost mysql]# ln -s /usr/local/mysql/bin/mysql /usr/bin

(8)使用临时密码登录验证mysql


[root@localhost mysql]# mysql -uroot -p"is/BIErGB051"
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 11
Server version: 8.0.25Copyright (c) 2000, 2021, Oracle and/or its affiliates.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> 

(9)修改密码

mysql> alter user root@"localhost" identified by "123456"

(10)exit退出,使用新密码登录

[root@localhost mysql]# mysql -uroot -p123456
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 12
Server version: 8.0.25 MySQL Community Server - GPLCopyright (c) 2000, 2021, Oracle and/or its affiliates.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>

(11)使用systemctl控制mysql

• systemd一个更高效的系统&服务管理器

– 开机服务并行启动,各系统服务间的精确依赖

– 服务目录:/usr/lib/systemd/system/

– 主要管理工具:systemctl

systemd方式:

注意: 为了和yum下载的情况一致,设置文件名称为mysqld.service,使用systemctl命令可以省略".service后缀"

systemctl start mysqld #启动mysqld

systemctl stop mysqld #停止mysqld

systemd管理方式思路:

用户--systemctl--》systemd--服务启动配置文件--》mysqld启动

•管理员服务文件默认路径

–/usr/lib/systemd/system/

•重新加载服务启动配置文件(每次修改mysqld.service文件都要执行这个命令一次)

systemctl daemon-reload

再启动mysqld:

systemctl start mysqld

[root@localhost system]# cd   /usr/lib/systemd/system   
[root@localhost system]# vim mysqld.service
[Unit]
Description=MySQL Server
After=network.target[Service]
Type=forkingPIDFile=/usr/local/mysql/data/mysql1.pid
ExecStart=/usr/local/mysql/support-files/mysql.server start
ExecStop=/usr/local/mysql/support-files/mysql.server stop[Install]
WantedBy=multi-user.target[root@localhost system]# systemctl daemon-reload[root@localhost system]# systemctl stop mysqld
[root@localhost system]# systemctl status mysqld
[root@localhost system]# systemctl start mysqld

5.使用shell脚本安装,mysql并启动:

为了更好的效果,新建一个Linux虚拟机处理:

(1) 设置hostname,方便查看pid文件(因为pid的文件名称与hostname有关):

就设置为mysql1

(2) 准备/etc/my.cnf文件(为了方便使用之前的同名文件也可)

[mysqld]
log-error=/usr/local/mysql/log/mysql.err
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
socket=/tmp/mysql.sock
character-set-server=UTF8MB4

(3)shell脚本文件install_mysql.sh

#!/bin/bashif [ -d "/usr/local/mysql" ];thenecho "/usr/local/mysql文件夹已经存在,请确认是否安装了mysql"exit
fi
echo "正在解压压缩包"
tar -Jxf mysql-8.0.25-linux-glibc2.12-x86_64.tar.xz
mv mysql-8.0.25-linux-glibc2.12-x86_64 /usr/local/mysql#创建一些必要的文件夹
mkdir /usr/local/mysql/{binlog,data,log,tmpdir,conf} -p#检查mysql进程
mysql_pid=`ps -ef | grep mysqld | wc -l`
if [ $mysql_pid -eq 1 ];thenecho "mysql进程没有运行"
elseecho "有mysql进程运行,请检查"
fi#检查mysql用户
mysql_user=`cat /etc/passwd | grep -w mysql | wc -l`
if [ $mysql_user -eq 1 ];thenecho "mysql用户已经存在"
elseecho "mysql用户不存在,开始添加mysql用户"useradd mysql -s /sbin/nologinecho "添加mysql用户成功"
fi#处理libtinfo.so.5报错
ln -s /usr/lib64/libtinfo.so.6.1 /usr/lib64/libtinfo.so.5#修改目录权限
chown -R mysql.mysql /usr/local/mysql#增加配置文件
if [ -f "/etc/my.cnf" ];thenecho "/etc/my.cnf文件存在"
elseecho "/etc/my.cnf文件不存在"exit
fi#初始化
echo "开始初始化"
/usr/local/mysql/bin/mysqld \
--defaults-file=/etc/my.cnf \
--user=mysql \
--initialize
#判断初始化是否成功
mysql_init=`cat /usr/local/mysql/log/mysql.err | grep -i "root@localhost:" | wc -l`
if [ $mysql_init -eq 1 ];thenecho "mysql初始化成功"
elseecho "mysql初始化失败"exit
fi#获取临时密码
temp_pwd=$(grep 'temporary password' /usr/local/mysql/log/mysql.err)
pwd=${temp_pwd##* }
echo "临时密码是: ${pwd}"#配置启动脚本
if [ ! -f "/etc/init.d/mysql.server" ];thencp /usr/local/mysql/support-files/mysql.server /etc/init.d/ -rfchmod 700 /etc/init.d/mysql.server
fi#启动mysql
/etc/init.d/mysql.server start#增加家环境变量
mysql_path=`grep 'export PATH=$PATH:/usr/local/mysql/bin' /etc/profile | wc -l`
if [ $mysql_path -eq 0 ];thenecho "export PATH=\$PATH:/usr/local/mysql/bin" >> /etc/profilesource /etc/profile
fi#通过临时密码登录mysql,并修改密码
mysql -uroot -p${pwd} --connect-expired-password -e 'alter user root@"localhost" identified by "123456"'
echo "mysql8.0.25安装完成!"

(4)给脚本添加执行权限

chomod +x install_mysql.sh

(5)执行脚本

./install_mysql.sh

注意:如果要使用service命令处理mysql,需要再将/etc/init.d/mysql.server改为/etc/init.d/mysql,如下图所示,也可以直接到脚本文件相应的位置修改代码!

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • Nginx和Tomcat实现负载均衡群集部署应用
  • k8s record 20240705
  • 视频号矩阵系统源码,实现AI自动生成文案和自动回复私信评论,支持多个短视频平台
  • Android Camera Framework:从基础到高级
  • vue3+springboot+mybatis+mysql项目实践--简单登录注册功能实现
  • seaweedfs + TiKV 部署保姆级教程
  • C语言文件操作技术详解
  • React组件间通信的几种方式
  • Spring相关的面试题
  • vue3中使用 tilwindcss报错 Unknown at rule @tailwindcss
  • QT之嵌入外部第三方软件到本窗体中
  • Java面试八股之Redis有哪些数据类型?底层实现分别是什么
  • 基于ssm的图书管理系统的设计与实现
  • jenkins打包java项目报错Error: Unable to access jarfile tlm-admin.jar
  • 部署大语言模型并对话
  • “Material Design”设计规范在 ComponentOne For WinForm 的全新尝试!
  • 【EOS】Cleos基础
  • 【附node操作实例】redis简明入门系列—字符串类型
  • 【跃迁之路】【641天】程序员高效学习方法论探索系列(实验阶段398-2018.11.14)...
  • CentOS 7 修改主机名
  • CSS实用技巧干货
  • Dubbo 整合 Pinpoint 做分布式服务请求跟踪
  • eclipse的离线汉化
  • Facebook AccountKit 接入的坑点
  • git 常用命令
  • java中具有继承关系的类及其对象初始化顺序
  • Linux学习笔记6-使用fdisk进行磁盘管理
  • vue 个人积累(使用工具,组件)
  • 基于Dubbo+ZooKeeper的分布式服务的实现
  • 警报:线上事故之CountDownLatch的威力
  • 聊一聊前端的监控
  • 前端存储 - localStorage
  • 三栏布局总结
  • 微信小程序上拉加载:onReachBottom详解+设置触发距离
  • 问题之ssh中Host key verification failed的解决
  • 想写好前端,先练好内功
  • 移动端唤起键盘时取消position:fixed定位
  • 【云吞铺子】性能抖动剖析(二)
  • ​卜东波研究员:高观点下的少儿计算思维
  • $jQuery 重写Alert样式方法
  • (3)选择元素——(17)练习(Exercises)
  • (CVPRW,2024)可学习的提示:遥感领域小样本语义分割
  • (pojstep1.1.1)poj 1298(直叙式模拟)
  • (ZT)出版业改革:该死的死,该生的生
  • (机器学习-深度学习快速入门)第三章机器学习-第二节:机器学习模型之线性回归
  • (算法二)滑动窗口
  • (一)WLAN定义和基本架构转
  • (一)认识微服务
  • (最新)华为 2024 届秋招-硬件技术工程师-单板硬件开发—机试题—(共12套)(每套四十题)
  • *1 计算机基础和操作系统基础及几大协议
  • .jks文件(JAVA KeyStore)
  • .net core 外观者设计模式 实现,多种支付选择
  • .Net Remoting常用部署结构
  • .NET/C# 项目如何优雅地设置条件编译符号?
  • .net安装_还在用第三方安装.NET?Win10自带.NET3.5安装