DB

1 篇文章 0 订阅

一.MySQL


一.MySQL

1.mysql下载

网址:https://dev.mysql.com/downloads/mysql/



2.Mysql安装好后如何进入CLI

  • 如果已经把mysql\bin增加到PATH环境变量,windows在cmd命令行,linux在shell命令行,执行mysql即可进入mysql CLI
  • 如果没有增加,需要进入mysql/bin目录再执行mysql

Windows使用DOS命令进入MySQL数据库
https://jingyan.baidu.com/article/948f5924dbdca8d80ff5f996.html

C:\Program Files\MySQL\MySQL Server 8.0\bin>mysql.exe -P 3306 -u root -p
Enter password: ****
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 8.0.11 MySQL Community Server - GPL
Copyright © 2000, 2018, Oracle and/or its affiliates. All rights reserved.
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>

将C:\Program Files\MySQL\MySQL Server 8.0\bin加入到系统环境变量Path中。

  • 先在系统变量中新建立MYSQL_HOME环境变量,其值为C:\Program Files\MySQL\MySQL Server 8.0
  • 对系统的Path环境变量做出如下修改:;%MYSQL_HOME%\bin;

C:\Users\lenovo>mysql -P 3306 -u root -p
Enter password:****
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 17
Server version: 8.0.11 MySQL Community Server - GPL
Copyright © 2000, 2018, Oracle and/or its affiliates. All rights reserved.
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>





3.MySQL安装问题–Can’t connect to MySQL server on localhost (10061)

解决方法
https://blog.csdn.net/lianzhang861/article/details/78919060
https://blog.csdn.net/xiaoshaohui1234/article/details/72947707
在这里插入图片描述在这里插入图片描述在这里插入图片描述




4.MySQL常用命令

https://blog.csdn.net/jin13277480598/article/details/52504592
https://blog.csdn.net/vbcssc/article/details/80401800

C:\Users\lenovo>mysql -P 3306 -u root -p
Enter password: ****
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 39
Server version: 8.0.11 MySQL Community Server - GPL
Copyright © 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> help
For information about MySQL products and services, visit:
http://www.mysql.com/
For developer information, including the MySQL Reference Manual, visit:
http://dev.mysql.com/
To buy MySQL Enterprise support, training, or other products, visit:
https://shop.mysql.com/

