程序员开发实例大全宝库

网站首页 > 编程文章 正文

centos 7 版本安装mysql8.0(centos7安装教程)

zazugpt 2024-08-28 00:00:55 编程文章 15 ℃ 0 评论


安装方式

此文章介绍基于centos7 使用tar 包安装对应的mysql版本

1.如何下载mysql对应版本(确定系统的相关参数)

2.如何初始化mysql以及添加相关组以及数据等

3.如何登录以及权限授权用户远程登录


a下载mysql

官网下载地址(https://dev.mysql.com/downloads/mysql/)《需提前注册相关账号,填相关信息下载》

我的下载地址

https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.39-linux-glibc2.17-x86_64.tar.xz

下载前需确认系统相关的参数:

如确认系统信息以及glibc 版本

      //查看系统信息
[root@instance-xrq8pqrp ~]# uname -a
Linux instance-xrq8pqrp 3.10.0-1160.83.1.el7.x86_64 #1 SMP Wed Jan 25 16:41:43 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux
//查看glibc版本
[root@instance-xrq8pqrp ~]# rpm -q glibc
glibc-2.17-326.el7_9.x86_64

通过以上可确认对应的下载信息版本

  wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.39-linux-glibc2.17-x86_64.tar.xz

b.操作安装前的准备工作

1.解压文件
[root@instance-xrq8pqrp ~]# tar -xJf mysql-8.0.39-linux-glibc2.17-x86_64.tar.xz
[root@instance-xrq8pqrp ~]# pwd
/root
[root@instance-xrq8pqrp ~]# ls
mysql-8.0.39-linux-glibc2.17-x86_64  mysql-8.0.39-linux-glibc2.17-x86_64.tar.xz
[root@instance-xrq8pqrp ~]# mv mysql-8.0.39-linux-glibc2.17-x86_64 mysql
[root@instance-xrq8pqrp ~]# ls
mysql  mysql-8.0.39-linux-glibc2.17-x86_64.tar.xz


[root@instance-xrq8pqrp ~]#mv mysql /usr/local/
  
[root@instance-xrq8pqrp ~]#  cd /usr/local/mysql/
  //创建数据存储目录以及授权
[root@instance-xrq8pqrp ~]#mkdir /usr/local/mysql/data  

[root@instance-xrq8pqrp ~]#chown -R mysql:mysql /usr/local/mysql/
 
[root@instance-xrq8pqrp ~]#chmod -R 755 /usr/local/mysql/

  2.添加环境变量
[root@instance-xrq8pqrp ~]# cat /etc/profile
‘‘‘‘’’’’
   export MYSQL_HOME=/usr/local/mysql
   export PATH=$MYSQL_HOME/bin:$PATH
   [root@instance-xrq8pqrp ~]# source /etc/profile
   
  3.准备配置文件以及设置开机启动
 //备份原配置文件
  [root@instance-xrq8pqrp ~]# mv /etc/my.cnf /etc/my.cnf.bak
//  新的配置文件如下
  [root@instance-xrq8pqrp ~]# cat /etc/my.cnf
[mysql]
# 设置mysql客户端默认字符集
default-character-set = utf8mb4

[mysqld]
#设置3306端口
port = 3306  
# 设置mysql的安装目录
basedir = /usr/local/mysql
# 设置mysql数据库的数据的存放目录
datadir = /usr/local/mysql/data

socket = /tmp/mysql.sock
#日志
log-error = /usr/local/mysql/data/mysql.log
pid-file = /usr/local/mysql/data/mysql.pid
# 允许最大连接数
max_connections = 1000 
#数据库默认字符集, 主流字符集支持一些特殊表情符号(特殊表情符占用4个字节)
character-set-server = utf8mb4
#数据库字符集对应一些排序等规则,注意要和character-set-server对应
collation-server = utf8mb4_general_ci
#设置client连接mysql时的字符集,防止乱码
init_connect = 'SET NAMES utf8mb4'
# 创建新表时将使用的默认存储引擎
default-storage-engine = INNODB
lower_case_table_names = 1 
max_allowed_packet = 16M

[client]
port  = 3306
socket  = /tmp/mysql.sock
//配置开机启动
[root@instance-xrq8pqrp ~]# ln -s /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@instance-xrq8pqrp ~]# chmod +x /etc/init.d/mysqld
[root@instance-xrq8pqrp ~]# chkconfig --add mysqld
[root@instance-xrq8pqrp ~]# chkconfig --list

注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。 

      要列出 systemd 服务,请执行 'systemctl list-unit-files'。
      查看在具体 target 启用的服务请执行
      'systemctl list-dependencies [target]'。

bcm-agent       0:关    1:关    2:开    3:开    4:开    5:开    6:关
mysqld          0:关    1:关    2:开    3:开    4:开    5:开    6:关
netconsole      0:关    1:关    2:关    3:关    4:关    5:关    6:关
network         0:关    1:关    2:开    3:开    4:开    5:开    6:关
opensmd         0:关    1:关    2:关    3:关    4:关    5:关    6:关
rdma            0:关    1:关    2:开    3:开    4:开    5:开    6:关

c 安装以及启动

//1.初始化数据
[root@instance-xrq8pqrp ~]#  mysqld --initialize  --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
//2.启动数据库
[root@instance-xrq8pqrp ~]#  systemctl start mysqld
//3.查看账号密码
[root@instance-xrq8pqrp ~]#   cat /usr/local/mysql/data/mysql.log | grep password

授权数据库远程登录

// 1.登录数据库修改密码
[root@instance-xrq8pqrp ~]# mysql -u root -p
Enter password:   
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';
Query OK, 0 rows affected (0.01 sec)

mysql> exit;

//授权远程登录


mysql> create user 'root'@'%' identified with mysql_native_password by '123456';
Query OK, 0 rows affected (0.01 sec)

mysql> grant all privileges on *.* to 'root'@'%' with grant option;
Query OK, 0 rows affected (0.01 sec)

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

mysql> 
  
  


至此centos7 数据库安装完成!


点关注不迷路,走技术路!

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表