2019.7.22笔记整理
Day1-mysql
zhenghan@zhenghan-host:/$ ip a
1: lo: mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:5f:fa:2f brd ff:ff:ff:ff:ff:ff
inet 10.36.174.64/24 brd 10.36.174.255 scope global dynamic ens33
valid_lft 590sec preferred_lft 590sec
inet6 fe80::9c1b:1aee:b318:874d/64 scope link
valid_lft forever preferred_lft forever
zhenghan@zhenghan-host:/$ which mysql
/usr/bin/mysql
zhenghan@zhenghan-host:/$ sudo service mysql status
[sudo] zhenghan 的密码:
● mysql.service - MySQL Community Server
Loaded: loaded (/lib/systemd/system/mysql.service; enabled; ven
Active: active (running) since 一 2019-07-22 12:58:25 CST; 6h a
Process: 7486 ExecStartPost=/usr/share/mysql/mysql-systemd-start
Process: 7479 ExecStartPre=/usr/share/mysql/mysql-systemd-start
Main PID: 7485 (mysqld)
CGroup: /system.slice/mysql.service
└─7485 /usr/sbin/mysqld
7月 22 12:58:23 zhenghan-host systemd[1]: Stopped MySQL Community
7月 22 12:58:23 zhenghan-host systemd[1]: Starting MySQL Community
7月 22 12:58:25 zhenghan-host systemd[1]: Started MySQL Community
zhenghan@zhenghan-host:/$ sudo apt install mysql-server -y
正在读取软件包列表... 完成
正在分析软件包的依赖关系树
正在读取状态信息... 完成
mysql-server 已经是最新版 (5.7.26-0ubuntu0.16.04.1)。
升级了 0 个软件包,新安装了 0 个软件包,要卸载 0 个软件包,有 204 个软件包未被升级。
命令总结:
ip a:查看你的网段地址
which mysql:查看你的mysql在哪里
sudo apt install mysql-server -y:apt 安装mysql server
cd /etc/mysql/ :进入到mysql
sudo cat debian.cnf:查看mysql上的所有信息(包括用户名和密码等)
mysql -u debian-sys-maint -pxxx:xxx为密码,这句命令含义为连接数据库。使用user为debian-sys-maint,口令为密码。
zhenghan@zhenghan-host:/$ cd /etc/mysql/
zhenghan@zhenghan-host:/etc/mysql$ sudo cat debian.cnf
# Automatically generated for Debian scripts. DO NOT TOUCH!
[client]
host = localhost
user = debian-sys-maint
password = 6nx8fzbE7rgW2By3
socket = /var/run/mysqld/mysqld.sock
[mysql_upgrade]
host = localhost
user = debian-sys-maint
password = 6nx8fzbE7rgW2By3
socket = /var/run/mysqld/mysqld.sock
zhenghan@zhenghan-host:/etc/mysql$ mysql -u debian-sys-maint -p6nx8fzbE7rgW2By3
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 22
Server version: 5.7.26-0ubuntu0.16.04.1 (Ubuntu)
Copyright (c) 2000, 2019, 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>
mysql-server服务进程:
DB里面包括好多库:
(1)datebase,库,查看库可用指令show databases
(2)information_schema
(3)mysql,这个也是库,这里面包括表table,视图view,函数function,索引index,其中查看表table的指令是show tables,表table里面有一个user表,user表示用户表。
(4)sys
mysql的客户端工具:mysql -u xx -pxxx这句命令连接数据库
客户端与服务端通过socket连接。
命令总结:
show databases:显示所有的数据库
show tables:显示数据表
use mysql:进入到并打开mysql数据库
命令总结:
mysql>select user,host,authentication_string from user;查看当前DB系统中所有的用户信息。
mysql>update user set
> host='%', authentication_string=password('root')
> where user='root';修改root和host命令
mysql> grant all privileges on *.* to 'root'@'%'
> identified by 'root';将所有的库下所有对象的权限都授予root用户,可以被远程连接
mysql> flush privileges;刷新权限
mysql> exit 退出mysql连接
> sudo service mysql restart 重启mysql服务器
mysql -uroot -proot 使用root用户连接
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select user,host,authentication_string from user;
+------------------+-----------+-------------------------------------------+
| user | host | authentication_string |
+------------------+-----------+-------------------------------------------+
| root | % | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
| mysql.session | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| mysql.sys | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| debian-sys-maint | localhost | *32A7870CDCF13E51FDF7575497438FC817790A2B |
+------------------+-----------+-------------------------------------------+
4 rows in set (0.05 sec)
mysql>
创建库的命令:
CREATE DATABASE db_name [default character set utf8;
如:create database fruit;
show create database fruit;查看创建数据库fruit
drop database fruit;删除数据库
创建表的语法:
create table table_name(
field_name field_type[(length)] [约束],
field_name2 ...,
) [ engine=InnoDB|MyISAM] [DEFAULT CHARSET=utf8 ]
如:
create table cource(
id integer primary key auto_increment,
name varchar(20) unique,
teacher varchar(20) not null
)
修改字段的语法:
alter table table_name modify 字段名 类型[(长度)] [约束];
如,修改score表的score字段的类型为int, 默认值为60
alter table score modify score int default 60;
修改表名:
alter table table_name rename new_table_name;