首页 > 数据库 >MySQL

MySQL

时间:2024-06-12 16:02:56浏览次数:16  
标签:-- MySQL mysqld mysql password root

MySQL

一、MySQL简介

1.1 MySQL 组成和常用工具

1.1.1 客户端程序

mysql: 交互式或非交互式的CLI工具

mysqldump:备份工具,基于mysql协议向mysqld发起查询请求,并将查得的所有数据转换成 insert等写操作语句保存文本文件中

mysqladmin:基于mysql协议管理mysqld

mysqlimport:数据导入工具

MyISAM存储引擎的管理工具:

myisamchk:检查MyISAM库

myisampack:打包MyISAM表,只读

1.1.2 服务器端程序

mysqld_safe

mysqld

mysqld_multi 多实例 ,示例:mysqld_multi --example

1.1.3 mysql命令使用格式

mysql [OPTIONS] [database]

常用选项:

-A, --no-auto-rehash 禁止补全
-u, --user= 用户名,默认为root
-h, --host= 服务器主机,默认为localhost
-p, --passowrd= 用户密码,建议使用-p,默认为空密码
-P, --port= 服务器端口
-S, --socket= 指定连接socket文件路径
-D, --database= 指定默认数据库
-C, --compress 启用压缩
-e   "SQL" 执行SQL命令
-V, --version 显示版本
-v  --verbose 显示详细信息
--print-defaults 获取程序默认使用的配置

1.1.4 mysqladmin命令使用格式

mysqladmin [OPTIONS] command command....

范例:

#查看mysql服务是否正常,如果正常提示mysqld is alive
mysqladmin -uroot -pcentos   ping
#关闭mysql服务,但mysqladmin命令无法开启
mysqladmin -uroot -pcentos shutdown
#创建数据库testdb
mysqladmin -uroot -pcentos   create testdb 
#删除数据库testdb
mysqladmin -uroot -pcentos   drop testdb
#修改root密码
mysqladmin -uroot -pcentos password 'magedu'
#日志滚动,生成新文件/var/lib/mysql/mariadb-bin.00000N
mysqladmin -uroot -pcentos flush-logs

1.1.5 服务器端配置

1.1.5.1 服务器端配置文件

服务器端(mysqld):工作特性有多种配置方式

1、命令行选项

2、配置文件: 类似ini格式集中式的配置,能够为mysql的各种应用程序提供配置信息

服务端配置文件:

/etc/my.cnf   #Global选项
/etc/mysql/my.cnf #Global选项
~/.my.cnf #User-specific 选项

配置文件格式:

[mysqld]
[mysqld_safe]
[mysqld_multi]
[mysql]
[mysqldump]
[server]
[client]
配置值格式:
parameter = value

说明:

_和- 相同
1,ON,TRUE意义相同, 0,OFF,FALSE意义相同,无区分大小写
1.1.5.2 socket 连接说明

服务器监听的两种socket地址:

  • IP socket: 监听在tcp的3306端口,支持远程通信,侦听3306/tcp端口可以在绑定有一个或全部接口的IP上

  • unix sock:监听在sock文件上,仅支持本机通信,如:/var/lib/mysql/mysql.sock

​ 说明:host为localhost时自动使用unix sock

范例:Mysql端口

mysql> SHOW VARIABLES LIKE 'port';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port         | 3306 |
+---------------+-------+
1 row in set (0.01 sec)
#MySQL8.0增加了一个33060/tcp端口
#Port 33060 is the default port for the MySQL Database Extended Interface (the 
MySQL X Protocol).
mysql> SHOW VARIABLES LIKE 'mysqlx_port';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| mysqlx_port   | 33060 |
+---------------+-------+
1 row in set (0.00 sec)

1.1.5.3 关闭mysqld网络连接

只侦听本地客户端口,所有客户端和服务器的交互都是通过一个socket文件实现,socket的配置存放在/var/lib/mysql/mysql.sock 可在/etc/my.cnf修改

范例:

vim /etc/my.cnf
[mysqld]
skip-networking=1
bind_address=127.0.0.1

二、MySQL安装

2.1 二进制安装

2.1.1 通用二进制格式安装 MySQL 8

2.1.1.1 准备用户
groupadd -r -g 306 mysql
useradd -r -g 306 -u 306 -d /data/mysql mysql
2.1.1.2 准备数据目录,建议使用逻辑卷
#可选做,后面的脚本mysql_install_db可自动生成此目录
mkdir /data/mysql
chown mysql:mysql /data/mysql
2.1.1.3 准备二进制程序
root@mysql01:~#tar -xvf   mysql-8.0.19-linux-glibc2.12-x86_64.tar.xz   -C /app/
cd /app
ln -sv mysql-VERSION mysql
chown -R root:root /app/mysql/
2.1.1.4 准备配置文件
root@mysql01:/app/mysql# vim /etc/my.cnf
[mysqld]
# 设置3306端口
port=3306
 
