📄 mysql database and linux tutorial.mht
字号:
From: <Saved by Microsoft Internet Explorer 5>
Subject: MySQL database and Linux Tutorial
Date: Wed, 4 Apr 2007 10:54:01 +0800
MIME-Version: 1.0
Content-Type: multipart/related;
type="text/html";
boundary="----=_NextPart_000_009D_01C776A7.8DBB0B10"
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3028
This is a multi-part message in MIME format.
------=_NextPart_000_009D_01C776A7.8DBB0B10
Content-Type: text/html;
charset="utf-8"
Content-Transfer-Encoding: quoted-printable
Content-Location: http://www.yolinux.com/TUTORIALS/LinuxTutorialMySQL.html
=EF=BB=BF<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD><TITLE>MySQL database and Linux Tutorial</TITLE>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Dutf-8"><LINK=20
href=3D"http://www.yolinux.com/TUTORIALS/yolinux.css" type=3Dtext/css=20
rel=3DSTYLESHEET>
<META content=3DMySQL,linux,tips,howto,help,install,setup =
name=3DKEYWORDS>
<META=20
content=3D"Using MySQL database with Linux. This tutorial also covers =
some of the pitfalls users may experience."=20
name=3DDESCRIPTION>
<META content=3D"Greg Ippolito" name=3Dauthor>
<META content=3D"MSHTML 6.00.2900.3059" name=3DGENERATOR></HEAD>
<BODY bgColor=3D#cccccc>
<HR SIZE=3D5>
<TABLE>
<TBODY>
<TR>
<TD width=3D"70%">
<H1>MySQL and Linux Tutorial</H1></TD></TR>
<TR>
<TD vAlign=3Dtop align=3Dright><!-- BEGIN RICH-MEDIA BURST! CODE =
--><!-- END BURST CODE --></TD></TR></TBODY></TABLE>
<HR SIZE=3D5>
<P>
<TABLE cellPadding=3D20>
<TBODY>
<TR>
<TD><IMG =
src=3D"http://www.yolinux.com/TUTORIALS/images/tux-DB-t3.gif"></TD><!-- =
Original artwork by Paul Pettit of Venice CA (310)617-6250, Copyright =
YoLinux.com -->
<TD vAlign=3Dtop>This tutorial covers the MySQL database on Red Hat =
Linux.=20
This tutorial will also cover the generation and use a simple =
database.=20
The interface language of the MySQL database is the standard SQL =
(Standard=20
Query Language) which allows for inserts, updates and queries of =
data=20
stored in relational tables. The SQL language is also used for the =
administration of the database for the creation and modification =
of=20
tables, users and access privileges. Tables are identified by =
unique names=20
and hold data in a row and column (record) structure. A fixed =
number of=20
named columns are defined for a table with a variable number of =
rows.=20
</TD></TR></TBODY></TABLE>
<P>
<HR>
<TABLE cellSpacing=3D1 cellPadding=3D4 width=3D"100%" bgColor=3D#000000 =
border=3D1>
<TBODY>
<TR bgColor=3D#cccccc>
<TD vAlign=3Dtop width=3D160 bgColor=3D#c0c0c0>
<P><FONT size=3D-1><B>Related YoLinux Tutorials:</B>=20
<P>=C2=B0<A =
href=3D"http://www.yolinux.com/TUTORIALS/LinuxDatabases.html">List of=20
Linux Databases</A>=20
<P>=C2=B0<A=20
=
href=3D"http://www.yolinux.com/TUTORIALS/LinuxTutorialPostgreSQL.html">Po=
stgreSQL</A>=20
<P>=C2=B0<A=20
=
href=3D"http://www.yolinux.com/TUTORIALS/LinuxTutorialTomcat.html">Apache=
,=20
Tomcat, MySQL and Java</A>=20
<P>=C2=B0<A =
href=3D"http://www.yolinux.com/TUTORIALS/index.html">YoLinux=20
Tutorials Index</A> </FONT>
<P>
<HR>
<P>
<SCRIPT type=3Dtext/javascript><!--=0A=
google_ad_client =3D "pub-8567479315106986";=0A=
google_ad_width =3D 160;=0A=
google_ad_height =3D 600;=0A=
google_ad_format =3D "160x600_as";=0A=
google_ad_channel =3D"";=0A=
google_color_border =3D ["000000","6699CC","2D5893","191933"];=0A=
google_color_bg =3D ["F0F0F0","003366","99AACC","333366"];=0A=
google_color_link =3D ["0000FF","FFFFFF","000000","99CC33"];=0A=
google_color_url =3D ["008000","AECCEB","000099","FFCC00"];=0A=
google_color_text =3D ["000000","AECCEB","003366","FFFFFF"];=0A=
//--></SCRIPT>
<SCRIPT =
src=3D"http://pagead2.googlesyndication.com/pagead/show_ads.js"=20
type=3Dtext/javascript>=0A=
</SCRIPT>
</P></TD>
<TD vAlign=3Dtop>
<TABLE cellSpacing=3D0 cellPadding=3D2 width=3D"100%" border=3D0>
<TBODY>
<TR bgColor=3D#ffcc33>
<TD><B><BIG>The MySQL Database Installation and=20
configuration:</BIG></B></TD></TR></TBODY></TABLE>
<P>
<H3>Red Hat / Fedora Core RPM Packages:</H3>
<UL>
<LI>mysql-<I>VERSION</I>.i386.rpm (Required)=20
<LI>mysql-server-<I>VERSION</I>.i386.rpm (Required)=20
<LI>mysqlclient9-<I>VERSION</I>.i386.rpm (Shared object =
libraries)=20
<LI>mysql-devel-<I>VERSION</I>.i386.rpm (C include files and =
libraries=20
for software developers)=20
<LI>php-mysql-<I>VERSION</I>.i386.rpm (For accessing MySQL =
database from=20
php) </LI></UL>Install: <SMALL><TT>rpm -ivh =
mysql-<I>VERSION</I>.i386.rpm=20
mysql-server-<I>VERSION</I>.i386.rpm=20
mysqlclient9-<I>VERSION</I>.i386.rpm</TT></SMALL> <BR>Check if =
installed:=20
<SMALL><TT>rpm -q mysql mysql-server mysqlclient9</TT></SMALL> =
<BR>The=20
examples on this page used mySQL 3.23.58 which is used in Red Hat =
7, 8, 9=20
and Fedora Core 1, 2, 3. <BR>Also see <A=20
=
href=3D"http://www.yolinux.com/TUTORIALS/LinuxTutorialSysAdmin.html#RPM">=
YoLinux.com=20
systems administration - using RPM</A> to set GPG signatures and =
install=20
RPM packages.=20
<P><B>Installing MySQL.com RPM packages:</B> If instaling newer =
versions=20
of MySQL from RPM packages obtained from MySQL.com, you must first =
import=20
and register their public GPG key:=20
<OL>
<LI>Download public key named <TT>build@mysql.com</TT> from <A=20
href=3D"http://www.keyserver.net/">http://www.keyserver.net/</A> =
with one=20
of two methods:=20
<UL>
<LI><TT>wget --output-document=3Dpubkey_mysql.asc=20
=
http://keyserver.veridis.com:11371/export?id=3D-8326718950139043339</TT> =
<BR>(Saves key 0x5072E1F5 as file <TT>pubkey_mysql.asc</TT>)=20
<LI><TT>gpg --keyserver keyserver.veridis.com --recv-key =
5072e1f5</TT>=20
<BR><TT>gpg --export -a 5072e1f5 > pubkey_mysql.asc</TT> =
</LI></UL>
<LI>Import key: <TT>rpm --import pubkey_mysql.asc</TT> =
</LI></OL>
<P>
<DL>
<DD><FONT color=3D#ff0000>[Potential Pitfall]</FONT>: Your =
system should=20
have a host name other than the default "localhost". Give your =
systems a=20
host name if you get the following installation error:=20
<DL>
<DD>
<TABLE cellSpacing=3D1 cellPadding=3D4 width=3D"100%" =
bgColor=3D#000000=20
border=3D1>
<TBODY>
<TR bgColor=3D#cccccc>
<TD><PRE>ERROR: 1062 Duplicate entry 'localhost-root' for =
key 1
ERROR: 1062 Duplicate entry 'localhost-' for key 1
</PRE></TD></TR></TBODY></TABLE></DD></DL>Use the command=20
<TT>hostname</TT> to give your system a hostname and also set in =
the=20
configuration file <TT>/etc/sysconfig/network </TT></DD></DL>
<P>
<H3>Start the database:</H3>
<DL>
<DD>Start the database: <TT>/etc/rc.d/init.d/mysqld start</TT> =
<BR>(The=20
script will run <TT>mysql_install_db</TT> to create a default =
database=20
in <TT>/var/lib/mysql/mysql/</TT> if the mysql init script has =
never=20
been run before. The install script will not be run again as =
long as the=20
default database directory exists.) <BR>The database executes as =
user=20
<TT>mysqld</TT> and group <TT>mysqld</TT>. </DD></DL>
<P>Notes:=20
<UL>
<LI>One may manually initialize the database with the command:=20
<TT>/usr/bin/mysql_install_db</TT> <BR>Creates system tables in=20
<TT>/var/lib/mysql/mysql/</TT> <BR>Only execute the first time =
MySQL is=20
installed.=20
<LI>Databases located in: <TT>/var/lib/mysql/</TT>=20
<LI>Default config file installed by RPM: <TT>/etc/my.cnf</TT>=20
<DL>
<DD>
<TABLE cellSpacing=3D1 cellPadding=3D4 width=3D"100%" =
bgColor=3D#000000=20
border=3D1>
<TBODY>
<TR bgColor=3D#c0c0c0>
<TD><PRE>[mysqld]
datadir=3D/var/lib/mysql
socket=3D/var/lib/mysql/mysql.sock
[mysql.server]
user=3Dmysql
basedir=3D/var/lib
[safe_mysqld]
err-log=3D/var/log/mysqld.log
pid-file=3D/var/run/mysqld/mysqld.pid
</PRE></TD></TR></TBODY></TABLE></DD></DL></LI></UL>
<P>
<H3>Post installation:</H3>
<OL>
<LI>Admin user id: <TT>root</TT> <BR>Default password:=20
<TT><I>blank</I></TT>=20
<P>The first task is to assign a password:=20
<DL>
<DD>
<TABLE cellSpacing=3D1 cellPadding=3D4 width=3D"100%" =
bgColor=3D#000000=20
border=3D1>
<TBODY>
<TR bgColor=3D#cccccc>
<TD><PRE><B>[prompt]$</B> mysqladmin -u root password =
'<I>new-password</I>'
</PRE></TD></TR></TBODY></TABLE>Note: the following SQL commands=20
will also work:=20
<TABLE cellSpacing=3D1 cellPadding=3D4 width=3D"100%" =
bgColor=3D#000000=20
border=3D1>
<TBODY>
<TR bgColor=3D#cccccc>
<TD><PRE><B>mysql></B> USE mysql;
<B>mysql></B> UPDATE user SET =
Password=3DPASSWORD('<B><I>new-password</I></B>') WHERE user=3D'root';
<B>mysql></B> FLUSH PRIVILEGES;
</PRE></TD></TR></TBODY></TABLE></DD></DL>
<LI>Create a database: (Creates directory=20
<TT>/var/lib/mysql/bedrock</TT>)=20
<DL>
<DD>
<TABLE cellSpacing=3D1 cellPadding=3D4 width=3D"100%" =
bgColor=3D#000000=20
border=3D1>
<TBODY>
<TR bgColor=3D#cccccc>
<TD><PRE><B>[prompt]$</B> mysqladmin -h localhost -u root =
-p<B><I>password</I></B> create bedrock
</PRE></TD></TR></TBODY></TABLE>(or use SQL command: <TT>CREATE=20
DATABASE bedrock;</TT>) </DD></DL>
<P></P>
<LI>Add tables, data, etc: <BR>Connect to database and issue the =
following SQL commands:=20
<DL>
<DD>
<TABLE cellSpacing=3D1 cellPadding=3D4 width=3D"100%" =
bgColor=3D#000000=20
border=3D1>
<TBODY>
<TR bgColor=3D#cccccc>
<TD><PRE><B>[prompt]$</B> mysql -h localhost -u root =
-p<B><I>password</I></B>
...
<B>mysql></B> use bedrock; <B><I>- Define database to =
connect to. Refers to directory path: /var/lib/mysql/bedrock</I></B>
<B>mysql></B> create table employee (Name char(20),Dept =
char(20),jobTitle char(20));
<B>mysql></B> DESCRIBE employee; <B><I>- View the table just =
created. Same as "show columns from employee;"</I></B>
+----------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+----------+------+-----+---------+-------+
| Name | char(20) | YES | | NULL | |
| Dept | char(20) | YES | | NULL | |
| jobTitle | char(20) | YES | | NULL | |
+----------+----------+------+-----+---------+-------+
3 rows in set (0.03 sec)
<B>mysql></B> show tables;
+-------------------+
| Tables_in_bedrock |
+-------------------+
| employee |
+-------------------+
<B>mysql></B> INSERT INTO employee VALUES ('Fred Flinstone','Quarry =
Worker','Rock Digger');
<B>mysql></B> INSERT INTO employee VALUES ('Wilma =
Flinstone','Finance','Analyst');
<B>mysql></B> INSERT into employee values ('Barney =
Rubble','Sales','Neighbor');
<B>mysql></B> INSERT INTO employee VALUES ('Betty =
Rubble','IT','Neighbor');
</PRE></TD></TR></TBODY></TABLE></DD></DL>Note: Data type used was=20
<TT>CHAR</TT>. Other data types include:=20
<UL>
<LI><TT>CHAR(M)</TT> : Fixed length string. Always stores M =
characters=20
whether it is holding 2 or 20 characters. Where M can range 1 =
to 255=20
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -