Linux安装MySQL【Ubuntu20.04】

操作系统:Ubuntu20.04

使用工具:xshell7、xftp7 

MySQL版本:5.7

一、删除Linux系统下自带的mariadb (因为centos自带一个老版Mariadb 与MySQL冲突)

  •  如果没有找到Mariadb直接跳过

#查找Mariadb
rpm -qa | grep mariadb

自带的mariadb

  • 删除Mariadb

yum -y remove mariadb-libs-5.5.68-1.el7.x86_64
  • 最后可以在检查一遍是否删除

二、下载MySQL

  • 官网下载:https://downloads.mysql.com/archives/community/

    下载方式1(用xftp):官网下载到本地用xftp上传到服务器

    下载方式2(用wget): 右键Download复制链接地址

#如果没有wget命需安装
sudo yum -y install wget

#mysql在线下载,后面地址为右键复制的链接
wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz
  • 百度网盘链接:https://pan.baidu.com/s/1qjBKWJAZZOW1WbSxjS3GXA?pwd=cfp5

    提取码:cfp5

Linux安装MySQL【Ubuntu20.04】_第1张图片

三、安装MySQL

  • 创建mysql目录并解压到指定文件夹

#解压文件
tar -zxvf mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz

#移动文件夹并重命名
 mv mysql-5.7.38-linux-glibc2.12-x86_64 /usr/local/mysql
  •    创建mysql用户组和用户并修改权限

groupadd mysql
useradd -r -g mysql mysql
  • 创建数据目录并赋予权限

mkdir -p  /data/mysql               #创建目录
chown mysql:mysql -R /data/mysql  		  #赋予权限
  • 配置my.cnf

vim /etc/my.cnf
  • 添加以下内容

