Linux下安装MySQL数据库及其使用

跟版网(www.genban.org)提供及其,数据库,安装,mysql,MySQL,资料库,密码,0跟版网精品网站模板,跟版网模板,网站模板,等网页设计素材资源,提供相关网页设计资源的教程和免费下载。跟版网,专业织梦网页设计模板资源站。。

  一、什麽是 MySQL
  
  MySQL (发音为 "My Ess Que Ell")是 Tcx 公司(http://www.tcx.se)开发的一个多人使用、多执行绪的 SQL 资料库 Server。MySQL 主要的目标在快速、稳定和容易使用。
  
  MySQL 可在此 http://www.mysql.net/ 取得。
  
  二、MySQL 的安装
  
  本文所使用的 MySQL 版本为 mysql-3.22.27.tar.gz(原始码档),作业环境为 RedHat6.0 +CLE0.8 。
  
  MySQL 预设情况下会安装至 /usr/local 目录下,不过为了日後移除方便,建议将 mysql 独立安装在 /usr/local/mysql 目录。底下为安装 MySQL 的步骤:
  
  取得 mysql-3.22.27.tar.gz 後, 於 /usr/local 目录下解开:
  # cd /usr/local
  # tar zxvf mysql-3.22.27.tar.gz
  # cd mysql-3.22.27
  设定 configure 安装选项,选择安装目录 (prefix)以及支援中文 Big5 码(with-charset=big5):
  # ./configure --prefix=/usr/local/mysql #--with-charset=big5
  开始编译并安装:
  # make
  # make install
  # scripts/mysql_install_db
  最後一个步骤是用来产生 MySQL grant tables(会建立一个 mysql 资料库和一些 tables,用来管理使用 MySQL 的授权资讯,也就是使用者有哪些使用资料库的权限)。
  
  三、启动、停止 MySQL
  
  要启动 MySQL 的方法:(以本文将 MySQL 安装在 /usr/local/mysql 为例)
  
  # /usr/local/mysql/share/mysql.server start
  注意在第一次执行前,须将 mysql.server 设成可执行(chmod 744 mysql.server),另外可将这行指令加在 /etc/rc.d/rc.local 档中,让 MySQL 在开机时自动启动。
  
  要停止 MySQL 的方法:
  
  # /usr/local/mysql/bin/mysqladmin shutdown
  如果你为 MySQL Administrator root 帐号(非作业系统的 root)设了密码,要停止 MySQL 则必须像下列这样做,MySQL 会询问你 root 的密码後才会执行 shutdown 的工作:
  
  # /usr/local/mysql/bin/mysqladmin -u root -p shutdown
  
  四、管理与使用 MySQL 简介
  
  在你开始前
  MySQL 提供了许多工具 (Client Tools)来与 MySQL 资料库 Server 连线,其中最主要的为 mysql 交谈式连线工具与 mysqladmin 公用程式,大部份时候使用者会用 mysql 来和资料库 Server 交谈。底下就以 mysql 连线工具来介绍如何维护与使用 MySQL。(以本文安装为例,mysql 工具位於 /usr/local/mysql/bin/mysql)。
  
  mysql 的使用语法如下:
  
  mysql [-u username] [-h host] [-p[password]] [dbname]
  MySQL 资料库有自己一套使用者帐号与权限管控方法,所以这边所指定的 username 与 password 是 MySQL 的使用者与密码,而不是作业系统的使用者与密码(当然任何使用者都能执行 mysql ,然後以 MySQL 的任何帐号进入使用) 。
  
  在你第一次安装好 MySQL 时,MySQL 的管理帐号为 root,没有设定密码 (非作业系统的 root)。所以在开始前,请先照下列步骤为 root 设好密码:
  
  使用 mysql 与 MySQL 资料库 Server 连线:
  # /usr/local/mysql/bin/mysql -u root mysql
  Reading table information for completion of table and column names
  You can turn off this feature to get a quicker startup with -A
  
  Welcome to the MySQL monitor. Commands end with ; or \g.
  Your MySQL connection id is 201 to server version: 3.22.27
  
  Type 'help' for help.
  
  mysql>
  
  在下了 mysql -u root mysql 指令,指定以 root 帐号并开启 mysql 系统资料库,连线至 MySQL 後,会看到一些提示讯息与 mysql 工具的提示符号,以後大部份的工作皆在此提示符号下完成。
  
  更改 MySQL系统管理者 root 密码:
  mysql> update user set password=password('新密码') where user='root';
  Query OK, 0 rows affected (0.00 sec)
  Rows matched: 2 Changed: 0 Warnings: 0
  
  mysql> FLUSH PRIVILEGES;
  Query OK, 0 rows affected (0.00 sec)
  
  mysql> quit
  Bye
  注意每个指令後要加上一个分号 ";" 才会让 mysql 开始执行。而第二道指令会让已载入记忆体的 mysql 系统资料库更新,最後离开 mysql 工具程式。
  
  在更新 root 密码後,日後要与 MySQL 连线的方法为:
  
  mysql -u root -p新密码
  或者是这样,让 mysql 询问 root 的密码:
  
  mysql -u root -p
  
  资料库维护
  接下来,我们以简单的通讯录资料库作为例子,来介绍如何用 mysql 工具程式来做资料库的维护(新增、授权、资料表维护等)。
  
  首先,以 MySQL root 帐号连线後建立一 addbook 资料库:
  # /usr/local/mysql/bin/mysql -u root -p
  Enter password:
  Welcome to the MySQL monitor. Commands end with ; or \g.
  Your MySQL connection id is 207 to server version: 3.22.27
  
  Type 'help' for help.
  
  mysql> create databae addbook;
  Query OK, 1 row affected (0.00 sec)
  指定使用 addbook 资料库,并建立一个 friends 资料表:
  mysql> use addbook;
  Database changed
  
  mysql> create table friends (
  -> name Char(15),
  -> telphone VarChar(20),
  -> icq Char(10),
  -> address VarChar(30)
  -> );
  Query OK, 0 rows affected (0.00 sec)
  新增几笔资料,并查询看看:
  mysql> insert into friends values(
  -> "maa", "29016710", "46243046", "台北县新庄市"
  -> );
  Query OK, 1 row affected (0.00 sec)
  
  mysql> insert into friends (name, icq, telphone, address ) Values (
  -> "cxlin", "39425893", "7654321", "台北县"
  -> );
  Query OK, 1 row affected (0.01 sec)
  
  mysql> select * from friends;
  +-------+----------+----------+--------------+
  | name | telphone | icq   | address   |
  +-------+----------+----------+--------------+
  | maa  | 29016710 | 46243046 | 台北县新庄市 |
  | cxlin | 7654321 | 39425893 | 台北县    |
  +-------+----------+----------+--------------+
  2 rows in set (0.00 sec)
  
  第二个 insert 指令指定了资料栏位的插入顺序,用法较第一个为弹性,而第一个指令必须依资料表建立结构时的顺序插入资料。
  
  更新、删除资料表记录:
  mysql> update friends set address = "桃园县" where name = "cxlin";
  Query OK, 1 row affected (0.00 sec)
  Rows matched: 1 Changed: 1 Warnings: 0
  
  mysql> select * from friends where name = "cxlin";
  +-------+----------+----------+---------+
  | name | telphone | icq   | address |
  +-------+----------+----------+---------+
  | cxlin | 7654321 | 39425893 | 桃园县 |
  +-------+----------+----------+---------+
  1 row in set (0.00 sec)
  
  mysql> delete from friends where name = "maa";
  Query OK, 1 row affected (0.01 sec)
  
  mysql> select * from friends;
  +-------+----------+----------+---------+
  | name | telphone | icq   | address |
  +-------+----------+----------+---------+
  | cxlin | 7654321 | 39425893 | 桃园县 |
  +-------+----------+----------+---------+
  1 row in set (0.00 sec)
  最後,建好资料库与资料表後,把 addbook 资料库中所有资料表的使用权限(select、insert、update、delete)授权给 maa@localhost(再次提醒,此处的 maa 为 MySQL 的使用者帐号,而非作业系统的 maa 帐号):
  mysql> grant select, insert, update, delete
  -> on addbook.*
  -> to maa@localhost identified by '1234567';
  Query OK, 0 rows affected (0.00 sec)
  之後,可用 maa 的身份进入 MySQL 存取 addbook 资料库:
  
  # /usr/local/mysql/bin/mysql -u maa -p addbook
  Enter password:
  Reading table information for completion of table and column names
  You can turn off this feature to get a quicker startup with -A
  
  Welcome to the MySQL monitor. Commands end with ; or \g.
  Your MySQL connection id is 211 to server version: 3.22.27
  
  Type 'help' for help.
  
  mysql> status
  --------------
  ./mysql Ver 9.36 Distrib 3.22.27, for pc-linux-gnu (i686)
  
  Connection id:     26
  Current database:    addbook
  Current user:      maa@localhost
  Server version     3.22.27
  Protocol version    10
  Connection       Localhost via UNIX socket
  UNIX socket       /tmp/mysql.sock
  Uptime:
本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

数据库查询哪个对像里面包含什么字段语句写法: select * from sysobjects o, syscomments s where o.id = s.id and text like %text% and o.xtype = P text 换成需要查的字段 数据库查询哪个对像里面包含表: select o.name from sys.all_sql_modules s,sys
一、 创建用户: 命令:CREATE USER username@host IDENTIFIED BY password; 说明:username - 你将创建的用户名, host - 指定该用户在哪个主机上可以登陆,如果是本地用户可用localhost, 如果想让该用户可以从任意远程主机登陆,可以使用通配符%. password - 该
在mysql中可以用group by对查询出的数据分组 select id,service,name FROM service GROUP BY name,service 如果要查看每组数据的总数,可以 select count(*) FROM service GROUP BY name,service 当要查询group by后的总数,可以这样 select count(*) from(s
mysql count group by统计条数方法 mysql 分组之后如何统计记录条数? gourp by 之后的 count,把group by查询结果当成一个表再count一次 select count(*) as count from(SELECT count(*) FROM 表名 WHERE 条件 GROUP BY id ) a; 实战例子: select count(*)
1.首先停止MySQL服务:service mysqld stop 2.加参数启动mysql:/usr/bin/mysqld_safe --skip-grant-tables 然后就可以无任何限制的访问mysql了 3.root用户登陆系统:mysql -u root -p mysql 4.切换数据库:use mysql 5.显示所有的表:show tables; 这里就可
摘要: SQL的WHERE子句中包含多个AND和OR 示例: SQL解析器在处理操作时会优先处理and操作: 假如有表product字段如下:id、product_id、product_price、product_name,现在要查找产品号为100或者101,并且价格大于200的商品,程序员可能会这样写: select * fr