时间:2021-07-01 10:21:17 帮助过:18人阅读
sudo apt-get install mysql-server mysql-client
然后按照提示输入
启动
service mysql start
停止
service mysql stop
重启
service mysql restart
#找到mysql配置文件并修改 sudo vi /etc/mysql/mysql.conf.d/mysqld.cnf 将bind-address=127.0.0.1注释 #登录mysql,运行命令 grant all privileges on *.* to ‘root‘@‘%‘ identified by ‘mysql‘ with grant option; flush privileges; #重启mysql
python@ubuntu:~$ mysql --help #查看命令 python@ubuntu:~$ mysql -uroot -pmysql #连接
#查看版本 mysql> select version(); +-------------------------+ | version() | +-------------------------+ | 5.7.20-0ubuntu0.16.04.1 | +-------------------------+ 1 row in set (0.00 sec) 查看版本:select version(); 显示当前时间:select now();
mysql -hip地址 -uroot -p
#创建数据库 create database 数据库名 charset=utf8; #删除数据库 drop database 数据库名; #切换数据库 use 数据库名; #查看当前选择的数据库 select database();
auto_increment表示自动增长
create table 表名(列及类型);
mysql> create table school( -> id int auto_increment primary key not null, -> name varchar(10) not null, -> gender bit default 1, -> birthday datetime);
修改表
alter table 表名 add|change|drop 列名 类型;
如:
mysql> alter table school add idDelete bit default 0;
删除表 drop table 表名; 查看表结构 desc 表名; 更改表名称 rename table 原表名 to 新表名; 查看表的创建语句 show create table 表名;
mysql> show create table school; -----+ | school | CREATE TABLE `school` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(10) NOT NULL, `gender` bit(1) DEFAULT b‘1‘, `birthday` datetime DEFAULT NULL, `idDelete` bit(1) DEFAULT b‘0‘, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 | +--------+-----------------------------
select * from 表名
1.全列插入:insert into 表名 values(...) mysql> insert into students values(0,"腾旭",1,"1999-9-9",0); +----+--------+--------+---------------------+----------+ | id | name | gender | birthday | isDelete | +----+--------+--------+---------------------+----------+ | 1 | 腾旭 | | 1999-09-09 00:00:00 | |
2.缺省插入:insert into 表名(列1,...) values(值1,...) mysql> insert into students(name) values(‘网易‘);
mysql> insert into students(gender,name) values(1,‘小米‘);
3.mysql 特有的
同时插入多条数据:insert into 表名 values(...),(...)...;
或insert into 表名(列1,...) values(值1,...),(值1,...)...;
mysql> insert into students(name) values(‘百度‘),(‘酷狗‘),(‘QQ‘); +----+--------+--------+---------------------+----------+ | id | name | gender | birthday | isDelete | | 5 | 百度 | | NULL | | | 6 | 酷狗 | | NULL | | | 7 | QQ | | NULL | | +----+--------+--------+---------------------+----------+
update 表名 set 列1=值1,... where 条件
mysql> update students set birthday = ‘1990-2-2‘ where id=2; mysql> update students set gender=0,birthday=‘2017-2-13‘ where id=6;
mysql> delete from students where id=5;
alter table students add isdelete bit default 0;
如果需要删除则
update students isdelete=1 where ...;
mysql> update students set isDelete=1 where id=6; mysql> select * from students where isDelete=0;
#进入超级管理员 sudo -s #进入mysql库目录 cd /var/lib/mysql #运行mysqldump命令 mysqldump –uroot –p 数据库名 > ~/Desktop/备份文件.sql; 按提示输入mysql的密码
root@ubuntu:/var/lib/mysql# mysqldump -uroot -p python3 > ~/Desktop/bak.sql python@ubuntu:~/Desktop$ vim bak.sql 一系列的sql语句
连接mysqk,创建数据库 退出连接,执行如下命令 mysql -uroot –p 数据库名 < ~/Desktop/备份文件.sql 根据提示输入mysql密码
mysql> create database py31 charset=utf8;
python@ubuntu:~/Desktop$ mysql -uroot -p py31 < bak.sql
2 数据库安装应用
标签:service sql 需要 lte 远程连接mysql sele update gen ima