[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.err
pid-file=/data/mysql/mysql.pid
#character config
character_set_server=utf8mb4
symbolic-links=0
explicit_defaults_for_timestamp=true
  • 初始化数据库并查看密码

#进入mysql的bin目录
cd /usr/local/mysql/bin/

#初始化
./mysqld --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql/ --datadir=/data/mysql/ --user=mysql --initialize

#查看密码
cat /data/mysql/mysql.err

Linux安装MySQL【Ubuntu20.04】_第2张图片

四、启动MySQL并更改密码

#先将mysql.server放置到/etc/init.d/mysql中
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql

没启动MySQL时:

service mysql status
root@iZm5e9phbzdxx0lysrv9t2Z:~# service mysql status
● mysql.service - LSB: start and stop MySQL
     Loaded: loaded (/etc/init.d/mysql; generated)
     Active: inactive (dead)
       Docs: man:systemd-sysv-generator(8)

Dec 02 15:57:00 iZm5e9phbzdxx0lysrv9t2Z systemd[1]: Stopped LSB: start and stop MySQL.
Dec 02 15:57:53 iZm5e9phbzdxx0lysrv9t2Z systemd[1]: Starting LSB: start and stop MySQL...
Dec 02 15:57:53 iZm5e9phbzdxx0lysrv9t2Z mysql[2578]: Starting MySQL
Dec 02 15:57:54 iZm5e9phbzdxx0lysrv9t2Z mysql[2578]: . *
Dec 02 15:57:54 iZm5e9phbzdxx0lysrv9t2Z systemd[1]: Started LSB: start and stop MySQL.
Dec 02 16:01:01 iZm5e9phbzdxx0lysrv9t2Z systemd[1]: Stopping LSB: start and stop MySQL...
Dec 02 16:01:01 iZm5e9phbzdxx0lysrv9t2Z mysql[2960]: Shutting down MySQL
Dec 02 16:01:03 iZm5e9phbzdxx0lysrv9t2Z mysql[2960]: .. *
Dec 02 16:01:03 iZm5e9phbzdxx0lysrv9t2Z systemd[1]: mysql.service: Succeeded.
Dec 02 16:01:03 iZm5e9phbzdxx0lysrv9t2Z systemd[1]: Stopped LSB: start and stop MySQL.
root@iZm5e9phbzdxx0lysrv9t2Z:~# 
root@iZm5e9phbzdxx0lysrv9t2Z:~# ps aux | grep mysql
root        3373  0.0  0.0   9032   720 pts/1    S+   16:05   0:00 grep mysql
root@iZm5e9phbzdxx0lysrv9t2Z:~# 

 启动MySQL:

#启动mysql
service mysql start
root@iZm5e9phbzdxx0lysrv9t2Z:~# service mysql start
root@iZm5e9phbzdxx0lysrv9t2Z:~# 
root@iZm5e9phbzdxx0lysrv9t2Z:~# ps aux | grep mysql
root        3407  0.0  0.0   2608  1700 ?        S    16:06   0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/mysql.pid
mysql       3637  0.8  8.9 1096848 181272 ?      Sl   16:06   0:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/data/mysql/mysql.err --pid-file=/data/mysql/mysql.pid --socket=/tmp/mysql.sock --port=3306
root        3669  0.0  0.0   9032   724 pts/1    S+   16:06   0:00 grep mysql
root@iZm5e9phbzdxx0lysrv9t2Z:~# 
root@iZm5e9phbzdxx0lysrv9t2Z:~# service mysql status
● mysql.service - LSB: start and stop MySQL
     Loaded: loaded (/etc/init.d/mysql; generated)
     Active: active (running) since Fri 2022-12-02 16:06:20 CST; 1min 7s ago
       Docs: man:systemd-sysv-generator(8)
    Process: 3382 ExecStart=/etc/init.d/mysql start (code=exited, status=0/SUCCESS)
      Tasks: 28 (limit: 2314)
     Memory: 171.3M
     CGroup: /system.slice/mysql.service
             ├─3407 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/mysql.pid
             └─3637 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/data/mysql/mysql.err --p>

Dec 02 16:06:19 iZm5e9phbzdxx0lysrv9t2Z systemd[1]: Starting LSB: start and stop MySQL...
Dec 02 16:06:19 iZm5e9phbzdxx0lysrv9t2Z mysql[3382]: Starting MySQL
Dec 02 16:06:20 iZm5e9phbzdxx0lysrv9t2Z mysql[3382]: . *
Dec 02 16:06:20 iZm5e9phbzdxx0lysrv9t2Z systemd[1]: Started LSB: start and stop MySQL.

启动MySQL成功【如果显示Failed to start mysql.service: Unit mysql.service not found.重启服务器】

五、MySQL操作

1、登录MySQL

进入Mysql安装目录

root@iZm5e9phbzdxx0lysrv9t2Z:~# cd /usr/local/mysql
root@iZm5e9phbzdxx0lysrv9t2Z:/usr/local/mysql# ls
bin  docs  include  lib  LICENSE  man  README  share  support-files
root@iZm5e9phbzdxx0lysrv9t2Z:/usr/local/mysql# bin/mysql -u root -p
Enter password: 

 如果不希望每次都到bin目录下使用mysql命令则执行以下命令

ln -s  /usr/local/mysql/bin/mysql    /usr/bin

Linux安装MySQL【Ubuntu20.04】_第3张图片

2、更改密码(依次执行)

登陆后如果不修改密码就开始操作MySQL,会出现如下提示:

mysql> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
mysql> 

更改密码: 

SET PASSWORD = PASSWORD('123456');
ALTER USER 'root'@'localhost' PASSWORD EXPIRE NEVER;
FLUSH PRIVILEGES;

3、让navicat能连接MySQL

use mysql                                  #访问mysql库
update user set host = '%' where user = 'root';      	#使root能再任何host访问
FLUSH PRIVILEGES;                             #刷新
  • 放行3306端口让外部连接

#3306 代表端口号
firewall-cmd --zone=public --add-port=3306/tcp --permanent

#刷新生效
firewall-cmd --reload

Linux安装MySQL【Ubuntu20.04】_第4张图片

  • 最后使用navicat连接

Linux安装MySQL【Ubuntu20.04】_第5张图片

六、MySQL一些命令

#查看MySQL状态
service mysql status

#启动MySQL
#service mysql start

#重启MySQL
service mysql reload

Linux安装MySQL - 哔哩哔哩

你可能感兴趣的:(MySQL,linux,运维,服务器)