MySql基础部署以及基本使用(用于个人学习与回顾)

发布时间:2020-07-15 09:02:53 作者:xwj16888812
阅读:290
mysql云数据库,弹性扩容,低至0.3元/天! 查看>>

MySQL数据库介绍

        -最早隶属于瑞典的MySQL AB公司

        -2008年1月,MySQL AB被SUN收购

        -2009年4月,SUN被Oracle收购

MySQL的特点及应用

         -使用C和C++编写,可移植性强

         -通过API支持Python/JAVA/Perl/PHP等语言

Mysql安装

[root@proxy ~]# systemctl stop mariadb
[root@proxy ~]# rm -rf /etc/my.cnf
[root@proxy ~]# rm -rf /var/lib/mysql/*
[root@proxy ~]# rpm -e --nodeps mariadb-server mariadb
警告:/var/log/mariadb/mariadb.log 已另存为 /var/log/mariadb/mariadb.log.rpmsave

       -采用-U升级安装,可替换冲突文件

       - 推荐将devel安装,用于支持其他软件

[root@proxy ~]# yum -y install perl-Data-Dumper perl-JSON perl-Time-HiRes
[root@proxy ~]# tar -xf mysql-5.7.17-1.el7.x86_64.rpm-bundle.tar 
[root@proxy ~]# rm -f mysql-community-server-minimal-5.7.17-1.el7.x86_64.rpm 
[root@proxy ~]# rpm -Uvh mysql-community-*.rpm
[root@localhost ~]# systemctl start mysqld                  
[root@localhost ~]# systemctl enable mysqld                 
[root@localhost ~]# systemctl status mysqld

MySQL初始配置

       -root,允许从localhost访问

       -首次登录密码在安装时随机生成

       -存储在错误日志文件中

[root@proxy ~]# grep 'temporary password' /var/log/mysqld.log
2019-06-24T15:19:18.303935Z 1 [Note] A temporary password is generated for root@localhost: zzXdihIzU4-_
[root@proxy ~]# mysql -uroot -p'zzXdihIzU4-_'
mysql> set global validate_password_policy=0;     //只验证长度
Query OK, 0 rows affected (0.00 sec)
mysql> set global validate_password_length=6;     //修改密码长度,默认值是8个字符
Query OK, 0 rows affected (0.00 sec)
mysql> alter user user() identified by "123456";  //修改登录密码
Query OK, 0 rows affected (0.00 sec)
[root@proxy ~]# mysql -uroot -p123456
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 3Server version: 5.7.17 MySQL Community Server (GPL)
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>

MySQL服务相关参数

文件
说明
/etc/my.cnf主配置文件
/var/lib/mysql数据库目录
默认端口号3306
进程号mysqld
传输协议TCP
进程所有者mysql
进程所属组mysql

数据库基本管理

      -Use 库名;                        //切换库

      -Select database();            //显示当前所在的库

      -Create database 库名;   //创建新库

      -Show tables;                   //显示已有的库

      -Drop database 库名;    //删除库

      -Desc 表名;                   //查看表结构

      -Select * from 表名;     // 查看表记录

      -Drop table 表名;        //删除表

       -添加新字段

  1.       ALTER TABLE 表名

  2.       ADD 字段名 类型(宽度) 约束条件;

  3.      可加 AFTER 字段名;

           或者FIRST;

mysql> desc tt1;
+-------+------------+------+-----+---------+-------+
| Field | Type       | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| id    | int(3)     | NO   | PRI | NULL    |       |
| name  | varchar(5) | NO   |     | NULL    |       |
| age   | int(3)     | NO   |     | NULL    |       |
+-------+------------+------+-----+---------+-------+
3 rows in set (0.01 sec)

