一、可以到http://mirrors.sohu.com上下载与自己电脑匹配的mysql rpm软件包或压缩包,我们这里使用二进制来安装MySQL

二、建议最好把MySQL安装在一个单独的分区上,最好使用逻辑卷LVM,这里我们安装在/dev/sdb5上使用逻辑分区

[root@beryl ~]# fdisk -l
[root@beryl ~]# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel. Changes will remain in memory only,
until you decide to write them. After that, of course, the previous
content won't be recoverable.


The number of cylinders for this disk is set to 2610.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
   (e.g., DOS FDISK, OS/2 FDISK)
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-2610, default 1):
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-2610, default 2610): +5G

Command (m for help): p

Disk /dev/sdb: 21.4 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         609     4891761   83  Linux

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
e
Partition number (1-4): 2
First cylinder (610-2610, default 610):
Using default value 610
Last cylinder or +size or +sizeM or +sizeK (610-2610, default 2610):
Using default value 2610

Command (m for help): p

Disk /dev/sdb: 21.4 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         609     4891761   83  Linux
/dev/sdb2             610        2610    16073032+   5  Extended

Command (m for help): n
Command action
   l   logical (5 or over)
   p   primary partition (1-4)
l
First cylinder (610-2610, default 610):
Using default value 610
Last cylinder or +size or +sizeM or +sizeK (610-2610, default 2610):
Using default value 2610

Command (m for help): p

Disk /dev/sdb: 21.4 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         609     4891761   83  Linux
/dev/sdb2             610        2610    16073032+   5  Extended
/dev/sdb5             610        2610    16073001   83  Linux

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
[root@beryl ~]# partprobe /dev/sdb
[root@beryl ~]# mkfs -t ext3 /dev/sdb5     //格式化为ext3
mke2fs 1.39 (29-May-2006)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
2011296 inodes, 4018250 blocks
200912 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4118806528
123 block groups
32768 blocks per group, 32768 fragments per group
16352 inodes per group
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208

Writing inode tables: done                           
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 22 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
[root@beryl ~]# mkdir mydata       //在根目录下创建mydata目录
[root@beryl ~]# mount /dev/sdb5 mydata/      //挂载sdb5到mydata目录上
[root@beryl ~]# ls mydata/       //查看挂载是否成功,lost+found表明挂载成功
lost+found
[root@beryl ~]# useradd -r -M mysql      //添加mysql用户,-r表示为系统用户


[root@beryl ~]# id mysql        //查看mysql属主属组
uid=27(mysql) gid=27(mysql) groups=27(mysql)
[root@beryl ~]# cd mydata/    
[root@beryl mydata]# mkdir data     //在mydata下创建data目录用于专门来安装mysql
[root@beryl mydata]# chown -R mysql:mysql .   

  //必须指定mydata属主属组及其子目录属主属组为mysql

[root@beryl mydata]# ll
total 20
drwxr-xr-x 2 mysql mysql  4096 Nov  3 21:45 data
drwx------ 2 mysql mysql 16384 Nov  3 21:43 lost+found
[root@beryl ~]# cd
[root@beryl ~]# tar -zxvf Desktop/mysql-5.5.17-linux2.6-i686.tar.gz -C /usr/local/

//解压mysql并使用-C指定直接解压到/usr/local
[root@beryl ~]# cd /usr/local    
[root@beryl local]# ls
bin  games    lib      mysql-5.5.17-linux2.6-i686  share
etc  include  libexec  sbin                        src
[root@beryl local]# ln -sv mysql-5.5.17-linux2.6-i686/ mysql   

