微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

mysql的安装与DDL的使用

目录


安装MysqLyum包

[root@YL ~]# wget http://dev.MysqL.com/get/MysqL57-community-release-el7-11.noarch.rpm
--2022-07-25 14:37:26--  http://dev.MysqL.com/get/MysqL57-community-release-el7-11.noarch.rpm
Resolving dev.MysqL.com (dev.MysqL.com)... 23.10.6.175, 2600:140a:1000:28d::2e31, 2600:140a:1000:287::2e31
Connecting to dev.MysqL.com (dev.MysqL.com)|23.10.6.175|:80... connected.
HTTP request sent, awaiting response... 301 Moved Permanently
Location: https://dev.MysqL.com/get/MysqL57-community-release-el7-11.noarch.rpm [following] 
[root@YL ~]# rpm -Uvh MysqL57-community-release-el7-11.noarch.rpm 
warning: MysqL57-community-release-el7-11.noarch.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Verifying...                          ################################# [100%]
Preparing...                          ################################# [100%]
Updating / installing...
   1:MysqL57-community-release-el7-11 ################################# [100%]
禁用MysqL
[root@YL ~]# yum module disable MysqL
MysqL Connectors Community                    25 kB/s |  49 kB     00:01    
MysqL Tools Community                        342 kB/s | 651 kB     00:01    
MysqL 5.7 Community Server                   644 kB/s | 2.6 MB     00:04    
Dependencies resolved.
nothing to do.
Complete!
安装MysqL需要的服务端客户端命令和工具
[root@YL ~]# yum -y install MysqL-community-server MysqL-community-client  MysqL-community-common MysqL-community-devel  --nogpgcheck
Last Metadata expiration check: 0:00:28 ago on Mon 25 Jul 2022 03:00:03 PM CST.
Dependencies resolved.
===========================================================================
 Package                Arch   Version             Repository         Size
===========================================================================
Installing:
 MysqL-community-client x86_64 5.7.38-1.el7        MysqL57-community  28 M
 MysqL-community-common x86_64 5.7.38-1.el7        MysqL57-community 311 k
 MysqL-community-devel  x86_64 5.7.38-1.el7        MysqL57-community 4.2 M
 MysqL-community-server x86_64 5.7.38-1.el7        MysqL57-community 178 M
...........
  perl-macros-4:5.26.3-416.el8.x86_64                                      
  perl-parent-1:0.237-1.el8.noarch                                         
  perl-podlators-4.11-1.el8.noarch                                         
  perl-threads-1:2.21-2.el8.x86_64                                         
  perl-threads-shared-1.58-2.el8.x86_64   

设置MysqL的开机自启

