PHP全栈开发(二):MYSQL学习

昨天进行了CentOS 7 下面的 LAMP搭建

今天学习一下MySQL的一些入门级操作

主要参考PHP中文网的“MySQL最新手册教程”

http://www.php.cn/mysql/mysql-administration.html

 

首先可以看下mysql服务是否已经启动了

[root@localhost ~]# ss -tl
State      Recv-Q Send-Q Local Address:Port                 Peer Address:Port                
LISTEN     0      50         *:mysql                    *:*                    
LISTEN     0      128        *:ssh                      *:*                    
LISTEN     0      100    127.0.0.1:smtp                     *:*                    
LISTEN     0      128       :::http                    :::*                    
LISTEN     0      128       :::ssh                     :::*                    
LISTEN     0      100      ::1:smtp                    :::*

如果没有启动的话,使用如下命令进行启动:

systemctl start mariadb.service

当然也可以使用如下命令进行关闭:

systemctl stop mariabd.service

如果关闭了的话可以看到在TCP状态里面是看不到mysql的

[root@localhost ~]# ss -tl
State      Recv-Q Send-Q Local Address:Port                 Peer Address:Port                
LISTEN     0      128         *:ssh                       *:*                    
LISTEN     0      100    127.0.0.1:smtp                      *:*                    
LISTEN     0      128        :::http                     :::*                    
LISTEN     0      128        :::ssh                      :::*                    
LISTEN     0      100       ::1:smtp                     :::*                    

 

好,昨天我们已经讲到初次进入mysql是不需要密码的,直接输入mysql就能进入了。

于是我们首先要给mysql设置一个root的密码,怎么设置呢,命令如下:

mysqladmin -u root password beijing2007;

然后我们再次进入的时候就需要输入密码了,如果这时候我们还想像以前一样只输入mysql命令的话,是不能进入的:

[root@localhost ~]# mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

我们需要按照如下的命令来进入:

[root@localhost ~]# mysql -u root -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 5.5.56-MariaDB MariaDB Server

Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

 

好了,设置好了root账号的密码之后,我们介绍mysql的几个简单的入门命令:

MariaDB [(none)]> show databases; #查看数据库
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)

可以看到现在我们的mysql里面有以上四个数据库,我们可以选择一个数据库,然后看看里面有什么数据表。

MariaDB [test]> use mysql;     #选择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
MariaDB [mysql]> show tables; #查看mysql数据库中的数据表
+---------------------------+
| Tables_in_mysql           |
+---------------------------+
| columns_priv              |
| db                        |
| event                     |
| func                      |
| general_log               |
| help_category             |
| help_keyword              |
| help_relation             |
| help_topic                |
| host                      |
| ndb_binlog_index          |
| plugin                    |
| proc                      |
| procs_priv                |
| proxies_priv              |
| servers                   |
| slow_log                  |
| tables_priv               |
| time_zone                 |
| time_zone_leap_second     |
| time_zone_name            |
| time_zone_transition      |
| time_zone_transition_type |
| user                      |
+---------------------------+
24 rows in set (0.00 sec)

可以看到有非常多的数据表,我们挑选一个user数据表,来看下这个表中有哪些列:

MariaDB [mysql]> show columns from user;
+------------------------+-----------------------------------+------+-----+---------+-------+
| Field                  | Type                              | Null | Key | Default | Extra |
+------------------------+-----------------------------------+------+-----+---------+-------+
| Host                   | char(60)                          | NO   | PRI |         |       |
| User                   | char(16)                          | NO   | PRI |         |       |
| Password               | char(41)                          | NO   |     |         |       |
| Select_priv            | enum('N','Y')                     | NO   |     | N       |       |
| Insert_priv            | enum('N','Y')                     | NO   |     | N       |       |
| Update_priv            | enum('N','Y')                     | NO   |     | N       |       |
| Delete_priv            | enum('N','Y')                     | NO   |     | N       |       |
| Create_priv            | enum('N','Y')                     | NO   |     | N       |       |
| Drop_priv              | enum('N','Y')                     | NO   |     | N       |       |
| Reload_priv            | enum('N','Y')                     | NO   |     | N       |       |
| Shutdown_priv          | enum('N','Y')                     | NO   |     | N       |       |
| Process_priv           | enum('N','Y')                     | NO   |     | N       |       |
| File_priv              | enum('N','Y')                     | NO   |     | N       |       |
| Grant_priv             | enum('N','Y')                     | NO   |     | N       |       |
| References_priv        | enum('N','Y')                     | NO   |     | N       |       |
| Index_priv             | enum('N','Y')                     | NO   |     | N       |       |
| Alter_priv             | enum('N','Y')                     | NO   |     | N       |       |
| Show_db_priv           | enum('N','Y')                     | NO   |     | N       |       |
| Super_priv             | enum('N','Y')                     | NO   |     | N       |       |
| Create_tmp_table_priv  | enum('N','Y')                     | NO   |     | N       |       |
| Lock_tables_priv       | enum('N','Y')                     | NO   |     | N       |       |
| Execute_priv           | enum('N','Y')                     | NO   |     | N       |       |
| Repl_slave_priv        | enum('N','Y')                     | NO   |     | N       |       |
| Repl_client_priv       | enum('N','Y')                     | NO   |     | N       |       |
| Create_view_priv       | enum('N','Y')                     | NO   |     | N       |       |
| Show_view_priv         | enum('N','Y')                     | NO   |     | N       |       |
| Create_routine_priv    | enum('N','Y')                     | NO   |     | N       |       |
| Alter_routine_priv     | enum('N','Y')                     | NO   |     | N       |       |
| Create_user_priv       | enum('N','Y')                     | NO   |     | N       |       |
| Event_priv             | enum('N','Y')                     | NO   |     | N       |       |
| Trigger_priv           | enum('N','Y')                     | NO   |     | N       |       |
| Create_tablespace_priv | enum('N','Y')                     | NO   |     | N       |       |
| ssl_type               | enum('','ANY','X509','SPECIFIED') | NO   |     |         |       |
| ssl_cipher             | blob                              | NO   |     | NULL    |       |
| x509_issuer            | blob                              | NO   |     | NULL    |       |
| x509_subject           | blob                              | NO   |     | NULL    |       |
| max_questions          | int(11) unsigned                  | NO   |     | 0       |       |
| max_updates            | int(11) unsigned                  | NO   |     | 0       |       |
| max_connections        | int(11) unsigned                  | NO   |     | 0       |       |
| max_user_connections   | int(11)                           | NO   |     | 0       |       |
| plugin                 | char(64)                          | NO   |     |         |       |
| authentication_string  | text                              | NO   |     | NULL    |       |
+------------------------+-----------------------------------+------+-----+---------+-------+
42 rows in set (0.00 sec)

 

