ubuntu安装MySQL5.7

文章详细记录了在Ubuntu18.04上安装MySQL5.7的过程,包括下载deb包,解决依赖问题,安装数据库,设置自定义的数据和日志目录,修改AppArmor配置,更新MySQL配置文件以允许远程访问,并创建具有远程登录权限的root用户。最后还涉及了卸载未成功安装的MySQL组件。
摘要由CSDN通过智能技术生成

ubuntu安装MySQL5.7

ubuntu安装MySQL5.7

1、下载MySQL

root@es-node1:/data/apps# wget https://cdn.mysql.com/archives/mysql-5.7/mysql-server_5.7.37-1ubuntu18.04_amd64.deb-bundle.tar
--2023-03-29 15:16:17--  https://cdn.mysql.com/archives/mysql-5.7/mysql-server_5.7.37-1ubuntu18.04_amd64.deb-bundle.tar
Resolving cdn.mysql.com (cdn.mysql.com)... 23.3.85.25
Connecting to cdn.mysql.com (cdn.mysql.com)|23.3.85.25|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 160399360 (153M) [application/x-tar]
Saving to: ‘mysql-server_5.7.37-1ubuntu18.04_amd64.deb-bundle.tar’
​
mysql-server_5.7.37-1ubuntu18.04_amd64.deb-bundle 100%[=============================================================================================================>] 152.97M  6.08MB/s    in 25s     
​
2023-03-29 15:16:44 (6.07 MB/s) - ‘mysql-server_5.7.37-1ubuntu18.04_amd64.deb-bundle.tar’ saved [160399360/160399360]
​

2、解压安装至自定义文件夹

root@es-node1:/data/apps# mkdir /data/apps/mysql -p
root@es-node1:/data/apps# tar xf mysql-server_5.7.37-1ubuntu18.04_amd64.deb-bundle.tar -C /data/apps/mysql
 ***删除2个测试相关的包
root@es-node1:/data/apps# rm -rf mysql-testsuite_5.7.37-1ubuntu18.04_amd64.deb mysql-community-test_5.7.37-1ubuntu18.04_amd64.deb
root@es-node1:/data/apps#
root@es-node1:/data/apps# dpkg -i mysql-*.deb
Selecting previously unselected package mysql-client.
(Reading database ... 206192 files and directories currently installed.)
Preparing to unpack mysql-client_5.7.37-1ubuntu18.04_amd64.deb ...
Unpacking mysql-client (5.7.37-1ubuntu18.04) ...
dpkg: warning: downgrading mysql-common from 5.8+1.0.5ubuntu2 to 5.7.37-1ubuntu18.04
Preparing to unpack mysql-common_5.7.37-1ubuntu18.04_amd64.deb ...
Unpacking mysql-common (5.7.37-1ubuntu18.04) over (5.8+1.0.5ubuntu2) ...
Selecting previously unselected package mysql-community-client.
Preparing to unpack mysql-community-client_5.7.37-1ubuntu18.04_amd64.deb ...
Unpacking mysql-community-client (5.7.37-1ubuntu18.04) ...
Selecting previously unselected package mysql-community-server.
Preparing to unpack mysql-community-server_5.7.37-1ubuntu18.04_amd64.deb ...
Unpacking mysql-community-server (5.7.37-1ubuntu18.04) ...
Selecting previously unselected package mysql-community-source.
Preparing to unpack mysql-community-source_5.7.37-1ubuntu18.04_amd64.deb ...
Unpacking mysql-community-source (5.7.37-1ubuntu18.04) ...
Selecting previously unselected package mysql-server.
Preparing to unpack mysql-server_5.7.37-1ubuntu18.04_amd64.deb ...
Unpacking mysql-server (5.7.37-1ubuntu18.04) ...
Setting up mysql-common (5.7.37-1ubuntu18.04) ...
Installing new version of config file /etc/mysql/conf.d/mysql.cnf ...
Installing new version of config file /etc/mysql/my.cnf.fallback ...
Setting up mysql-community-client (5.7.37-1ubuntu18.04) ...
dpkg: dependency problems prevent configuration of mysql-community-server:
 mysql-community-server depends on libmecab2; however:
  Package libmecab2 is not installed.