//把解压的mysql创建链接到mysql
create symbolic link `mysql' to `mysql-5.5.17-linux2.6-i686/'
[root@beryl local]# ll -h
total 76K
drwxr-xr-x  2 root root 4.0K Aug  8  2008 bin
drwxr-xr-x  2 root root 4.0K Aug  8  2008 etc
drwxr-xr-x  2 root root 4.0K Aug  8  2008 games
drwxr-xr-x  2 root root 4.0K Aug  8  2008 include
drwxr-xr-x  2 root root 4.0K Aug  8  2008 lib
drwxr-xr-x  2 root root 4.0K Aug  8  2008 libexec
lrwxrwxrwx  1 root root   27 Nov  3 21:48 mysql -> mysql-5.5.17-linux2.6-i686/
drwxr-xr-x 13 root root 4.0K Nov  3 21:46 mysql-5.5.17-linux2.6-i686
drwxr-xr-x  2 root root 4.0K Aug  8  2008 sbin
drwxr-xr-x  4 root root 4.0K Apr 12  2011 share
drwxr-xr-x  2 root root 4.0K Aug  8  2008 src
[root@beryl local]# cd mysql
[root@beryl mysql]# ll
total 76
drwxr-xr-x  2 root root   4096 Nov  3 21:46 bin
-rw-r--r--  1 7161 wheel 17987 Oct 12 04:10 COPYING
drwxr-xr-x  4 root root   4096 Nov  3 21:46 data
drwxr-xr-x  2 root root   4096 Nov  3 21:46 docs
drwxr-xr-x  3 root root   4096 Nov  3 21:46 include
-rw-r--r--  1 7161 wheel  7604 Oct 12 04:10 INSTALL-BINARY
drwxr-xr-x  3 root root   4096 Nov  3 21:46 lib
drwxr-xr-x  4 root root   4096 Nov  3 21:46 man
drwxr-xr-x 10 root root   4096 Nov  3 21:46 mysql-test
-rw-r--r--  1 7161 wheel  2552 Oct 12 04:10 README
drwxr-xr-x  2 root root   4096 Nov  3 21:46 scripts
drwxr-xr-x 27 root root   4096 Nov  3 21:46 share
drwxr-xr-x  4 root root   4096 Nov  3 21:46 sql-bench
drwxr-xr-x  2 root root   4096 Nov  3 21:46 support-files
[root@beryl mysql]# chown -R mysql:mysql .              

//把当前目录属主属组递归修改mysql

[root@beryl mysql]# ll -h
total 76K
drwxr-xr-x  2 mysql mysql 4.0K Nov  3 21:46 bin
-rw-r--r--  1 mysql mysql  18K Oct 12 04:10 COPYING
drwxr-xr-x  4 mysql mysql 4.0K Nov  3 21:46 data
drwxr-xr-x  2 mysql mysql 4.0K Nov  3 21:46 docs
drwxr-xr-x  3 mysql mysql 4.0K Nov  3 21:46 include
-rw-r--r--  1 mysql mysql 7.5K Oct 12 04:10 INSTALL-BINARY
drwxr-xr-x  3 mysql mysql 4.0K Nov  3 21:46 lib
drwxr-xr-x  4 mysql mysql 4.0K Nov  3 21:46 man
drwxr-xr-x 10 mysql mysql 4.0K Nov  3 21:46 mysql-test
-rw-r--r--  1 mysql mysql 2.5K Oct 12 04:10 README
drwxr-xr-x  2 mysql mysql 4.0K Nov  3 21:46 scripts
drwxr-xr-x 27 mysql mysql 4.0K Nov  3 21:46 share
drwxr-xr-x  4 mysql mysql 4.0K Nov  3 21:46 sql-bench
drwxr-xr-x  2 mysql mysql 4.0K Nov  3 21:46 support-files
[root@beryl mysql]# scripts/mysql_install_db --help      //查看mysql安装帮助信息
Usage: scripts/mysql_install_db [OPTIONS]
  --basedir=path       The path to the MySQL installation directory.  

 //默认安装位置就是/usr/local
  --builddir=path      If using --srcdir with out-of-directory builds, you
                       will need to set this to the location of the build
                       directory where built files reside.
  --cross-bootstrap    For internal use.  Used when building the MySQL system
                       tables on a different host than the target.
  --datadir=path       The path to the MySQL data directory.
  --defaults-extra-file=name
                       Read this file after the global files are read.
  --defaults-file=name Only read default options from the given file name.
  --force              Causes mysql_install_db to run even if DNS does not
                       work.  In that case, grant table entries that normally
                       use hostnames will use IP addresses.
  --help               Display this help and exit.                    
  --ldata=path         The path to the MySQL data directory. Same as --datadir.
  --no-defaults        Don't read default options from any option file.
  --rpm                For internal use.  This option is used by RPM files
                       during the MySQL installation process.
  --skip-name-resolve  Use IP addresses rather than hostnames when creating
                       grant table entries.  This option can be useful if
                       your DNS does not work.
  --srcdir=path        The path to the MySQL source directory.  This option
                       uses the compiled binaries and support files within the
                       source tree, useful for if you don't want to install
                       MySQL yet and just want to create the system tables.
  --user=user_name     The login username to use for running mysqld.  Files
                       and directories created by mysqld will be owned by this
                       user.  You must be root to use this option.  By default
                       mysqld runs using your current login name and files and
                       directories that it creates will be owned by you.

