zoukankan      html  css  js  c++  java
  • 菜鸟的《Linux程序设计》学习——MySQL数据库安装、配置及基本操作

    1. MySQL数据库:

    在涉及到一些大型的Web系统或者嵌入式软件的开发时,都少不了用数据库来管理数据。在Windows操作系统下,使用过各种各样的数据库,如:sqlServer、Oracle、MySQL等等,我们知道,在Windows系统上的软件安装配置都是界面式,操作明显而简捷。
    那么在Linux操作系统上,对于数据库的选用,好像首推的是MySQL,它是一个关系型数据库管理系统,由瑞典MySQL AB公司开发,目前属于Oracle公司。MySQL是一种关联数据库管理系统,关联数据库将数据保存在不同的表中,而不是将所有数据放在一个大仓库内,这样就增加了速度并提高了灵活性。MySQL的SQL语言是用于访问数据库的最常用标准化语言。MySQL软件采用了双授权政策(本词条“授权政策”),它分为社区版和商业版,由于其体积小、速度快、总体拥有成本低,尤其是开放源码这一特点,一般中小型网站的开发都选择MySQL作为网站数据库。由于其社区版的性能卓越,搭配PHP和Apache可组成良好的开发环境。
    今天,阅读了《Linux程序设计》的MySQL这一章节,根据自己在虚拟机中安装配置的步骤,记录于此,供于以后的学习回顾。

    2. MySQL安装步骤:

    2.1 查看自己系统中是否已经存在

    因为mysql数据库在Linux上实在是太流行了,所以目前下载的主流Linux系统版本基本上都集成了mysql数据库在里面,我们可以通过如下命令来查看我们的操作系统上是否已经安装了mysql数据库:
    [root@localhost ~]# rpm -qa | grep mysql

    我们使用root账户登录系统,由于我的系统上已经安装MySQL ,所以得到系统中已安装版本信息:

    若是未安装过,则会出现没有安装的提示,此时即可跳过步骤2的卸载。

    2.2 卸载原有的MySQL

    使用下面命令,卸载现有MySQL:
    [root@localhost ~]# rpm -e --nodeps mysql
    
    此时,系统中已经不存在了MySQL的安装记录。

    2.3 yum方式安装MySQL

    通过yum的方式来进行mysql的数据库安装,首先我们可以输入 yum list | grep mysql 命令来查看yum上提供的mysql数据库可下载的版本:

    [root@localhost ~]#yum list | grep mysql

    我们可以看到yum服务器上有很多MySQL的可下载版本:


    然后,我们通过输入 yum install -y mysql-server mysql mysql-devel 命令将mysql mysql-server mysql-devel都安装
    [root@localhost ~]# yum install -y mysql-server mysql mysql-devel
    

    几秒钟时间过后,我们可以看到下面的信息:


    当我们看到complete这个赏心悦目的单词时,说明我们已经成功的安装了MySQL数据库。

    3. MySQL数据库配置

    3.1 MySQL服务设置

    在安装完mysql数据库以后,会发现会多出一个mysqld的服务,这个就是咱们的数据库服务,我们通过输入 service mysqld start 命令就可以启动我们的mysql服务。

    一般我们将该数据库的服务设置为开机自启动,使用命令chkconfig mysqld on来设置,同时使用命令chkconfig --list | grep mysql来查看结果:
    [root@localhost ~]# chkconfig mysqld on
    [root@localhost ~]# chkconfig --list | grep mysql
    mysqld         	0:off	1:off	2:on	3:on	4:on	5:on	6:of

    3.2 账户密码设置

    在我们安装完MySQL数据库后,就默认有一个名称为root的管理员账户,此时,需要手动设置密码:
    使用命令mysqladmin -u root password 'root' 
    设定密码之后,我们可以通过命令mysql -u root -p来登录数据库:

    至此,对于MySQL数据库配置,就可以结束了。

    4. MySQL基本操作

    4.1 基本配置文件

    (1)在/etc/my.cnf内存储了MySQL数据库的主要配置信息,查看:
    [root@localhost ~]# cat /etc/my.cnf
    [mysqld]
    datadir=/var/lib/mysql
    socket=/var/lib/mysql/mysql.sock
    user=mysql
    # Disabling symbolic-links is recommended to prevent assorted security risks
    symbolic-links=0
    
    [mysqld_safe]
    log-error=/var/log/mysqld.log
    pid-file=/var/run/mysqld/mysqld.pid
    [root@localhost ~]# 
    

    (2)在/var/lib/mysql内存储了数据库文件,查看:
    [root@localhost mysql]# cd /var/lib/mysql
    [root@localhost mysql]# ls -l
    total 20500
    -rw-r--r--. 1 root  root     10717 May 13 10:34 create
    -rw-rw----. 1 mysql mysql 10485760 May 13 10:27 ibdata1
    -rw-rw----. 1 mysql mysql  5242880 May 13 10:27 ib_logfile0
    -rw-rw----. 1 mysql mysql  5242880 May 13 10:27 ib_logfile1
    drwx------. 2 mysql mysql     4096 May 13 10:27 mysql
    srwxrwxrwx. 1 mysql mysql        0 May 13 10:27 mysql.sock
    drwx------. 2 mysql mysql     4096 May 13 10:27 test
    [root@localhost mysql]# 
    

    4.2 数据库基本操作

    (1)创建一个数据库
    由上面的默认文件可以看出,我们的MySQL数据库中有两个默认database,一个是mysql,另一个是test。
    我们创建一个自己的数据库,命令如下:
    [root@localhost mysql]# mysql -u root -p
    Enter password: 
    Welcome to the MySQL monitor.  Commands end with ; or g.
    Your MySQL connection id is 21
    Server version: 5.1.73 Source distribution
    
    Copyright (c) 2000, 2013, 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> create database yr;
    Query OK, 1 row affected (0.00 sec)
    
    mysql> exit
    Bye
    [root@localhost mysql]# 
    首先,我们用root账户登录数据库,可以看到mysql> 命令提示符号,执行创建数据库命令create database name; 得到创建成功消息,退出数据库使用命令exit。
    现在,我们可以在/var/lib.mysql下面看到已经创建的数据库yr:
    [root@localhost mysql]# ls -l
    total 20504
    -rw-r--r--. 1 root  root     10717 May 13 10:34 create
    -rw-rw----. 1 mysql mysql 10485760 May 13 10:27 ibdata1
    -rw-rw----. 1 mysql mysql  5242880 May 13 10:27 ib_logfile0
    -rw-rw----. 1 mysql mysql  5242880 May 13 10:27 ib_logfile1
    drwx------. 2 mysql mysql     4096 May 13 10:27 mysql
    srwxrwxrwx. 1 mysql mysql        0 May 13 10:27 mysql.sock
    drwx------. 2 mysql mysql     4096 May 13 10:27 test
    drwx------. 2 mysql mysql     4096 May 13 14:41 yr
    [root@localhost mysql]# 
    
    (2)建表并添加数据
    下面,再次登录MySQL我们在上面创建的数据库中建表,并添加几条数据:
    [root@localhost mysql]# mysql -u root -p
    Enter password: 
    Welcome to the MySQL monitor.  Commands end with ; or g.
    Your MySQL connection id is 23
    Server version: 5.1.73 Source distribution
    
    Copyright (c) 2000, 2013, 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> use yr;
    Database changed
    mysql> create table user(
        -> Id INTEGER AUTO_INCREMENT NOT NULL PRIMARY KEY,
        -> Name VARCHAR(50),
        -> age INTEGER);
    Query OK, 0 rows affected (0.07 sec)
    
    mysql> 

    至此,yr数据库中便创建成功一个user表,有Id 、 Name 、age三个属性,其中Id为主键。
    添加并查询数据:
    mysql> insert into user(Id , Name , age)values(1,"aa",20);
    Query OK, 1 row affected (0.10 sec)
    
    mysql> insert into user(Id , Name , age)values(2,"bb",21);
    Query OK, 1 row affected (0.00 sec)
    mysql> select Id , Name , age from user;
    +----+------+------+
    | Id | Name | age  |
    +----+------+------+
    |  1 | aa   |   20 |
    |  2 | bb   |   21 |
    +----+------+------+
    
    (3)从数据库表中删除数据
    下面我们删除一条Id=1的数据:
    mysql> delete from user where Id=1;
    Query OK, 1 row affected (0.05 sec)
    
    mysql> select Id , Name , age from user;
    +----+------+------+
    | Id | Name | age  |
    +----+------+------+
    |  2 | bb   |   21 |
    +----+------+------+
    1 row in set (0.00 sec)
    
    mysql> 

    (4)删除数据库表和数据库
    删除数据库表和数据库使用drop命令:
    mysql> drop table user;
    Query OK, 0 rows affected (0.02 sec)
    
    mysql> drop database yr;
    Query OK, 0 rows affected (0.28 sec)
    
    mysql> exit
    Bye
    [root@localhost mysql]# ll
    total 20500
    -rw-r--r--. 1 root  root     10717 May 13 10:34 create
    -rw-rw----. 1 mysql mysql 10485760 May 13 10:27 ibdata1
    -rw-rw----. 1 mysql mysql  5242880 May 13 10:27 ib_logfile0
    -rw-rw----. 1 mysql mysql  5242880 May 13 10:27 ib_logfile1
    drwx------. 2 mysql mysql     4096 May 13 10:27 mysql
    srwxrwxrwx. 1 mysql mysql        0 May 13 10:27 mysql.sock
    drwx------. 2 mysql mysql     4096 May 13 10:27 test
    [root@localhost mysql]# 

    以上是linux系统有关mysql安装、配置以及使用的全部内容。
  • 相关阅读:
    DevExpress.XtraScheduler控件的使用方法
    读写Excel文档
    让程序以管理员身份运行
    读写TXT文档
    判断程序是自动启动还是用户启动
    用指针读BMP图像
    判断网络是否连接通
    WSL初体验
    Realsense内参标定
    FreeSwitch权威指南
  • 原文地址:https://www.cnblogs.com/shine-yr/p/5214956.html
Copyright © 2011-2022 走看看