​
dpkg: error processing package mysql-community-server (--install):
 dependency problems - leaving unconfigured
Setting up mysql-community-source (5.7.37-1ubuntu18.04) ...
dpkg: dependency problems prevent configuration of mysql-server:
 mysql-server depends on mysql-community-server (= 5.7.37-1ubuntu18.04); however:
  Package mysql-community-server is not configured yet.
​
dpkg: error processing package mysql-server (--install):
 dependency problems - leaving unconfigured
Setting up mysql-client (5.7.37-1ubuntu18.04) ...
Processing triggers for man-db (2.9.1-1) ...
Processing triggers for systemd (245.4-4ubuntu3.16) ...
Errors were encountered while processing:
 mysql-community-server
 mysql-server
  • 上述中因为环境依赖问题未成功安装,接下来我们安装环境依赖

    root@es-node1:/data/apps# apt install libmecab2 libtinfo5 -y

  • 安装完依赖后我们继续安装MySQL

    root@es-node1:/data/apps# dpkg -i mysql-*.deb
    (Reading database … 206441 files and directories currently installed.)
    Preparing to unpack mysql-client_5.7.37-1ubuntu18.04_amd64.deb …
    Unpacking mysql-client (5.7.37-1ubuntu18.04) over (5.7.37-1ubuntu18.04) …
    Preparing to unpack mysql-common_5.7.37-1ubuntu18.04_amd64.deb …
    Unpacking mysql-common (5.7.37-1ubuntu18.04) over (5.7.37-1ubuntu18.04) …
    Preparing to unpack mysql-community-client_5.7.37-1ubuntu18.04_amd64.deb …
    Unpacking mysql-community-client (5.7.37-1ubuntu18.04) over (5.7.37-1ubuntu18.04) …
    Preparing to unpack mysql-community-server_5.7.37-1ubuntu18.04_amd64.deb …
    .
    Unpacking mysql-community-server (5.7.37-1ubuntu18.04) over (5.7.37-1ubuntu18.04) …
    Preparing to unpack mysql-community-source_5.7.37-1ubuntu18.04_amd64.deb …
    Unpacking mysql-community-source (5.7.37-1ubuntu18.04) over (5.7.37-1ubuntu18.04) …
    Preparing to unpack mysql-server_5.7.37-1ubuntu18.04_amd64.deb …
    Unpacking mysql-server (5.7.37-1ubuntu18.04) over (5.7.37-1ubuntu18.04) …
    Setting up mysql-common (5.7.37-1ubuntu18.04) …
    Setting up mysql-community-client (5.7.37-1ubuntu18.04) …
    Setting up mysql-community-source (5.7.37-1ubuntu18.04) …
    Setting up mysql-client (5.7.37-1ubuntu18.04) …
    Setting up mysql-community-server (5.7.37-1ubuntu18.04) …
    Setting up mysql-server (5.7.37-1ubuntu18.04) …
    Processing triggers for man-db (2.9.1-1) …
    Processing triggers for systemd (245.4-4ubuntu3.16) …
    root@es-node1:/data/apps#

    查看mysql运行状态

    root@es-node1:/data/apps# systemctl status mysql.service
    ● mysql.service - MySQL Community Server
    Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
    Active: active (running) since Wed 2023-03-29 15:20:05 CST; 8s ago
    Main PID: 8205 (mysqld)
    Tasks: 27 (limit: 9156)
    Memory: 173.4M
    CGroup: /system.slice/mysql.service
    └─8205 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid

    Mar 29 15:20:05 es-node1 systemd[1]: Starting MySQL Community Server…
    Mar 29 15:20:05 es-node1 systemd[1]: Started MySQL Community Server.
    root@es-node1:/data/apps#
    到此为止MySQL安装成功

