Ubuntu 22.04安装MySQL

一、MySQL简介

MySQL是一个关系型数据库管理系统,由瑞典 MySQL AB 公司开发,属于 Oracle 旗下产品。MySQL是最流行的关系型数据库管理系统之一,在 WEB 应用方面,MySQL是最好的RDBMS (Relational Database Management System,关系数据库管理系统)应用软件之一。

二、Ubuntu 22.04安装MySQL

1、安装MySQL(apt)

1)安装MySQL

root@ubuntu:~# apt -y update

root@ubuntu:~# apt -y install mysql-server

2)登录MySQL

root@ubuntu:~# mysql

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 8

Server version: 8.0.40-0ubuntu0.22.04.1 (Ubuntu)

 

Copyright (c) 2000, 2024, 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> select version();

+————————-+

| version()                 |

+————————-+

| 8.0.40-0ubuntu0.22.04.1 |

+————————-+

1 row in set (0.00 sec)

3)配置MySQL

root@ubuntu:~# sed -i ‘/pid-file/s/# //’ /etc/mysql/mysql.conf.d/mysqld.cnf

root@ubuntu:~# sed -i ‘/socket/s/# //’ /etc/mysql/mysql.conf.d/mysqld.cnf

root@ubuntu:~# sed -i ‘/3306/s/# //’ /etc/mysql/mysql.conf.d/mysqld.cnf

root@ubuntu:~# sed -i ‘/bind-address/s/127.0.0.1/0.0.0.0/g’ /etc/mysql/mysql.conf.d/mysqld.cnf

root@ubuntu:~# sed -i ‘/datadir/s/# //’ /etc/mysql/mysql.conf.d/mysqld.cnf

root@ubuntu:~# sed -i -e ‘/max_connections/s/# //’ -e ‘/max_connections/s/151/1000/’ /etc/mysql/mysql.conf.d/mysqld.cnf

root@ubuntu:~# sed -i ‘/general_log/s/# //g’ /etc/mysql/mysql.conf.d/mysqld.cnf

root@ubuntu:~# sed -i ‘/slow_query_log/s/# //g’ /etc/mysql/mysql.conf.d/mysqld.cnf

root@ubuntu:~# sed -i ‘/server-id/s/# //’ /etc/mysql/mysql.conf.d/mysqld.cnf

root@ubuntu:~# sed -i ‘/log_bin/s/# //’ /etc/mysql/mysql.conf.d/mysqld.cnf

root@ubuntu:~# sed -i ‘/binlog_expire_logs_seconds/s/# //’ /etc/mysql/mysql.conf.d/mysqld.cnf

root@ubuntu:~# sed -i ‘/max_binlog_size/s/100M/512M/’ /etc/mysql/mysql.conf.d/mysqld.cnf

4)重启MySQL

root@ubuntu:~# systemctl restart mysql

root@ubuntu:~# systemctl status mysql

Ubuntu 22.04安装MySQL-图片1

5)配置root密码

root@ubuntu:~# mysql -u root 2>/dev/null -e “ALTER USER ‘root’@’localhost’ IDENTIFIED WITH mysql_native_password BY ‘G7#Wv8eDQbwdEBCL’;”

root@ubuntu:~# mysql -uroot -pG7#Wv8eDQbwdEBCL

mysql: [Warning] Using a password on the command line interface can be insecure.

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 9

 

Server version: 8.0.40-0ubuntu0.22.04.1 (Ubuntu)

 

Copyright (c) 2000, 2024, 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>

6)常用基本操作

1、启动MySQL

root@ubuntu:~# systemctl start mysql

2、停止MySQL

root@ubuntu:~# systemctl stop mysql

3、重启MySQL

root@ubuntu:~# systemctl restart mysql

4、查询MySQL运行状态

root@ubuntu:~# systemctl status mysql

5、查询MySQL进程

root@ubuntu:~# ps -ef |grep mysql

6、查询MySQL监听端口

root@ubuntu:~# netstat -lntup |grep mysql

7、卸载MySQL

root@ubuntu:~# apt -y –purge autoremove mysql-server

2、安装MySQL(二进制)

1)安装依赖

root@ubuntu:~# apt -y update

root@ubuntu:~# apt -y install wget libncurses5 libnuma-dev

2)下载二进制软件包

root@ubuntu:~# wget https://mirrors.huaweicloud.com/mysql/Downloads/MySQL-8.0/mysql-8.0.29-linux-glibc2.12-x86_64.tar.xz

3)解压

root@ubuntu:~# tar xf mysql-8.0.29-linux-glibc2.12-x86_64.tar.xz

4)重命名

root@ubuntu:~# mv mysql-8.0.29-linux-glibc2.12-x86_64 /usr/local/mysql

5)创建用户和数据库日志目录

root@ubuntu:~# useradd -s /sbin/nologin mysql

root@ubuntu:~# mkdir -p /var/log/mysql

root@ubuntu:~# chown -R mysql:mysql /var/log/mysql

6)配置my.cnf

root@ubuntu:~# vim /etc/my.cnf