List of all MySQL commands:
Note that all text commands must be first on line and end with ';'
? (\?) Synonym for help.
clear (\c) Clear the current input statement.
connect (\r) Reconnect to the server. Optional arguments are db and host.
delimiter (\d) Set statement delimiter.
ego (\G) Send command to mysql server, display result vertically.
exit (\q) Exit mysql. Same as quit.
go (\g) Send command to mysql server.
help (\h) Display this help.
notee (\t) Don’t write into outfile.
print (\p) Print current command.
prompt (\R) Change your mysql prompt.
quit (\q) Quit mysql.
rehash (\#) Rebuild completion hash.
source (\.) Execute an SQL script file. Takes a file name as an argument.
status (\s) Get status information from the server.
tee (\T) Set outfile [to_outfile]. Append everything into given outfile.
use (\u) Use another database. Takes database name as argument.
charset (\C) Switch to another charset. Might be needed for processing binlog with multi-byte charsets.
warnings (\W) Show warnings after every statement.
nowarning (\w) Don’t show warnings after every statement.
resetconnection(\x) Clean session context.

For server side help, type 'help contents'

mysql> help contents;
You asked for help about help category: “Contents”
For more information, type 'help <item>', where <item> is one of the following
categories:
Account Management
Administration
Components
Compound Statements
Data Definition
Data Manipulation
Data Types
Functions
Functions and Modifiers for Use with GROUP BY
Geographic Features
Help Metadata
Language Structure
Plugins
Storage Engines
Table Maintenance
Transactions
User-Defined Functions
Utility

mysql> help Account Management
You asked for help about help category: “Account Management”
For more information, type 'help <item>', where <item> is one of the following
topics:
ALTER RESOURCE GROUP
ALTER USER
CREATE RESOURCE GROUP
CREATE ROLE
CREATE USER
DROP RESOURCE GROUP
DROP ROLE
DROP USER
GRANT
RENAME USER
REVOKE
SET DEFAULT ROLE
SET PASSWORD
SET RESOURCE GROUP
SET ROLE

mysql> help Administration;
You asked for help about help category: “Administration”
For more information, type 'help <item>', where <item> is one of the following
topics:
BINLOG
CACHE INDEX
FLUSH
HELP COMMAND
KILL
LOAD INDEX
RESET
RESET PERSIST
RESTART
SET
SET CHARACTER SET
SET NAMES
SHOW
SHOW BINARY LOGS
SHOW BINLOG EVENTS
SHOW CHARACTER SET
SHOW COLLATION
SHOW COLUMNS
SHOW CREATE DATABASE
SHOW CREATE EVENT
SHOW CREATE FUNCTION
SHOW CREATE PROCEDURE
SHOW CREATE TABLE
SHOW CREATE TRIGGER
SHOW CREATE USER
SHOW CREATE VIEW
SHOW DATABASES
SHOW ENGINE
SHOW ENGINES
SHOW ERRORS
SHOW EVENTS
SHOW FUNCTION CODE
SHOW FUNCTION STATUS
SHOW GRANTS
SHOW INDEX
SHOW MASTER STATUS
SHOW OPEN TABLES
SHOW PLUGINS
SHOW PRIVILEGES
SHOW PROCEDURE CODE
SHOW PROCEDURE STATUS
SHOW PROCESSLIST
SHOW PROFILE
SHOW PROFILES
SHOW RELAYLOG EVENTS
SHOW SLAVE HOSTS
SHOW SLAVE STATUS
SHOW STATUS
SHOW TABLE STATUS
SHOW TABLES
SHOW TRIGGERS
SHOW VARIABLES
SHOW WARNINGS
SHUTDOWN




5.MySQL驱动相关问题解决

所有的MySQL驱动下载网址:https://dev.mysql.com/downloads/connector/
JDBC的MySQL驱动:https://dev.mysql.com/downloads/connector/j/
JDBC的MySQL驱动下载时,首先需要选择平台,才可进行下载

JDBC驱动程序的类型

类型1:JDBC-ODBC桥驱动程序
将应用程序对JDBC的调用转换为对ODBC的调用,通过ODBC数据源进行数据库的连接。
ODBC(Open Database Connectivity,开放数据库互连)是Microsoft提出的数据库访问接口标准,定义了访问数据库的API,这些API独立于不同厂商的DBMS,也独立于具体的编程语言。
类型2:数据库本地客户端驱动程序
将应用程序对JDBC的调用委托给数据库的本地客户端,由本地客户端将操作请求和SQL语句发送给数据库的服务器端。服务器端将操作结果通过本地客户端转发给Java程序。
类型3:本地Java驱动程序
使用Java语言实现本地JDBC驱动程序。使得Java程序可以不依赖外在的组件(如ODBC、数据库本地客户端等)直接和数据库进行连接。
类型4:中间件服务器Java驱动程序
Java语言在中间件服务器端实现JDBC驱动程序。客户端使用中间件服务器提供的数据库访问接口进行数据库操作。

JDBC驱动程序的使用建议

建议1:尽量不使用JDBC-ODBC桥驱动程序和数据库本地客户端驱动程序
建议2:使用本地Java驱动程序,对于独立的Java客户端程序是一个好的选择。不仅使得Java客户端不依赖于特定的环境,而且由于不需要经过中间环境的传递,可以获得更好的SQL语句处理速度。
建议3:
对于容器中运行的程序,建议使用中间件服务器提供的JDBC Java驱动程序将数据库访问统一置于中间件服务器的管理之下,具有更好的安全性对于客户端程序来说,也减少了由于数据库变化造成的代码修改。

!!!!! java反射技术!!!!!

https://blog.csdn.net/sinat_38259539/article/details/71799078

mysql-connector-java-5.1.26-bin.jar中Driver.class驱动的字节码文件位于com.mysql.jdbc包中
Class.forName("com.mysql.jdbc.Driver");
com.mysql.jdbc:包名
Driver:字节码文件名
Class.forName:利用反射的方法通过字节码文件(.class)获取信息。
在这里插入图片描述
F:(mysql-connector-java-8.0.11.zip)\mysql-connector-java-8.0.11\mysql-connector-java-8.0.11.jar
mysql-connector-java-8.0.11.jar中Driver.class驱动的字节码文件位于两个包中:com.mysql.cj.jdbc包和com.mysql.jdbc包中
在这里插入图片描述
在这里插入图片描述问题1:
官方标注com.mysql.jdbc包下的Driver.class已经过时,应该使用最新的com.mysql.cj.jdbc包中的Driver.class文件

Loading class com.mysql.jdbc.Driver. This is deprecated(不赞成的). The new driver class iscom.mysql.cj.jdbc.Driver’.

解决:
故新的JDBC驱动的加载应为如下形式:
Class.forName("com.mysql.cj.jdbc.Driver");//根据驱动名称加载DB驱动
String url="jdbc:mysql://localhost:3306/DBNAME;//DB 路径
String user="USERNAME";//DB用户名
String password="PASSWORD";//DB密码
Connection conn=DriverManager.getConnection(url,user,password);//建立并获得DB链接

问题2:

Wed May 09 13:27:07 CST 2018 WARN: Establishing SSL connection without server’s identity verification is not recommended.
According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn’t set.
For compliance with existing applications not using SSL the verifyServerCertificate property is set to ‘false’.
You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.

解决:在你要连接到的数据库的名字后面加上:?&useSSL=false
String url="jdbc:mysql://localhost:3306/DBNAME?&useSSL=false;

问题3:

Exception in thread “main” java.sql.SQLException: The server time zone value ‘???ú±ê×??±??’ is unrecognized or represents more than one time zone.
You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.

解决:
String url = "jdbc:mysql://localhost:3306/DBNAME?serverTimezone=UTC";
在url后面加上?serverTimezone=UTC

故最后mysql-connector-java-8.0.11.jar驱动的加载形式可为如下没有错误的形式:*
Class.forName("com.mysql.cj.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/DBNANME?serverTimezone=UTC","USERNAME","PASSWORD");




6.MySQL 如何修改已建立表的结构

https://blog.csdn.net/zhongbeida_xue/article/details/79442581
https://blog.csdn.net/qq_39698293/article/details/79481241

  • 修改表名
    alter table <旧表名> rename <新表名>;
  • 修改字段的数据类型
    alter table 表名 modify 字段名 新数据类型;
  • 修改字段名和字段的数据类型
    alter table 表名 change 旧字段名 新字段名 新的数据类型;
  • 修改字段位置
    alter table 表名 modify 字段 数据类型 after 已存在的字段/first
  • 增加字段
    alter table 表名 add 新字段名 新数据类型 (约束条件‘如:not null’)(位置‘first’)
    将新加的字段放在某个已存在的字段后
    alter table 表名 add 新字段名 新数据类型 (约束条件) after 已存在的字段;
  • 删除字段
    alter table 表名 drop 字段名;





7.JDBC链接DB的调试建立技巧

在eclipse-jee的IDE中单独建立一个Java Project调试DB链接,操作方面的内容,而不是直接在Dynamic Web Project中直接调试待全部测试通过后在集成到Dynamic Web Project项目中。
Shift+Alt+N->Others...->在新打开的窗口中选择Java Project
在这里插入图片描述

在这里插入图片描述
打开项目的调试有两种方式:

  • 上边菜单栏上的图标
  • 右击项目Debug As->Debug Configuration
    在这里插入图片描述

在这里插入图片描述
Debug As->Debug Configration->在打开的窗口中配置需要调试的项目名称,需要调试的项目的主类。
在这里插入图片描述
在这里插入图片描述
配置完成后将打开Debug专用的perspective透视图
perspective:n观点;远景;透视图;adj透视的
在这里插入图片描述若Debug完成后想回到项目原有的透视图界面有两种方式

  • 点击上面菜单栏最右侧的 Open Perspective图标 ,可以选择你想要使用的各种透视图界面。
    在这里插入图片描述

9ibG9nLmNzZG4ubmV0L3FxXzQxODc1NTA2,size_16,color_FFFFFF,t_70)
在这里插入图片描述- 在 Window菜单栏 下找到 Perspective菜单项 ,在Perspective菜单项中包含了 Open Perspective
在这里插入图片描述
最后DB的链接操作不要忘了引入JDBC驱动包

  • 右击项目:Build Path->Configure Build Path
  • Libraries中点击右侧:Add External JARS
  • 加入事先下载好的JDBC驱动包
    在这里插入图片描述
    在这里插入图片描述