# 设置mysql的安装目录
basedir=/app/mysql
 
# 设置mysql数据库的数据的存放目录
datadir=/data/mysql
 
# 允许最大连接数
#max_connections=151
 
# 服务端使用的字符集默认为UTF8MB4
#character-set-server=utf8mb4
 
# 创建新表时将使用的默认存储引擎
#default-storage-engine=INNODB
 
# 设置mysql的socket文件位置
socket=/data/mysql.sock
 
# 设置mysql服务的pid文件位置
pid-file=/data/mysql/mysql.pid
 
# 启用查询缓存
#query_cache_type=1
#query_cache_size=16M
 
# 设置默认时区
#default-time-zone='+08:00'
 
# 设置内部内存临时表的最大值
#tmp_table_size=16M
 
# 设置内存临时表的最大大小
#max_heap_table_size=16M
 
# 开启慢查询日志
#slow_query_log=1
#slow_query_log_file=/usr/local/mysql/data/mysql-slow.log
 
# 慢查询时长
#long_query_time=10
 
# 二进制日志配置
log_bin=/data/mysql/mysql-bin
server-id=1
skip_name_resolve = on    #禁止主机名解析,建议使用
#[mysql]
# 设置客户端默认字符集
#default-character-set=utf8mb4
 
[client]
# 设置客户端连接服务端时默认使用的端口和socket
#port=3306
socket=/tmp/mysql.sock
#default-character-set=utf8mb4
2.1.1.5 准备环境变量
root@mysql01:/app/mysql# echo 'PATH=/app/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
root@mysql01:/app/mysql# . /etc/profile.d/mysql.sh 
2.1.1.6 初始化数据库文件并提取root密码
2.1.1.6.1 生成随机密码
root@mysql01:/app/mysql# mysqld --initialize --user=mysql --datadir=/data/mysql
2024-06-12T06:19:03.447111Z 0 [System] [MY-013169] [Server] /app/mysql-8.0.19-linux-glibc2.12-x86_64/bin/mysqld (mysqld 8.0.19) initializing of server in progress as process 55880
2024-06-12T06:19:05.715056Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: )=r=u>j;K0i) #随机密码

2.1.1.6.2 生成空密码

mysqld --initialize-insecure --user=mysql --datadir=/data/mysql 
2.1.1.7安全配置向导
root@mysql01:~# mysql_secure_installation 

Securing the MySQL server deployment.

Enter password for user root: 

The existing password for the user account root has expired. Please set a new password.

New password: 

Re-enter new password: 

VALIDATE PASSWORD COMPONENT can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD component?

Press y|Y for Yes, any other key for No: y

There are three levels of password validation policy:

LOW    Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary                  file

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 2
Using existing password for root.

