DB

一.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/*g861/article/details/78919060
https://blog.csdn.net/xiaoshaohui1234/article/details/72947707
DBDBDB




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驱动下载时,首先需要选择平台,才可进行下载

!!!!! 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)获取信息。
DB
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包中
DB
DB问题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
DB

DB
打开项目的调试有两种方式:

  • 上边菜单栏上的图标
  • 右击项目Debug As->Debug Configuration
    DB

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

  • 点击上面菜单栏最右侧的 Open Perspective图标 ,可以选择你想要使用的各种透视图界面。
    DB

DB
DB- 在 Window菜单栏 下找到 Perspective菜单项 ,在Perspective菜单项中包含了 Open Perspective
DB