修改MySQL远程访问地址 、日志目录和数据目录

root@es-node1:# mkdir /data/mysql                       (自定义创建mysql数据存储的位置)
root@es-node1:# mkdir /data/logs/mysql                  (自定义创建mysql存储日志的位置)
root@es-node1:# cd /data/mysql
root@es-node1:# cp -rf /var/lib/mysql/* ./              (备份原有的MySQL默认的数据文档存储目录)
root@es-node1:# chown -R mysql:mysql mysql/             (修改上面创建的mysql目录的属性)
root@es-node1:/data# cd /data/logs
root@es-node1:/data/logs# chown -R mysql:mysql mysql/

修改服务目录属性

修改文件的50、51和62、63行
root@es-node1:/data# vim /etc/apparmor.d/usr.sbin.mysqld
原有内容:
 50   /var/lib/mysql/ r,
 51   /var/lib/mysql/** rwk,
 
 54   /var/lib/mysql-files/ r,
 55   /var/lib/mysql-files/** rwk,
 修改后内容:
 50   /data/mysql/ r,
 51   /data/mysql/** rwk,
​
 62   /data/logs/mysql/ r,
 63   /data/logs/mysql/** rw,
root@es-node1:/data# vim /etc/apparmor.d/abstractions/mysql
原有内容:       /var/lib/mysql{,d}/mysql{,d}.sock rw,
修改后内容:     /data/mysql{,d}/mysql{,d}.sock rw,
root@es-node1:/data/mysql# systemctl restart apparmor.service

修改MySQL配置文件

root@es-node1:/data/mysql# vim /etc/mysql/mysql.conf.d/mysqld.cnf
原有内容:
[mysqld]
pid-file        = /var/run/mysqld/mysqld.pid
socket          = /var/run/mysqld/mysqld.sock
datadir         = /var/lib/mysql
log-error       = /var/lib/log/mysql/error.log
# By default we only accept connections from localhost
bind-address    = 127.0.0.1
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
修改后内容:
[mysqld]
pid-file        = /var/run/mysqld/mysqld.pid
socket          = /var/run/mysqld/mysqld.sock
datadir         = /data/mysql
log-error       = /data/logs/mysql/error.log
# By default we only accept connections from localhost
bind-address    = 0.0.0.0
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0         #跳过密码登录到数据库
​
root@es-node1:/data/mysql# systemctl restart mysql

新增root管理员用户并开放远程登录权限和密码

root@es-node1:/data/apps# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.37 MySQL Community Server (GPL)
​
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> create user root@'%' identified with mysql_native_password by "填写自己的密码";
Query OK, 0 rows affected (0.01 sec)
​
mysql> grant all on *.* to root@'%';
Query OK, 0 rows affected (0.00 sec)
​
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
​
mysql> 
mysql> 
mysql> exit
​

卸载未安装成功的mysql

1、首先我们查看一下mysql的依赖项

dpkg --list | grep mysql

以上输入会有以下代码的输出:

ii libmysqlclient-dev 5.7.34-0ubuntu0.18.04.1 amd64 MySQL database development files
ii libmysqlclient20:amd64 5.7.34-0ubuntu0.18.04.1 amd64 MySQL database client library
ii mysql-client-5.7 5.7.34-0ubuntu0.18.04.1 amd64 MySQL database client binaries
ii mysql-client-core-5.7 5.7.34-0ubuntu0.18.04.1 amd64 MySQL database core client binaries
ii mysql-common 5.8+1.0.4 all MySQL database common files, e.g. /etc/mysql/my.cnf
ii mysql-server-5.7 5.7.34-0ubuntu0.18.04.1 amd64 MySQL database server binaries and system database setup
ii mysql-server-core-5.7 5.7.34-0ubuntu0.18.04.1 amd64 MySQL database server binaries
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值