8.mysql与java数据类型对应关系

https://blog.csdn.net/JonWu0102/article/details/81476189
https://blog.csdn.net/liaonanfeng88/article/details/83580314




9.怎么连接虚拟机中的MYSQL数据库服务器

https://blog.csdn.net/link_sky/article/details/79683828
Navicat for MySQL 连接虚拟机上的MYSQL https://blog.csdn.net/liouxiaozhou/article/details/78483126

  1. 要选桥接模式, 确保虚拟机的IP地址要和自己使用的主机IP在一个网段

在这里插入图片描述
2. 关闭虚拟机的防火墙

如果前面两步不做:用远程连接Mysql服务器的数据库会出现2203错误代码

“ERROR2003: Can’t connect to MySQL server on ‘localhost’ (10061)”
从任何主机都能访问到服务器上的MySql的方法 https://www.2cto.com/database/201701/590856.html

在这里插入图片描述

  1. mysql中,给远程连接添加用户权限

服务器上登入mysql后,更改“mysql”数据库里的 “user” 表里的 “host” 项,从”localhost”改称’%’。

第一句:是以权限用户root登录 (cmd,切换到mysql的 bin目录下,再输入如下语句)

mysql -u root -p

第二句:选择mysql库(最后要记得带上;)

mysql>use mysql;

第三句:查看mysql库中的user表的host值(即可进行连接访问的主机/IP名称)

mysql>select host, user from user where user=’root’;

第四句:修改host值(以通配符%的内容增加主机/IP地址),当然也可以直接增加IP地址

mysql>update user set host = ‘%’ where user =‘root’;

第五句:刷新MySQL的系统权限相关表

mysql>flush privileges;

第六句:再重新查看user表时,有修改。

mysql>select host, user from user where user=’root’;




10.解决Mysql报错java.sql.SQLException:null,message from server:"Host ‘xxx’ is not allowed to connect

https://blog.csdn.net/yang5726685/article/details/52529082

在这里插入图片描述
在这里插入图片描述在这里插入图片描述
















  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

killingwill

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值