下载MySQL Installer

https://dev.mysql.com/downloads/installer/

配置编码

C:\ProgramData\MySQL\MySQL Server 5.7\my.ini

  1. [mysqld]
  2. character-set-server=utf8

配置好后重启服务
image.png

命令行登录数据库

image.png

查看编码

show variables like '%character%';
image.png

CentOS安装MySQL

下载rpm:wget http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
安装rpm:yum -y install mysql57-community-release-el7-10.noarch.rpm
安装MySQL:yum -y install mysql-community-server
启动MySQL:systemctl start mysqld.service
查看初始密码:grep "password" /var/log/mysqld.log
登录数据库:mysql -uroot -p
修改密码安全策略为低:set global validate_password_policy=0;
修改root密码:ALTER USER 'root'@'localhost' IDENTIFIED BY '12345678';
授予root远程管理权限:GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '12345678';
退出:exit

操作数据库基本命令

查看所有数据库:show databases;
创建数据库:create database hello;
使用数据库:use hello;
删除数据库:drop database hello;
修改数据库编码:alter database hello character set utf8;

用户管理

创建用户:create user abc@localhost identified by '123456';
创建用户:create user xyz@'%' identified by '123456';
分配权限:grant all on hello.* to abc@localhost;
分配权限:grant insert,update,select,delete on hello.* to xyz@'%';
查看权限:show grants for abc@localhost;
撤销权限:revoke delete,update on hello.* from xyz@'%';
撤销权限:revoke all on hello.* from xyz@'%';
删除用户:drop user xyz@'%';
删除用户:drop user abc@localhost;

操作表基本命令