# 服务器端配置
[mysqld]
# 数据存储目录
datadir = /data/mysql
# socket通信文件
socket = /tmp/mysql.sock
# 使用Mysql用户启动
user = mysql
# Mysql服务运行的端口号
port = 3306
# 定义error错误文件
log-error = /var/log/mysql/mysql.log
# 开启bin-log日志
log-bin = mysql-bin
# Mysql服务ID号
server-id = 1 
# 开启慢查询日志
slow_query_log = 1
# 慢查询日志记录时间为1秒
slow_launch_time = 1
# 定义慢查询日志文件
slow_query_log_file = /var/log/mysql/slow.log
# binlog日志过期清理时间
expire_logs_days = 5
# 定义binlog的模式为Mixed Level模式,默认为Statement Level
binlog_format = MIXED
# 设置binlog日志文件最大大小,达到512M,自动生成新的Binlog文件
max_binlog_size = 512M
# binlog缓存大小
binlog_cache_size = 256m   
# 最大binlog缓存大小
max_binlog_cache_size = 512m
# 非事务语句缓存大小
binlog_stmt_cache_size = 256m
# PID文件路径
pid-file = mysqld.pid
# 最大连接数
max_connections = 5000
# 开启记录所有SQL语句记录
general_log = 1
# 记录所有SQL语句文件
general_log_file = /var/log/mysql/general.log
# 开启从库同步数据写入log-bin日志
log_slave_updates = 1
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
# 设置字符集为utf8
character-set-server = utf8
# 缓存区大小
innodb_buffer_pool_size = 1G
# 缓冲区实例个数
innodb_buffer_pool_instances = 2
sync_binlog = 0
[client]
default-character-set = utf8
port = 3306
socket = /tmp/mysql.sock
[mysql]
default-character-set = utf8

7)初始化数据库

root@ubuntu:~# mkdir -p /data/mysql

root@ubuntu:~# chown -R mysql:mysql /data/mysql

root@ubuntu:~# /usr/local/mysql/bin/mysqld –initialize-insecure –user=mysql –basedir=/usr/local/mysql –datadir=/data/mysql

root@ubuntu:~# ll /data/mysql/

Ubuntu 22.04安装MySQL-图片2

8)创建软链接

root@ubuntu:~# ln -sf /usr/local/mysql/bin/mysql* /usr/bin

9)配置开机启动及启动MySQL

root@ubuntu:~# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql

root@ubuntu:~# systemctl daemon-reload

root@ubuntu:~# systemctl start mysql

root@ubuntu:~# systemctl status mysql

Ubuntu 22.04安装MySQL

10)设置root密码

root@ubuntu:~# mysql -u root 2>/dev/null -e “ALTER USER ‘root’@’localhost’ IDENTIFIED WITH mysql_native_password BY ‘G7#Wv8eDQbwdEBCL’;”

11)登录MySQL

root@ubuntu:~# mysql -uroot -pG7#Wv8eDQbwdEBCL

mysql: [Warning] Using a password on the command line interface can be insecure.

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 11

Server version: 8.0.29 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> select version();

+———–+

| version() |

+———–+

| 8.0.29 |

+———–+

1 row in set (0.00 sec)

原创文章,作者:admin,如若转载,请注明出处:https://hostingchat.cn/425.html

(0)
admin的头像admin
上一篇 2024年11月7日 下午2:11
下一篇 2024年11月21日 下午4:26

相关推荐

  • Mysql使用mysqldump备份权限整理

    1、创建一个用于备份数据库的用户 mysql> create user ‘backup’@’localhost’ identi…

    2021年6月7日
    000
  • Mysql权限整理及授权命令

    1、创建用户 MariaDB [(none)]> use mysql; MariaDB [mysql]> create user test@’%&#8217…

    2020年7月31日
    000
  • Mysql主从架构部署

    MySQL复制概述 Mysql内建的复制功能是构建大型,高性能应用程序的基础。将Mysql的数据分布到多个系统上去,这种分布的机制,是通过将Mysql的某一台主机的数据复制到其它主…

    2017年12月14日
    000
  • CentOS 7.9构建MySQL PXC高可用集群

    一、Percona XtraDB Cluster相关概念及原理 1、Percona XtraDB Cluster简介 Percona XtraDB Cluster是基于Galera…

    2022年9月1日
    000
  • Linux部署Mysql-Proxy读写分离

    Mysql-Proxy简介 Mysql Proxy是一个处于你的client端和Mysql server端之间的简单程序,它可以监测、分析或改变它们的通信。它使用灵活,没有限制,常…

    2017年12月14日
    000
  • MySQL 连接

    MySQL 连接 使用mysql二进制方式连接 您可以使用MySQL二进制方式进入到mysql命令提示符下来连接MySQL数据库。 实例 以下是从命令行中连接mysql服务器的简单…

    2020年5月27日
    000
  • Mysql数据库备份脚本

    摘要 众所周知数据是应用的核心部分,程序坏了换台机器重新发布就可以,但数据一旦丢失,造成的损失将不可挽回,程序发布到生产后,数据的备份便显得尤为重要,由于不一定所有的服务均有资金完…

    2019年6月25日
    000
  • Linu源码安装Mysql-5.7.18

    一、Mysql-5.7新特性介绍 这一节中,将依次介绍MySQL 5.7的各种新特性。由于MySQL 5.7改进较多,因此,本文将这些新特性进行了简单的分类,分为安全性、灵活性、易…

    2017年12月14日
    000
  • MySQL查看数据库数据量大小、表大小、索引大小

    说明: 通过MySQL的information_schema数据库,可查询数据库中每个表占用的空间、表记录的行数;该库中有一个TABLES表,这个表主要字段分别是: TABLE_S…

    2020年7月7日
    000
  • MySQL Binlog日志三种模式

    binlog 基本认识 MySQL的二进制日志可以说是MySQL最重要的日志了,它记录了所有的DDL和DML(除了数据查询语句)语句,以事件形式记录,还包含语句所执行的消耗的时间,…

    2020年8月5日
    000

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注