All other options are passed to the mysqld program
[root@beryl mysql]# scripts/mysql_install_db --datadir=/mydata/data --user=mysql
WARNING: The host 'beryl.tie' could not be looked up with resolveip.
This probably means that your libc libraries are not 100 % compatible
with this binary MySQL version. The MySQL daemon, mysqld, should work
normally with the exception that host name resolving will not work.
This means that you should use IP addresses instead of hostnames
when specifying MySQL privileges !
Installing MySQL system tables...
OK
Filling help tables...
OK

To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system

PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:

./bin/mysqladmin -u root password 'new-password'
./bin/mysqladmin -u root -h beryl.tie password 'new-password'

Alternatively you can run:
./bin/mysql_secure_installation

which will also give you the option of removing the test
databases and anonymous user created by default.  This is
strongly recommended for production servers.

See the manual for more instructions.

You can start the MySQL daemon with:
cd . ; ./bin/mysqld_safe &

You can test the MySQL daemon with mysql-test-run.pl
cd ./mysql-test ; perl mysql-test-run.pl

Please report any problems with the ./bin/mysqlbug script!

[root@beryl mysql]# ln -sv /usr/local/mysql/include/ /usr/include/mysql

//把include头文件链接到/usr/local/mysql
create symbolic link `/usr/include/mysql' to `/usr/local/mysql/include/'
[root@beryl mysql]# ls man
man1  man8
[root@beryl mysql]# vim /etc/man.config        //导出man文件,加载man路径
MANPATH /usr/local/mysql/man
[root@beryl mysql]# vim /etc/ld.so.conf.d/mysql.conf     

 //导处所有lib库到/usr/local/mysql/lib
/usr/local/mysql/lib
[root@beryl mysql]# ldconfig -v          //整个库文件重新加载一下当前所有的库
[root@beryl mysql]# vim /etc/profile      //导出bin二进制文件
PATH=$PATH:/usr/local/mysql/bin
[root@beryl mysql]# . !$     //为了让其立即生效可重读,但有其他副作用
. /etc/profile
[root@beryl mysql]# cd support-files/      //support-files存放所有启动脚本
[root@beryl support-files]# ls
binary-configure   my-huge.cnf             mysqld_multi.server
config.huge.ini    my-innodb-heavy-4G.cnf  mysql-log-rotate
config.medium.ini  my-large.cnf            mysql.server
config.small.ini   my-medium.cnf           ndb-config-2-node.ini
magic              my-small.cnf
[root@beryl support-files]# cp mysql.server /etc/init.d/mysqld 

//拷贝mysqld启动脚本到init.d下
[root@beryl support-files]# chkconfig --add mysqld    //添加mysqld到开机自启动
[root@beryl support-files]# chkconfig --list mysqld     
[root@beryl support-files]# cp my-large.cnf /etc/my.cnf       

//为当前系统准备my.cnfl配置文件并拷贝到/etc/my.cnf  , 要根据自己硬件的内存选择此配置文件,可以使用free -m命令查看自己的内存大小

[root@beryl support-files]# vim /etc/my.cnf     

thread_concurrency = 2         //并发线程最好为cpu个数的两倍
datadir = /mydata/data           //添加目录数据目录所在的位置
[root@beryl support-files]# cd
[root@beryl ~]# service mysqld start          //启动mysql
Starting MySQL...   

[root@beryl ~]# cd mydata/data/      //可以到mydata下的data查看安装之后所产生的文件
[root@beryl ~]# netstat -tnlp             //查看mysql端口3306是否已开启
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name  
tcp        0      0 127.0.0.1:2208              0.0.0.0:*                   LISTEN      2480/hpiod         
tcp        0      0 0.0.0.0:611                 0.0.0.0:*                   LISTEN      2125/rpc.statd     
tcp        0      0 0.0.0.0:111                 0.0.0.0:*                   LISTEN      2092/portmap       
tcp        0      0 127.0.0.1:631               0.0.0.0:*                   LISTEN      2511/cupsd         
tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN      2543/sendmail: acce
tcp        0      0 127.0.0.1:2207              0.0.0.0:*                   LISTEN      2485/python        
tcp        0      0 :::3306                     :::*                        LISTEN      4782/mysqld        
tcp        0      0 :::22                       :::*                        LISTEN      2500/sshd          
[root@beryl ~]# mysqladmin -uroot -hlocalhost password '123456'