可以看到user这个表里面有host,user,password这些列,并且这些列的类型都显示出来了,除此之外还有其他很多的列。

可以用SQL语句查一下user这个表里面的root用户的数据:

MariaDB [mysql]> select host, user, password from user where user = 'root';
+-----------------------+------+-------------------------------------------+
| host                  | user | password                                  |
+-----------------------+------+-------------------------------------------+
| localhost             | root | *627B6CF6E29B501BF1FF770A61EDBB7C59A399B2 |
| localhost.localdomain | root |                                           |
| 127.0.0.1             | root |                                           |
| ::1                   | root |                                           |
+-----------------------+------+-------------------------------------------+
4 rows in set (0.00 sec)

当然我们需要使用mysql的时候也不能老是使用root账户来进行操作,我们需要在数据表user里面插入一个新的用户。

MariaDB [mysql]> insert into user (host, user, password, select_priv, insert_priv, update_priv) values ('localhost', 'php', password('php123'), 'Y', 'Y', 'Y');
Query OK, 1 row affected, 4 warnings (0.00 sec)

使用Insert语句在user这个表里面插入一个Php的用户,当然了在插入命令之前一定要使用use语句来选择数据库,不然谁知道你插到哪里去了呢。

还有在values函数里面的密码一定要使用Password函数括起来,不然的话再Mysql里面显示的是明文,而且输入密码也不对的。

插入了数据之后我们还需要让这些数据生效,使用如下命令:

MariaDB [mysql]> flush privileges;
Query OK, 0 rows affected (0.00 sec)

然后现在我们可以使用Php这个账号进行登录mysql的操作了

mysql -u php -p

 在命令行创建数据表:

CREATE TABLE baoxiu_list(
id int(11) not null primary key auto_increment,
xiaoqu varchar(255) NOT NULL,
bd varchar(255) DEFAULT NULL,
room varchar(255) DEFAULT NULL,
name varchar(255) DEFAULT NULL,
userid varchar(255) DEFAULT NULL,
contact varchar(255) DEFAULT NULL,
tiandan varchar(255) DEFAULT NULL,
remark varchar(255) DEFAULT NULL,
status varchar(255) DEFAULT NULL,
jiedan varchar(255) DEFAULT NULL,
jungong varchar(255) DEFAULT NULL)
ENGINE=InnoDB DEFAULT CHARSET=utf8;

 查看数据库编码格式:

show variables like 'character_set_database';

创建数据库时指定数据库的字符集

mysql>create database <数据库名> character set utf8;

创建数据表时指定数据表的编码格式

create table tb_books (
    name varchar(45) not null,
    price double not null,
    bookCount int not null,
    author varchar(45) not null ) default charset = utf8;

修改数据库的编码格式

mysql>alter database <数据库名> character set utf8;

修改数据表格编码格式

mysql>alter table <表名> character set utf8;

 删除数据表中的某一列

mysql> delete from student where id=1;

显示数据表中的所有数据

select * from <数据表名>

按行显示数据表中的所有数据

select * from <数据表名> \G

 在数据表中新增列

alter table tablename add column columnname varchar(20) not null after somecolumn;

查看表结构

desc table_name;

改变列名,列属性

alter table tbl_name change old_col_name new_col_name data_type not null auto_increment primary key

 

posted @ 2018-08-03 11:30  为往圣继绝学2333  阅读(156)  评论(0编辑  收藏  举报