[root@YL ~]# systemctl  enable --Now MysqLd
[root@YL ~]# systemctl  status MysqLd
● MysqLd.service - MysqL Server
   Loaded: loaded (/usr/lib/systemd/system/MysqLd.service; enabled; vendor>
   Active: active (running) since Mon 2022-07-25 15:03:53 CST; 6s ago
     Docs: man:MysqLd(8)
           http://dev.MysqL.com/doc/refman/en/using-systemd.html
  Process: 4325 ExecStart=/usr/sbin/MysqLd --daemonize --pid-file=/var/run>
  Process: 4276 ExecStartPre=/usr/bin/MysqLd_pre_systemd (code=exited, sta>
 Main PID: 4328 (MysqLd)
    Tasks: 27 (limit: 4743)
   Memory: 247.1M
   CGroup: /system.slice/MysqLd.service
           └─4328 /usr/sbin/MysqLd --daemonize --pid-file=/var/run/MysqLd/>

此时的3306端口已经开启了
[root@YL ~]# ss -antl
State   Recv-Q  Send-Q   Local Address:Port    Peer Address:Port  Process  
LISTEN  0       128            0.0.0.0:22           0.0.0.0:*              
LISTEN  0       128               [::]:22              [::]:*              
LISTEN  0       80                   *:3306               *:*      

在日志文件中找出临时密码,修改MysqL登陆密码

[root@YL ~]# grep "password" /var/log/MysqLd.log
2022-07-25T07:03:51.180440Z 1 [Note] A temporary password is generated for root@localhost: ?Rp,5kU6du;p

使用获取到的临时密码登录MysqL
[root@YL ~]# MysqL -uroot -p?Rp,5kU6du;p
修改MysqL登录密码
MysqL> set global validate_password_policy=0;
Query OK, 0 rows affected (0.01 sec)

MysqL> set global validate_password_length=1;
Query OK, 0 rows affected (0.00 sec)

MysqL> alter user 'root'@'localhost' identified by 'liuyang123!';
Query OK, 0 rows affected (0.00 sec)

MysqL> quit
Bye

为了防止MysqL自动更新为8.0版本,直接删除前面安装的yum源

[root@YL ~]# rpm -e MysqL57-community-release 

查看MysqL的当前版本

[root@YL ~]# MysqL -V
MysqL  Ver 14.14 distrib 5.7.38, for Linux (x86_64) using  EditLine wrapper

登录MysqL数据库

[root@YL ~]# MysqL -uroot -pliuyang123!
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 2
Server version: 5.7.38 MysqL Community Server (GPL)

copyright (c) 2000, 2022, Oracle and/or its affiliates.

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> 

不进入数据库查看他的内容

[root@YL ~]# MysqL -uroot -p -e 'show databases;'
Enter password: 
+--------------------+
| Database           |
+--------------------+
| information_schema |
| MysqL              |
| performance_schema |
| sys                |
+--------------------+

MysqL DDL的操作

MysqL> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| MysqL              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

创建一个名为liuyang的数据库
MysqL> create database if not exists liuyang;
Query OK, 1 row affected (0.01 sec)
查看数据库
MysqL> show databses;
ERROR 1064 (42000): You have an error in your sql Syntax; check the manual that corresponds to your MysqL server version for the right Syntax to use near 'databses' at line 1
MysqL> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| liuyang            |
| MysqL              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

删除liuyang这个数据库
MysqL> drop database if exists liuyang;
Query OK, 0 rows affected (0.01 sec)
MysqL> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| MysqL              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

表操作

MysqL> create table ly(id int not null,name varchar(20),age tinyint(2));
Query OK, 0 rows affected (0.01 sec)
查看表
MysqL> show tables;
+-------------------+
| Tables_in_liuyang |
+-------------------+
| ly                |
+-------------------+
1 row in set (0.00 sec)
MysqL> drop table if exists ly;
Query OK, 0 rows affected>  (0.00 sec)
MysqL> show tables;
Empty set (0.00 sec)


创建用户

MysqL>  CREATE USER 'liuyang'@'127.0.0.1' IDENTIFIED BY 'liuyang123!';
Query OK, 0 rows affected (0.00 sec)
[root@YL ~]# MysqL -uliuyang -pliuyang123! -h 127.0.0.1
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 7
Server version: 5.7.38 MysqL Community Server (GPL)

copyright (c) 2000, 2022, Oracle and/or its affiliates.

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> drop user 'liuyang'@'127.0.0.1';
Query OK, 0 rows affected (0.00 sec)

查看支持的所有字符集

MysqL> show character set;
+----------+---------------------------------+---------------------+--------+
| Charset  | Description                     | Default collation   | Maxlen |
+----------+---------------------------------+---------------------+--------+
| big5     | Big5 Traditional Chinese        | big5_chinese_ci     |      2 |
| dec8     | DEC West European               | dec8_swedish_ci     |      1 |
| cp850    | DOS West European               | cp850_general_ci    |      1 |
| hp8      | HP West European                | hp8_english_ci      |      1 |
...........
| gb18030  | China National Standard GB18030 | gb18030_chinese_ci  |      4 |
+----------+---------------------------------+---------------------+--------+
41 rows in set (0.00 sec)
查看当前数据库支持的所有存储引擎
MysqL> show engines;
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine             | Support | Comment                                                        | Transactions | XA   | Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| InnoDB             | DEFAULT | Supports transactions, row-level locking, and foreign keys     | YES          | YES  | YES        |
| MRG_MYISAM         | YES     | Collection of identical MyISAM tables                          | NO           | NO   | NO         |
| MEMORY             | YES     | Hash based, stored in memory, useful for temporary tables      | NO           | NO   | NO         |
| BLACKHOLE          | YES     | /dev/null storage engine (anything you write to it disappears) | NO           | NO   | NO         |
| MyISAM             | YES     | MyISAM storage engine                                          | NO           | NO   | NO         |
| CSV                | YES     | CSV storage engine                                             | NO           | NO   | NO         |
| ARCHIVE            | YES     | Archive storage engine                                         | NO           | NO   | NO         |
| PERFORMANCE_SCHEMA | YES     | Performance Schema                                             | NO           | NO   | NO         |
| FEDERATED          | NO      | Federated MysqL storage engine                                 | NULL         | NULL | NULL       |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
9 rows in set (0.00 sec)
查看数据库信息
MysqL> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| MysqL              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)
不进入某数据库而列出其包含的所有表
MysqL> show tables from liu;
+---------------+
| Tables_in_liu |
+---------------+
| liuyang       |
+---------------+
1 row in set (0.00 sec)
查看表结构
MysqL> desc liu.liuyang;
+-------+--------------+------+-----+---------+-------+
| Field | Type         | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id    | int(11)      | NO   |     | NULL    |       |
| name  | varchar(100) | NO   |     | NULL    |       |
| age   | tinyint(4)   | YES  |     | NULL    |       |
+-------+--------------+------+-----+---------+-------+
3 rows in set (0.01 sec)
查看某表的创建命令
MysqL> show create table liu.liuyang;
+---------+----------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table   | Create Table                                                                                                                                             |
+---------+----------------------------------------------------------------------------------------------------------------------------------------------------------+
| liuyang | CREATE TABLE `liuyang` (
  `id` int(11) NOT NULL,
  `name` varchar(100) NOT NULL,
  `age` tinyint(4) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+---------+----------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
查看某表的状态
MysqL> show table status like 'liuyang'\G
*************************** 1. row ***************************
           Name: liuyang
         Engine: InnoDB
        Version: 10
     Row_format: Dynamic
           Rows: 0
 Avg_row_length: 0
    Data_length: 16384
Max_data_length: 0
   Index_length: 0
      Data_free: 0
 Auto_increment: NULL
    Create_time: 2022-07-25 21:28:00
    Update_time: NULL
     Check_time: NULL
      Collation: latin1_swedish_ci
       Checksum: NULL
 Create_options: 
        Comment: 
1 row in set (0.00 sec)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