Estimated strength of the password: 100 
Change the password for root ? ((Press y|Y for Yes, any other key for No) : no 

 ... skipping.
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.


Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : no

 ... skipping.
By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.


Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
 - Dropping test database...
Success.

 - Removing privileges on test database...
Success.

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.

All done! 

排错:

root@mysql01:~# mysql -uroot -p
mysql: error while loading shared libraries: libtinfo.so.5: cannot open shared object file: No such file or directory

解决方案:

root@mysql01:~# apt install libncurses5
缺少这个包

2.2 源码编译安装

三、MySQL多实例

3.1 多实例介绍

  • 什么是数据库多实例

多实例类似微信双开,端口号类比微信账号,数据库类比聊天窗口,表类比聊天记录(N50期张柏杰 语录)

MySQL多实例就是在一台服务器上同时开启多个不同的服务端口(如:3306、3307等),同时运 行多个MySQL服务进程,这些服务进程通过不同的Socket监听不同的服务端口来提供服务。 多实例可能是MySQL的不同版本,也可能是MySQL的同一版本实现

  • 多实例的好处

可有效利用服务器资源。当单个服务器资源有剩余时,可以充分利用剩余资源提供更多的服务,且 可以实现资源的逻辑隔离节约服务器资源。例如公司服务器资源紧张,但是数据库又需要各自尽量 独立的提供服务,并且还需要到主从复制等技术,多实例就是最佳选择

  • 多实例弊端

存在资源互相抢占的问题。比如:当某个数据库实例并发很高或者SQL查询慢时,整个实例会消耗 大量的CPU、磁盘I/O等资源,导致服务器上面其他的数据库实例在提供服务的质量也会下降,所以 具体的需求要根据自己的实际情况而定。

3.2 多实例部署

生产中使用较少

SQL语言

一、关系型数据库的常见组件

  • 数据库:databases
  • 表:table,行:row,列:column
  • 索引:index
  • 视图:view
  • 存储过程:procedure
  • 存储函数:function
  • 触发器:trigger
  • 事件调度器:event scheduler,任务计划
  • 用户:user
  • 权限:privilege

1.1 SQL语言规范

  • SQL标准:
#单行注释,注意有空格
-- 注释内容   
#多行注释
/*注释内容
注释内容
注释内容*/ 
  • mysql注释:

    #注释内容
    

标签:--,MySQL,mysqld,mysql,password,root
From: https://www.cnblogs.com/yml2024/p/18244104

相关文章

  • MySQL 8.3.0 主从热备
    IP角色版本192.168.140.153主8.3.0192.168.140.159从8.3.0一、准备环境1、卸载mariadbrpm-qa|grepmariadbrpm-emariadb-libs--nodeps2、安装依赖yum-yinstallperl二、安装MySQL1、下载安装包wgethttps://downloads.mysql.com/archives/get/p/23/file/mysq......
  • Mariadb版本的JDBC驱动,连接云上Mysql出现连接超时
    记录一下一个小问题的解决,Mariadb驱动连接云上Mysql的时候,如果频繁连接偶尔出现读取超时的现象。通过分析报错的堆栈发现,在 org.mariadb.jdbc.internal.protocol.AbstractConnectProtocol#postConnectionQueries这个方法里面有一个判断,usePipelineAuth默认为true,导致设置sock......
  • 【MySQL随缘更】存储过程
    文章目录一、存储过程概述1.1、什么是存储过程1.2、存储过程特点1.3、存储过程优缺点二、存储过程创建2.1、创建格式2.2、变量2.3、变量作用域三、存储过程参数3.1、in3.2、out3.3、inout四、存储过程条件4.1、if…else…endif4.2、if…elseif…else…endif4.3、cas......
  • mysql常用的聚合函数---汇总数据
    此篇文章内容均来自与mysql必知必会教材,后期有衍生会继续更新、补充知识体系结构文章目录AGV()count()根据需求可以进行组合处理max()min()max()、min()、avg()组合使用汇总数据AGV()AVG():1、单列使用AVG();2、多列求平均值的情况下必须使用多个agv()语法:selectav......
  • 深入探究MySQL游标(Cursor)
    前言MySQL游标(Cursor)是MySQL中用于处理查询结果的一种机制。游标允许我们在查询结果集中逐行处理数据,而不是一次性获取所有数据。这对于处理大量数据非常有用,因为它可以减少内存消耗并提高性能。在MySQL中,游标主要用于存储过程和函数。一、游标的概念游标是一个数据库对......
  • 【接口自动化测试框架练习】springboot+react+mysql~极简版postman
    可以说是一个toyprogram,chatgpt完成了一部分工作,我也完成了一部分工作,我俩合作的,我占百分之80%,他百分之20%,哈哈没他不行,源码奉上。https://github.com/Jinwenxin/test-api-frontend1.功能简介:分成三部分,如左侧导航栏所示:测试用例管理:测试用例的增删改查以及运行测试套件管理......
  • mongodb的安装使用、mongodb与redis,memcache,mysql的区别优缺点 以及 好用的MongoDB
    一、mongodb的安装使用、与redis,memcache,mysql的区别优缺点    MongoDB是一个介于关系数据库和非关系数据库之间的基于分布式文件存储的数据库。是非关系数据库当中功能最丰富,最像关系数据库的。他支持的数据结构非常松散,是类似json的bson格式,因此可以存储比较复杂的数......
  • 50.基于SSM的停车场管理系统的设计与实现|基于SSM+ Mysql+Java设计与实现(可运行源码+
    内容概要:全套项目源码+详尽文档,一站式解决您的学习与项目需求。适用人群:计算机、通信、人工智能、自动化等专业的学生、老师及从业者。使用场景及目标:无论是毕设、期末大作业还是课程设计,一键下载,轻松部署,助您轻松完成项目。项目代码经过调试测试,确保直接运行,节省您的时......
  • 【mysql】PROCESSLIST 查看运行中的线程
    发现mysql语句查询响应特别慢,想看一下mysql现时正在做什么事情?通过以下语句,可以看mysql有哪些线程正在运行,也可以帮助定位问题。比如有一些运行特别久的语句。用这个语句看一下:showprocesslistshowfullprocesslist或者用这个语句:select*frominformation_schema.`......
  • MySql 增、删、改、查数据库
    前言之前几天写了MySql的GROUPBY语句和JOIN语句,今天补一下创建数据库、表的语句。首先假设已经暗转好MySQL数据库,然后创建数据库、表。创建数据库createdatabase[ifnotexists]数据库名;[CHARACTERSETcharset_name][COLLATEcollation_name];ifnote......