//mysql默认安装成功后所有账号都没有设置密码,因此需要给mysql相关账号添加密码。-h可直接指定为其添加密码的主机名称。若不适用-h则表示为所有root用户添加密码。
[root@beryl ~]# mysql -uroot -p      //使用root用户连入mysql
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.5.17-log MySQL Community Server (GPL)

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> use mysql          //设置默认使用的数据库
Database changed
mysql> SELECT User,Host,Password FROM user WHERE User='root';
+------+-----------+-------------------------------------------+
| User | Host      | Password                                  |
+------+-----------+-------------------------------------------+
| root | localhost | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| root | beryl.tie |                                           |
| root | 127.0.0.1 |                                           |
| root | ::1       |                                           |
+------+-----------+-------------------------------------------+
4 rows in set (0.00 sec)
mysql> SET PASSWORD FOR root@beryl.tie=PASSWORD('123456');  

//使用另一种方式为mysql用户添加密码
Query OK, 0 rows affected (0.01 sec)
mysql> SELECT User,Host,Password FROM user WHERE User='root';

//查看mysql所有root用户、主机、密码等信息
+------+-----------+-------------------------------------------+
| User | Host      | Password                                  |
+------+-----------+-------------------------------------------+
| root | localhost | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| root | beryl.tie | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| root | 127.0.0.1 |                                           |
| root | ::1       |                                           |
+------+-----------+-------------------------------------------+
4 rows in set (0.00 sec)
mysql> FLUSH PRIVILEGES;          //加速生效
Query OK, 0 rows affected (0.00 sec)
mysql> SET PASSWORD FOR root@127.0.0.1=PASSWORD('123456');
Query OK, 0 rows affected (0.00 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT User,Host,Password FROM user WHERE User='root';
+------+-----------+-------------------------------------------+
| User | Host      | Password                                  |
+------+-----------+-------------------------------------------+
| root | localhost | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| root | beryl.tie | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| root | 127.0.0.1 | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| root | ::1       |                                           |
+------+-----------+-------------------------------------------+
4 rows in set (0.00 sec)
mysql> DROP USER root@'::1';            //删除ipv6用户账号
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT User,Host,Password FROM user WHERE User='root';
+------+-----------+-------------------------------------------+
| User | Host      | Password                                  |
+------+-----------+-------------------------------------------+
| root | localhost | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| root | beryl.tie | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| root | 127.0.0.1 | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+------+-----------+-------------------------------------------+
3 rows in set (0.00 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
mysql> GRANT ALL PRIVILEGES ON *.* TO root@'192.168.200.%' IDENTIFIED BY '123456';

//添加使其192.168.200网段的用户具有连入mysql数据库的权限
Query OK, 0 rows affected (0.01 sec)

mysql> SELECT User,Host,Password FROM user WHERE User='root';
+------+---------------+-------------------------------------------+
| User | Host          | Password                                  |
+------+---------------+-------------------------------------------+
| root | localhost     | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| root | beryl.tie     | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| root | 127.0.0.1     | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| root | 192.168.200.% | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+------+---------------+-------------------------------------------+
4 rows in set (0.00 sec)
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.01 sec)
mysql> SELECT User,Host,Password FROM user WHERE User='';

//mysql数据库默认安装成功后会有两个为空的用户账号,很不安全
+------+-----------+----------+
| User | Host      | Password |
+------+-----------+----------+
|      | localhost |          |
|      | beryl.tie |          |
+------+-----------+----------+
2 rows in set (0.00 sec)
mysql> help DELETE      //使用help查看DELETE命令的帮助信息

mysql> DELETE FROM user WHERE user='';         //删除这两个用户名为空的账号
Query OK, 2 rows affected (0.01 sec)

mysql> SELECT User,Host,Password FROM user WHERE User='';
Empty set (0.00 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

 

 

//使用二进制方式在redhat上安装mysql及其基本配置,暂做到这里,谢谢