mysql> alter table tt1 add interest varchar(40);
Query OK, 0 rows affected (0.08 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc tt1;
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id       | int(3)      | NO   | PRI | NULL    |       |
| name     | varchar(5)  | NO   |     | NULL    |       |
| age      | int(3)      | NO   |     | NULL    |       |
| interest | varchar(40) | YES  |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

         -修改字段类型

             alter table 表名

             modify 字段名 类型(宽度)约束条件;

             可加 after 字段名;

             或者 first ;

mysql> desc tt1;
+----------+--------------------+------+-----+---------+-------+
| Field    | Type               | Null | Key | Default | Extra |
+----------+--------------------+------+-----+---------+-------+
| id       | int(3)             | NO   | PRI | NULL    |       |
| name     | varchar(5)         | NO   |     | NULL    |       |
| age      | int(3)             | NO   |     | NULL    |       |
| gender   | enum('boy','girl') | NO   |     | NULL    |       |
| interest | varchar(40)        | YES  |     | NULL    |       |
+----------+--------------------+------+-----+---------+-------+
5 rows in set (0.00 sec)

mysql> alter table tt1 modify name char(6) not null;
Query OK, 0 rows affected (0.34 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc tt1;
+----------+--------------------+------+-----+---------+-------+
| Field    | Type               | Null | Key | Default | Extra |
+----------+--------------------+------+-----+---------+-------+
| id       | int(3)             | NO   | PRI | NULL    |       |
| name     | char(6)            | NO   |     | NULL    |       |
| age      | int(3)             | NO   |     | NULL    |       |
| gender   | enum('boy','girl') | NO   |     | NULL    |       |
| interest | varchar(40)        | YES  |     | NULL    |       |
+----------+--------------------+------+-----+---------+-------+
5 rows in set (0.00 sec)

         -修改字段名

             alter table 表名

             change 源字段名 新字段名 类型(宽度) 约束条件;

mysql> desc tt1;
+----------+--------------------+------+-----+---------+-------+
| Field    | Type               | Null | Key | Default | Extra |
+----------+--------------------+------+-----+---------+-------+
| id       | int(3)             | NO   | PRI | NULL    |       |
| name     | varchar(5)         | NO   |     | NULL    |       |
| age      | int(3)             | NO   |     | NULL    |       |
| sex      | enum('boy','girl') | YES  |     | NULL    |       |
| interest | varchar(40)        | YES  |     | NULL    |       |
+----------+--------------------+------+-----+---------+-------+
5 rows in set (0.00 sec)

mysql> alter table tt1 change sex gender enum('boy','girl') not null;
Query OK, 0 rows affected (0.33 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc tt1;
+----------+--------------------+------+-----+---------+-------+
| Field    | Type               | Null | Key | Default | Extra |
+----------+--------------------+------+-----+---------+-------+
| id       | int(3)             | NO   | PRI | NULL    |       |
| name     | varchar(5)         | NO   |     | NULL    |       |
| age      | int(3)             | NO   |     | NULL    |       |
| gender   | enum('boy','girl') | NO   |     | NULL    |       |
| interest | varchar(40)        | YES  |     | NULL    |       |
+----------+--------------------+------+-----+---------+-------+
5 rows in set (0.00 sec)

         -删除字段

             alter table 表名

             drop 字段名;

mysql> desc tt1;
+----------+--------------------+------+-----+---------+-------+
| Field    | Type               | Null | Key | Default | Extra |
+----------+--------------------+------+-----+---------+-------+
| id       | int(3)             | NO   | PRI | NULL    |       |
| name     | char(6)            | NO   |     | NULL    |       |
| age      | int(3)             | NO   |     | NULL    |       |
| gender   | enum('boy','girl') | NO   |     | NULL    |       |
| interest | varchar(40)        | YES  |     | NULL    |       |
+----------+--------------------+------+-----+---------+-------+
5 rows in set (0.00 sec)

mysql> alter table tt1 drop gender;
Query OK, 0 rows affected (0.35 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc tt1;
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id       | int(3)      | NO   | PRI | NULL    |       |
| name     | char(6)     | NO   |     | NULL    |       |
| age      | int(3)      | NO   |     | NULL    |       |
| interest | varchar(40) | YES  |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

         -修改表名

              alter table 表名

              rename 新表名;

mysql> alter table tt1 rename tt2;
Query OK, 0 rows affected (0.31 sec)
mysql> desc tt1;
ERROR 1146 (42S02): Table 'studb.tt1' doesn't exist
mysql> desc tt2;
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id       | int(3)      | NO   | PRI | NULL    |       |
| name     | char(6)     | NO   |     | NULL    |       |
| age      | int(3)      | NO   |     | NULL    |       |
| interest | varchar(40) | YES  |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

时间函数

类型用途
now()获取系统当前日期和时间
year()执行时动态获得系统日期时间
sleep()休眠N秒
curdate()获取当前的系统日期
curtime()获取当前的系统时刻
month()

获取指定时间中的月份

date()获取指定时间中的日期
time()获取指定时间中的时刻
mysql> select now(),sysdate(),curdate();
+---------------------+---------------------+------------+
| now()               | sysdate()           | curdate()  |
+---------------------+---------------------+------------+
| 2019-06-25 22:10:45 | 2019-06-25 22:10:45 | 2019-06-25 |
+---------------------+---------------------+------------+
1 row in set (0.00 sec)
mysql> select date(now()),time(now());
+-------------+-------------+
| date(now()) | time(now()) |
+-------------+-------------+
| 2019-06-25  | 22:11:41    |
+-------------+-------------+
1 row in set (0.00 sec)

亿速云「云数据库 MySQL」免部署即开即用,比自行安装部署数据库高出1倍以上的性能,双节点冗余防止单节点故障,数据自动定期备份随时恢复。点击查看>>

推荐阅读:
  1. MySQL数据导入导出(用于个人学习与回顾)
  2. Mysql索引概述(用于个人学习与回顾)

开发者交流群:

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

linux mysql

上一篇:tomcat 登录时用户名和密码的查看及设置

下一篇:短域名原理

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》
开发者交流群×