• linux安装mysql


    linux安装mysql

    安装mysql

    docker方式安装

    检查docker版本

    docker --version
    
    • 1

    确保Docker启动

    systemctl start docker
    
    • 1

    查看可用的 MySQL 版本

    docker search mysql
    
    • 1

    拉取最新版MySQL镜像

    (如果要安装指定版本将latest换成版本号即可)

    docker pull mysql:latest
    
    • 1

    查看已下载的镜像

    docker image ls 
    或者
    docker images
    
    • 1
    • 2
    • 3

    运行容器
    参数说明:
    • -p 3306:3306 :映射容器服务的 3306 端口到宿主机的 3306 端口,外部主机可以直接通过 宿主机ip:3306 访问到 MySQL 的服务。
    • MYSQL_ROOT_PASSWORD=123456:设置 MySQL 服务 root 用户的密码。
    • --name 容器别名

    docker run -itd --name mysql-test -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql
    
    • 1

    设置docker 自启动

    systemctl enable docker.service
    
    • 1

    查看自启动

    systemctl list-unit-files | grep enable
    
    • 1

    设置 docker mysql镜像自启动

    docker update c90c76bd1dae --restart=always
    
    • 1
    #2.进入mysql容器:这里的mysql-test最好使用容器的id来替换
    docker exec -it mysql-test bash
    #3.使用账号密码登录
    mysql -uroot -p123456
    #4.创建数据库:
    CREATE DATABASE luckyframe CHARACTER SET utf8;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    直接安装

    一、安装之前检测系统是否有自带的MySQL

    rpm -qa | grep mysql #检查是否安装过MySQL

    rpm -qa | grep mariadb #检查是否存在 mariadb 数据库(内置的MySQL数据库),有则强制删除

    rpm -e --nodeps mariadb-libs-5.5.68-1.el7.x86_64 #强制删除

    [root@MeterSphere /home/test]$rpm -qa | grep mysql
    [root@MeterSphere /home/test]$rpm -qa | grep mariadb
    mariadb-libs-5.5.68-1.el7.x86_64
    [root@MeterSphere /home/test]$rpm -e --nodeps  mariadb-libs-5.5.68-1.el7.x86_64
    
    • 1
    • 2
    • 3
    • 4
    二、下载mysql源
    [root@MeterSphere /home/test]$rpm -e --nodeps  mariadb-libs-5.5.68-1.el7.x86_64
    [root@MeterSphere /home/test]$curl -O https://repo.mysql.com//mysql57-community-release-el7-11.noarch.rpm
      % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                     Dload  Upload   Total   Spent    Left  Speed
    100 25680  100 25680    0     0  21371      0  0:00:01  0:00:01 --:--:-- 21382
    [root@MeterSphere /home/test]$
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    三、安装mysql源
    [root@MeterSphere /home/test]$yum localinstall mysql57-community-release-el7-11.noarch.rpm
    
    • 1
    四、检查mysql源是否安装成功

    出现以下信息表示安装成功了

    [root@MeterSphere /home/test]$yum repolist enabled | grep "mysql.*-community.*"
    
    mysql-connectors-community/x86_64 MySQL Connectors Community                 206
    mysql-tools-community/x86_64      MySQL Tools Community                       94
    mysql57-community/x86_64          MySQL 5.7 Community Server                 624
    [root@MeterSphere /home/test]$
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    五、有了MySQL源后安装MySQL
    [root@MeterSphere /home/test]$yum install mysql-community-server
    
    • 1

    安装会报错

    Public key for mysql-community-client-5.7.40-1.el7.x86_64.rpm is not installed
    
    
     Failing package is: mysql-community-client-5.7.40-1.el7.x86_64
     GPG Keys are configured as: file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
    
    • 1
    • 2
    • 3
    • 4
    • 5

    安装报错,提示无公共秘钥

    解决方法:

    [root@MeterSphere /home/test]$rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
    
    • 1

    然后再次安装

    [root@MeterSphere /home/test]$yum install mysql-community-server
    
    • 1
    六、查看是否安装成功

    有对应的server的信息即为成功

    [root@MeterSphere /home/test]$yum list installed mysql-*
    Loaded plugins: fastestmirror
    Loading mirror speeds from cached hostfile
    Installed Packages
    mysql-community-client.x86_64                               5.7.40-1.el7                                @mysql57-communitymysql-community-common.x86_64                               5.7.40-1.el7                                @mysql57-communitymysql-community-libs.x86_64                                 5.7.40-1.el7                                @mysql57-communitymysql-community-server.x86_64                               5.7.40-1.el7                                @mysql57-community[root@MeterSphere /home/test]$
    
    • 1
    • 2
    • 3
    • 4
    • 5
    七、启动MySQL并检查MySQL运行状态
    [root@MeterSphere /home/test]$systemctl start mysqld
    [root@MeterSphere /home/test]$systemctl status mysqld
    ● mysqld.service - MySQL Server
       Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
       Active: active (running) since Wed 2022-11-02 13:05:30 CST; 9s ago
         Docs: man:mysqld(8)
               http://dev.mysql.com/doc/refman/en/using-systemd.html
      Process: 18745 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
      Process: 18596 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
     Main PID: 18749 (mysqld)
        Tasks: 27
       Memory: 320.6M
       CGroup: /system.slice/mysqld.service
               └─18749 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
    
    Nov 02 13:05:25 MeterSphere.teyuntong.net systemd[1]: Starting MySQL Server...
    Nov 02 13:05:30 MeterSphere.teyuntong.net systemd[1]: Started MySQL Server.
    [root@MeterSphere /home/test]$
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    八、获取MySQL默认登录密码,登录MySQL,并修改默认密码
    [root@MeterSphere /home/test]$grep 'temporary password' /var/log/mysqld.log
    2022-11-02T05:05:27.765831Z 1 [Note] A temporary password is generated for root@localhost: mhNQq>rje16d
    
    
    • 1
    • 2
    • 3

    输入以下命令后,再输入默认登录密码,就能以 root 帐号登录 mysql

    [root@MeterSphere /home/test]$mysql -uroot -p
    
    • 1

    在创建数据库或数据表之前,要先用一下命令修改 root 用户的密码

    mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'Mysql@123456';
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> 
    
    • 1
    • 2
    • 3
    • 4
    九、授予root用户远程访问权限,并刷新权限使生效

    grant all privileges on . to ‘root’ @‘%’ identified by ‘Mysql@123’;

    第一个*是数据库,可以改成允许访问的数据库名称

    第二个 是数据库的表名称,代表允许访问任意的表

    root代表远程登录使用的用户名,可以自定义

    %代表允许任意ip登录,如果你想指定特定的IP,可以把%替换掉就可以了

    password代表远程登录时使用的密码,可以自定义

    让权限立即生效:flush privileges

    grant all privileges on *.* to 'root' @'%' identified by 'Mysql@123456' with grant option;
    
    • 1
    mysql> grant all privileges on *.* to 'root' @'%' identified by 'Mysql@123456'; 
    Query OK, 0 rows affected, 1 warning (0.00 sec)
    
    mysql> flush privileges;
    Query OK, 0 rows affected (0.01 sec)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    十、设置为开机自启动
    [root@MeterSphere /home/test]$systemctl enable mysqld
    
    • 1
    十一、MySQL配置文件保存在/etc/路径下,可修改配置,重启MySQL服务后配置生效
    [root@MeterSphere /home/test]$cat /etc/my.cnf
    # For advice on how to change settings please see
    # http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
    
    [mysqld]
    #
    # Remove leading # and set to the amount of RAM for the most important data
    # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
    # innodb_buffer_pool_size = 128M
    #
    # Remove leading # to turn on a very important data integrity option: logging
    # changes to the binary log between backups.
    # log_bin
    #
    # Remove leading # to set options mainly useful for reporting servers.
    # The server defaults are faster for transactions and fast SELECTs.
    # Adjust sizes as needed, experiment to find the optimal values.
    # join_buffer_size = 128M
    # sort_buffer_size = 2M
    # read_rnd_buffer_size = 2M
    datadir=/var/lib/mysql
    socket=/var/lib/mysql/mysql.sock
    
    # Disabling symbolic-links is recommended to prevent assorted security risks
    symbolic-links=0
    
    log-error=/var/log/mysqld.log
    pid-file=/var/run/mysqld/mysqld.pid
    [root@MeterSphere /home/test]$
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    重启

    systemctl restart mysqld
    
    • 1

    卸载Mysql

    1、查看MySQL安装情况:

    强制删除已安装MySQL

    rpm -qa | grep mysql
    
    • 1

    2、查找所有MySQL目录并删除:

    find / -name mysql 
    
    • 1

    3、 删除my.cnf:

    rm -f /etc/my.cnf
    
    • 1
  • 相关阅读:
    怎么裁剪视频时长?手把手教你裁剪
    2021.03青少年软件编程(Python)等级考试试卷(四级)
    多线程可见
    Notion——构建个人知识库
    工厂智能工具介绍——5W1H
    OpenDataV低代码平台新增组件流程
    Kafka关键点总结-消息的顺序性
    第11章 AOF持久化
    校企合作再开新篇|哈尔滨工业大学人工智能研究院院长刘劼一行莅临云畅科技考察调研
    js正则表达式
  • 原文地址:https://blog.csdn.net/qq13933506749/article/details/127668376