查看当前库的所有表:show tables;
创建表:

  1. CREATE TABLE `user` (
  2. `id` int(11) NOT NULL,
  3. `name` varchar(4) DEFAULT NULL,
  4. `age` int(11) DEFAULT NULL,
  5. PRIMARY KEY (`id`)
  6. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

查看表字段信息:desc user;
删除表:drop table user;
查看指定表创建语句:show create table user;
修改表名:alter table user rename to user2;
清空表:truncate table user;

操作表列基本命令

增加列:alter table user add (password char(40), address varchar(255));
删除列:alter table user drop address;
修改列:alter table user change password pass char(60);
修改列类型:alter table user modify pass char(32);

操作记录基本命令

插入记录1:INSERT INTOuser(id,name,age) VALUES ('1', '张三', '18');
插入记录2:INSERT INTOuser(id,name,age) VALUES ('2', '李四', '20');
删除记录:DELETE FROMuserWHERE (id='2');
修改记录:UPDATEuserSETage='30' WHERE (id='1');
查询记录:SELECT * FROMuser;

更多查询命令

查询指定列:select name,age from user;
重复的合并为一条记录:select distinct age from user;
列运算:select age+10 from user;
空替换为0参与计算:select IFNULL(age,0)+10 from user;
字符串拼接:select concat(name,age) from user;
别名:select concat(name,age) nameAge from user;
指定范围:select * from user limit 3,5;
指定条件:select * from user where age>18;
指定条件:select * from user where age between 6 and 50;
指定条件:select * from user where name='王丹' and age=7;
指定条件:select * from user where name in ('王丹','李浩');
指定条件:select * from user where name is null;
指定条件:select * from user where name is not null;
模糊查询:select * from user where name like '张_';
模糊查询:select * from user where name like '张%';
升序:select * from user order by age asc;
降序:select * from user order by age desc;
多种排序:select * from user order by age asc, name desc;
聚合函数:select count(*) 计数, sum(age) 总年龄, max(age) 最大年龄, min(age) 最小年龄, avg(age) 平均年龄 from user;
分组查询:select age,count(*) from user group by age;
合并结果集:select * from user1 UNION ALL select * from user2;
内连接方言:select u.name 姓名, f.name 文件名 from user u,file f where u.id=f.id;
内连接标准:select * from user u INNER JOIN file f ON u.id=f.id;
左外连接:select * from user u LEFT OUTER JOIN file f ON u.id=f.id;
右外连接:select * from user u RIGHT OUTER JOIN file f ON u.id=f.id;
子查询:select * from user u where id=(select id from file f where u.id=f.id);
子查询:select * from user u where id in (select id from file f where u.id=f.id);

备份和恢复

  1. 导出hello数据库中所有表:mysqldump -uroot -p hello > hello.sql
  2. 删除hello数据库里面的所有表
  3. 导入所有表到hello数据库中:mysql -uroot -p hello < hello.sql

image.png
另一种导入方式:
image.png

JDBC

  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.PreparedStatement;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. public class Demo {
  7. public static void main(String[] args) throws Exception {
  8. Class.forName("com.mysql.jdbc.Driver");
  9. Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/hello?useSSL=false", "root", "Aomei@1118");
  10. insert(conn);
  11. inserts(conn);
  12. update(conn);
  13. selects(conn);
  14. deletes(conn);
  15. conn.close();
  16. }
  17. // 增加1条
  18. private static void insert(Connection conn) throws SQLException {
  19. PreparedStatement ps = conn.prepareStatement("INSERT INTO user (id, name, age) VALUES (?,?,?)");
  20. ps.setInt(1, 1);
  21. ps.setString(2, "张三");
  22. ps.setInt(3, 18);
  23. System.out.println("增加:" + ps.executeUpdate());
  24. ps.close();
  25. }
  26. // 批量增加
  27. private static void inserts(Connection conn) throws SQLException {
  28. PreparedStatement ps = conn.prepareStatement("INSERT INTO user (id, name, age) VALUES (?,?,?)");
  29. for(int i=2; i<=9; i++) {
  30. ps.setInt(1, i);
  31. ps.setString(2, "张三"+i);
  32. ps.setInt(3, 20+i);
  33. ps.addBatch();
  34. }
  35. System.out.println("批量增加:" + ps.executeBatch().length);
  36. ps.close();
  37. }
  38. // 修改1条
  39. private static void update(Connection conn) throws SQLException {
  40. PreparedStatement ps = conn.prepareStatement("UPDATE user SET age=? WHERE id=?");
  41. ps.setInt(1, 30);
  42. ps.setInt(2, 1);
  43. System.out.println("修改:" + ps.executeUpdate());
  44. ps.close();
  45. }
  46. // 查询所有
  47. private static void selects(Connection conn) throws SQLException {
  48. PreparedStatement ps = conn.prepareStatement("SELECT * FROM user");
  49. ResultSet rs = ps.executeQuery();
  50. System.out.println("-------------------------------------");
  51. while(rs.next()) {
  52. System.out.println("ID:" + rs.getInt("id") + "\t姓名:" + rs.getString("name") + "\t年龄:" + rs.getInt("age"));
  53. }
  54. System.out.println("-------------------------------------");
  55. rs.close();
  56. ps.close();
  57. }
  58. // 删除所有
  59. private static void deletes(Connection conn) throws SQLException {
  60. PreparedStatement ps = conn.prepareStatement("DELETE FROM user");
  61. System.out.println("删除所有:" + ps.executeUpdate());
  62. ps.close();
  63. }
  64. }

事务

  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.PreparedStatement;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. public class Demo {
  7. public static void main(String[] args) throws Exception {
  8. Class.forName("com.mysql.jdbc.Driver");
  9. Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/hello?useSSL=false", "root", "Aomei@1118");
  10. try {
  11. conn.setAutoCommit(false);
  12. insert(conn);
  13. insert(conn);
  14. conn.commit();
  15. } catch (Exception e) {
  16. System.err.println(e.getMessage());
  17. conn.rollback();
  18. }
  19. selects(conn);
  20. conn.close();
  21. }
  22. // 增加1条
  23. private static void insert(Connection conn) throws SQLException {
  24. PreparedStatement ps = conn.prepareStatement("INSERT INTO user (id, name, age) VALUES (?,?,?)");
  25. ps.setInt(1, 1);
  26. ps.setString(2, "张三");
  27. ps.setInt(3, 18);
  28. System.out.println("增加:" + ps.executeUpdate());
  29. ps.close();
  30. }
  31. // 查询所有
  32. private static void selects(Connection conn) throws SQLException {
  33. PreparedStatement ps = conn.prepareStatement("SELECT * FROM user");
  34. ResultSet rs = ps.executeQuery();
  35. System.out.println("-------------------------------------");
  36. while(rs.next()) {
  37. System.out.println("ID:" + rs.getInt("id") + "\t姓名:" + rs.getString("name") + "\t年龄:" + rs.getInt("age"));
  38. }
  39. System.out.println("-------------------------------------");
  40. rs.close();
  41. ps.close();
  42. }
  43. }