1.1 linux下安装mysql(mysql V8.0.29)

安装mysql8

0. 查看机器版本

getconf LONG_BIT

1. 下载tar包

# 1.在线下载方式
wget https://cdn.mysql.com//Downloads/MySQL-8.0/mysql-8.0.29-linux-glibc2.12-i686.tar.xz
wget https://cdn.mysql.com//Downloads/MySQL-8.0/mysql-8.0.29-linux-glibc2.12-x86_64.tar.xz
# 离线上传方式
rz -be

# 2.解压到/usr/local
tar -vzxf mysql-8.0.29-linux-glibc2.12-i686 -C /usr/local
tar -vxf mysql-8.0.29-linux-glibc2.12-x86_64.tar.xz -C /usr/local

# 3. 重命名
mv /usr/local/mysql-8.0.29-linux-glibc2.12-i686 /usr/local/mysql

# 4.创建mysql用户组和用户并修改权限,创建数据目录并赋予权限
/*-r 参数表示 mysql 用户是系统用户,不可用于登录系统,创建用户 mysql 并将其添加到用户组 mysql 中*/
groupadd mysql                     #创建用户组
useradd -r -g mysql mysql          #创建用户 mysql 并将其添加到用户组 mysql 中
chown -R mysql /usr/local/mysql/   #分配用户组,将文件的所有属性改为 mysql 用户
chgrp -R mysql /usr/local/mysql/   #分配用户组,将组属性改为 mysql 组

image.png
mkdir -p  /data/mysql              #创建目录
chown mysql:mysql -R /data/mysql   #赋予权限

2. 修改my.cnf配置文件

#创建my.cnf
vim /etc/my.cnf

以下为my.cnf配置

[client]
port = 3306
socket = /tmp/mysql.sock
[mysqld]
bind-address=0.0.0.0
port = 3306
user=mysql
basedir=/usr/local/mysql
datadir=/data/mysql
socket=/tmp/mysql.sock
log-error=/data/mysql/mysql.log
pid-file=/data/mysql/mysql.pid
symbolic-links=0
explicit_defaults_for_timestamp=true
character-set-server = utf8mb4
collation-server = utf8mb4_general_ci
lower_case_table_names = 1
sql_mode = 'STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION,NO_ZERO_DATE,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO'
default-time_zone = '+8:00'

3.初始化数据库

cd /usr/local/mysql/
./bin/mysqld --initialize --user=mysql    #初始化数据库
cat /data/mysql/mysql.log                #查看数据库密码
1653185174(1).png

4.启动服务

#再开一个窗口,进入 cd /usr/local/mysql/support-files/ ,然后./mysql.server start 启动服务
cd /usr/local/mysql/support-files/
./mysql.server start    #启动服务
#----------------------------------------------
./mysql.server restart    #重启服务
image.png

5.登陆

cd /usr/local/mysql/bin/
./mysql -u root -p  #登陆,回车输入mysql.log里的密码
[root@VM-16-15-centos lib64]# cd /usr/local/mysql/bin/
[root@VM-16-15-centos bin]# ./mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.29
Copyright (c) 2000, 2022, 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> 
#修改密码
mysql> alter user 'root'@'localhost' identified by '123456';

Tip: 如出现error while loading shared libraries: libtinfo.so.5: cannot open shared object file

[root@VM-16-15-centos ~]# cd /usr/local/mysql/bin/
[root@VM-16-15-centos bin]# ./mysql -u root -p
./mysql: error while loading shared libraries: libtinfo.so.5: 
cannot open shared object file: No such file or directory
#↓↓↓↓↓↓ 不存在libtinfo.so.5,需要手动切换
ll  /usr/lib64    #查看该目录下的libtinfo.so版本
...
wxr-xr-x   1 root root   411872 Jan 21  2021 libfdisk.so.1.1.0
lrwxrwxrwx   1 root root       15 Jun 16  2020 libffi.so.6 -> libffi.so.6.0.2
-rwxr-xr-x   1 root root    37024 Jun 16  2020 libffi.so.6.0.2
...
#↑↑↑↑↑↑ 将不存在的libtinfo.so.5库用libtinfo.so.6替代,执行如下命令,
再次执>行./mysql -u root -p命令即可。
cd /usr/lib64/
ln -s libtinfo.so.6 libtinfo.so.5   #建立链接

6.加入到系统服务

# 复制 mysql.server 至/etc/init.d/放置到/etc/init.d/,重命名为mysqld
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
#----------------------------------------------
#修改/etc/init.d/mysqld 配置文件
vim /etc/init.d/mysqld
#↓↓↓↓↓↓ 路径与/etc/my.cnf中保持一致
...
# overwritten by settings in the MySQL configuration files.

basedir=/usr/local/mysql
datadir=/data/mysql

# Default value, in seconds, afterwhich the script should timeout waiting
...
#↑↑↑↑↑↑
#添加到系统服务(启动)
chkconfig --add mysqld   # service name -> mysqld
systemctl status mysqld  #检查服务状态
systemctl start mysqld  #启动服务
systemctl stop mysqld  #关闭服务
systemctl reload mysqld  #重启服务
#↓↓↓↓↓↓
[root@VM-16-15-centos bin]# systemctl status mysqld
[root@VM-16-15-centos bin]# systemctl status mysqld
● mysqld.service - LSB: start and stop MySQL
   Loaded: loaded (/etc/rc.d/init.d/mysqld; generated)
   Active: active (running) since Sun 2022-05-22 15:32:14 CST; 3s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 573200 ExecStop=/etc/rc.d/init.d/mysqld stop (code=exited, status=0/SUCCESS)
  Process: 573453 ExecStart=/etc/rc.d/init.d/mysqld start (code=exited, status=0/SUCCESS)
    Tasks: 39 (limit: 10824)
   Memory: 359.8M
   CGroup: /system.slice/mysqld.service
           ├─573466 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/my>
           └─573750 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plu>
#↑↑↑↑↑↑

7.设置环境变量

修改编辑vim /etc/profile文件,与配置jdk类同,添加如下

export MYSQL_HOME=/usr/local/mysql
export PATH=$PATH:$JAVA_HOME/bin:$MYSQL_HOME/bin

8.设置mysql为全局环境变量

#如果是临时加可直接执行该命令,重启会消失
#如果是永久使用,可修改编辑profile文件(vim /etc/profile),在最后添加`,然后就可以快乐的直接使用mysql命令了
export PATH=/usr/local/mysql/bin
#退出编辑后,刷新文件
source /etc/profile

你可能感兴趣的:(1.1 linux下安装mysql(mysql V8.0.29))