一、前言
linux下安装mysql大概有四种安装方式,分别为仓库安装、本地安装、容器安装、以及源码安装。这几种方式分别对应着不同的工作环境,因此掌握这四种安装mysql方式便会极大的提高我们的工作效率,从而获得老板赏识事业平步青云走向人生巅峰!
二、安装流程
-
仓库安装
[root@localhost ~]# cat /etc/redhat-release //展示linux版本 Red Hat Enterprise Linux release 9.1 (Plow) [root@localhost ~]# rpm -ivh https://repo.mysql.com//mysql80-community-release-el9-1.noarch.rpm //安装包 [root@localhost ~]# dnf install mysql-server -y //下载MySQL包 [root@localhost ~]# rpm -qa | grep mysql mysql80-community-release-el9-1.noarch mysql-community-common-8.0.32-1.el9.x86_64 mysql-community-client-plugins-8.0.32-1.el9.x86_64 mysql-community-libs-8.0.32-1.el9.x86_64 mysql-community-client-8.0.32-1.el9.x86_64 mysql-community-icu-data-files-8.0.32-1.el9.x86_64 mysql-community-server-8.0.32-1.el9.x86_64 [root@localhost ~]# systemctl start mysqld [root@localhost ~]# systemctl status mysqld [root@localhost ~]# grep 'temporary password' /var/log/mysqld.log //显示mysql的临时密码 [root@localhost ~]# mysql -uroot -p #登录之后必须先修改用户密码,默认密码策略要求密码至少包含一个大写字母、一个小写字母、一位数字和一个特殊字符,并且密码总长度至少为8个字符。 mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'Admin123!'; //修改密码
-
本地安装
[root@localhost mysql]# wget https://cdn.mysql.com//Downloads/MySQL-8.0/mysql-8.0.32-1.el9.x86_64.rpm-bundle.tar //获得网站mysql包 [root@localhost mysql]# tar xvf mysql-8.0.32-1.el9.x86_64.rpm-bundle.tar //解压mysql包 [root@localhost mysql]# dnf localinstall mysql-community-server-8.0.32-1.el9.x86_64.rpm \ > mysql-community-client-8.0.32-1.el9.x86_64.rpm \ > mysql-community-common-8.0.32-1.el9.x86_64.rpm \ > mysql-community-icu-data-files-8.0.32-1.el9.x86_64.rpm \ > mysql-community-client-plugins-8.0.32-1.el9.x86_64.rpm \ > mysql-community-libs-8.0.32-1.el9.x86_64.rpm [root@localhost mysql]# systemctl start mysqld //开启MySQL服务 [root@localhost mysql]# systemctl status mysqld [root@localhost mysql]# grep 'temporary password' /var/log/mysqld.log [root@localhost ~]# mysql -uroot -p Enter password: mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'Admin123!'; mysql> show databases; mysql> exit
3. 容器安装
[root@localhost ~]# dnf install -y yum-utils device-mapper-persistent-data lvm2 [root@localhost ~]# yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo //添加docker源 [root@localhost ~]# sed -i 's+download.docker.com+mirrors.aliyun.com/docker-ce+' /etc/yum.repos.d/docker-ce.repo [root@localhost ~]# dnf install docker -ce --allowerasing -y //下载docker [root@localhost ~]# systemctl start docker //开启docker [root@localhost ~]# systemctl status docker [root@localhost ~]# docker version [root@localhost ~]# docker run hello-world [root@localhost ~]# docker run --name mysqltest -e MYSQL_ROOT_PASSWORD=123456 -d mysql //用docker启动mysql [root@localhost ~]# docker ps [root@localhost ~]# docker exec -it mysqltest /bin/bash bash-4.4# mysql -uroot -p
4. 源码安装
这个也是四个当中最费劲的一种方式,那么我们来看看具体操作
[root@localhost ~]# wget https://cdn.mysql.com//Downloads/MySQL-8.0/mysql-boost-8.0.32.tar.gz #添加用户和组 [root@localhost ~]# groupadd mysql [root@localhost ~]# useradd -r -g mysql -s /bin/false mysql #开始源码安装 [root@localhost ~]# tar xvf mysql-boost-8.0.32.tar.gz [root@localhost mysql-8.0.32]# yum install gcc gcc-c++ cmake -y [root@localhost bld]# yum install cmake gcc-toolset-12-gcc gcc-toolset-12-gcc-c++ gcc-toolset-12-binutils openssl-devel ncurses-devel libtirpc rpcgen git -y [root@localhost bld]# rpm -ivh https://mirrors.tuna.tsinghua.edu.cn/centos-stream/9-stream/CRB/x86_64/os/Packages/libtirpc-devel-1.3.3-0.el9.x86_64.rpm [root@localhost ~]# cd mysql-8.0.32/ [root@localhost mysql-8.0.32]# mkdir bld [root@localhost mysql-8.0.32]# cd bld [root@localhost bld]# cmake .. -DWITH_BOOST=../boost/boost_1_77_0/ [root@localhost bld]# make ########该过程很久,请耐心等待 [root@localhost bld]# make install [root@localhost mysql]# cd /usr/local/mysql [root@localhost mysql]# mkdir mysql-files [root@localhost mysql]# chown mysql:mysql mysql-files [root@localhost mysql]# chmod 750 mysql-files [root@localhost mysql]# bin/mysqld --initialize --user=mysql [root@localhost mysql]# bin/mysql_ssl_rsa_setup [root@localhost mysql]# bin/mysqld_safe --user=mysql & [root@localhost mysql]# grep 'temporary password' /var/log/mysqld.log [root@localhost mysql]# ./bin/mysql -uroot -p Enter password: mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'Admin123!'; mysql> show databases; mysql> exit #停止mysql服务 [root@localhost mysql]# ./bin/mysqladmin -uroot -pAdmin123! shutdown # Next command is optional [root@localhost mysql]# cp support-files/mysql.server /etc/init.d/mysql.server [root@localhost mysql]# systemctl daemon-reload [root@localhost mysql]# systemctl start mysql######说明1:mysql命令登录数据库说明 #mysql -u 用户名 -p密码 -h连接的数据库服务器的ip [-D]数据库名 -P 端口 [root@mysql8-0-30 ~]# mysql -uroot -pAdmin123! -P3306 -D mysql -h localhost mysql> select version(); +-----------+ | version() | +-----------+ | 8.0.30 | +-----------+ mysql> select database(); +------------+ | database() | +------------+ | mysql | +------------+ mysql> use sys; mysql> select user(); +----------------+ | user() | +----------------+ | root@localhost | +----------------+ mysql> select current_user(); +----------------+ | current_user() | +----------------+ | root@localhost | +----------------+ ######说明2、mysql用户修改密码使用的方式 方式1:使用mysqladmin [root@mysql-8-0-32 ~]# mysqladmin -uroot -p password 'Admin123!' 方式2:使用mysql的alter语句 mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'Admin123!'; 方式3:使用set passwod mysql> set password for root@localhost='Admin123!'; 方式4:使用mysql的update语句,8.0可以删除密码 mysql> select user,host,authentication_string from mysql.user; mysql> update mysql.user set authentication_string='' where user='root'; mysql> flush privileges; mysql> exit Bye [root@mysql-8-0-32 ~]# mysql ######说明3:如果忘记数据库root用户密码。可以通过下面的办法修改 方式1: [root@mysql8-0-30 ~]# systemctl stop mysqld [root@mysql8-0-30 ~]# mysqld --user=mysql --skip-grant-tables [root@mysql8-0-30 ~]# mysql mysql> flush privileges; mysql> alter user root@localhost identified by 'Admin123!'; mysql> exit [root@mysql8-0-30 ~]# killall mysqld 方式2: [root@mysql-8-0-32 ~]# vim /tmp/mysql-init alter user root@localhost identified by 'Admin123!'; [root@mysql-8-0-32 ~]# systemctl stop mysqld [root@mysql-8-0-32 ~]# mysqld --init-file=/tmp/mysql-init --user=mysql & [root@mysql-8-0-32 ~]# killall mysqld ######说明4:配置数据库的root用户远程登录 mysql> select user,host from mysql.user; +------------------+-----------+ | user | host | +------------------+-----------+ | mysql.infoschema | localhost | | mysql.session | localhost | | mysql.sys | localhost | | root | localhost | +------------------+-----------+ mysql.infoschema:系统用户,管理和访问系统自带的information_schema数据库。 mysql.session:mysql的插件将会使用该用户访问mysql数据库服务器。客户端不能直接使用该用户进行链接。 mysql.sys:该用户避免数据库管理员重命名或者删除root用户时发生的问题,客户端不能直接使用该用户进行链接。 root:mysql的超级用户,用于管理mysql数据库,拥有所有权限,可执行任何操作,不建议使用该用户操作mysql数据库。 mysql> update mysql.user set host='%' where user='root'; mysql> flush privileges; #使用命令行界面客户端测试 [root@mysql8-0-30 ~]# mysql -uroot -pAdmin123! -h 192.168.168.128 #使用图形界面客户端连接 https://www.navicat.com.cn/ https://dev.mysql.com/downloads/workbench/ https://dbeaver.io/download/ https://webyog.com/product/sqlyog/