Linux下安装MySQL

安装环境:Linux服务器CentOS 5.5

安装版本:mysql-5.5.8.tar.gz

1、安装 cmake 编译器。

1)、下载cmake

#cd /usr/local/src

#wget http://www.cmake.org/files/v2.8/cmake-2.8.4.tar.gz

2)、解压cmake

#tar -zvxf cmake-2.8.4.tar.gz

3)、配置编译

#cd cmake-2.8.4
#yum -y install gcc
#yum -y install gcc-c++
#yum -y install ncurses-devel
#./configure
#make
#make install

2、安装MySQL

1)、下载MySQL。

#cd /usr/local/src

#wget http://sdk.ruiya.com/linux/mysql-5.5.9.tar.gz

2)、添加必要的组和拥有者

#groupadd mysql

#useradd -r -g mysql mysql

3)、解压MySQL

#tar -zvxf mysql-5.5.9.tar.gz

4)、配置编译

如果是重装MySql,请先删除my.cnf如: rm -rf /etc/my.cnf

#mkdir /usr/local/mysql

#mkdir /usr/local/mysql/data

#cd /usr/local/src/mysql-5.5.9

#cmake . \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DINSTALL_DATADIR=/usr/local/mysql/data \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DEXTRA_CHARSETS=all \
-DENABLED_LOCAL_INFILE=1

参数说明:

-DCMAKE_INSTALL_PREFIX=/usr/local/mysql        //安装目录
-DINSTALL_DATADIR=/usr/local/mysql/data         //数据库存放目录
-DDEFAULT_CHARSET=utf8                        //使用utf8字符
-DDEFAULT_COLLATION=utf8_general_ci            //校验字符
-DEXTRA_CHARSETS=all                            //安装所有扩展字符集
-DENABLED_LOCAL_INFILE=1                        //允许从本地导入数据

#make

#make install

注意事项:

重新编译时,需要清除旧的对象文件和缓存信息。 

# make clean
# rm -f  CMakeCache.txt
# rm -rf /etc/my.cnf 

4)、设置目录权限

# cd /usr/local/mysql

# chown -R root:mysql . //把当前目录中所有文件的所有者所有者设为root,所属组为mysql

# chown -R mysql:mysql data
 

5)、配置文件

# cp support-files/my-medium.cnf /etc/my.cnf //这个配置仅适合小内存系统(32M - 64M)

打开如下注释

innodb_data_home_dir = /usr/local/mysql/data
innodb_data_file_path = ibdata1:10M:autoextend
innodb_log_group_home_dir = /usr/local/mysql/data

innodb_buffer_pool_size = 16M
innodb_additional_mem_pool_size = 2M

innodb_log_file_size = 5M
innodb_log_buffer_size = 8M
innodb_flush_log_at_trx_commit = 1
innodb_lock_wait_timeout = 50

添加默认字符集

[client]
default-character-set = utf8    // 添加编码支持
[mysqld]
default-character-set = utf8   // 添加编码支持
max_connections = 10000     //根据服务器性能调节
basedir = /usr/local/mysql //设置安装目录,这样在系统启动时才能正确运行到/etc/rc.d/init.d/mysql start

6)、创建系统数据库的表

# cd /usr/local/mysql

# scripts/mysql_install_db --user=mysql

7)、设置权限启动

设置环境变量

# vi /root/.bash_profile

在PATH=$PATH:$HOME/bin添加参数为:

PATH=$PATH:$HOME/bin:/usr/local/mysql/bin:/usr/local/mysql/lib

#source /root/.bash_profile

手动启动MySQL 

# cd /usr/local/mysql

# ./bin/mysqld_safe --user=mysql &   //启动MySQL,但不能停止

启动日志写在此文件下:/usr/local/mysql/data/localhost.err

关闭MySQL服务

# mysqladmin -u root -p shutdown  //这里MySQL的root用户还没有配置密码,所以为空值。

通过脚本启动MySQL

# ln -s /usr/local/mysql/support-files/mysql.server /usr/local/mysql
//必须注意,是放在mysql目录下,不是bin目录下
# cp /usr/local/mysql/support-files/mysql.server /usr/local/mysql  
# mysql.server start //启动mysql
# mysql.server stop //停止mysql

在引导时启动MySQL

# ln -s /usr/local/mysql/support-files/mysql.server /etc/rc.d/init.d/mysql

# ln -s /usr/local/mysql/mysql.server /etc/rc.d/init.d/mysql 
# cd /etc/rc.d/init.d
# chkconfig --add mysql       //配置是否自动启动, chkconfig --del mysql 可删除
# chmod +x /etc/rc.d/init.d/mysql    //添加如执行权限

Tips
Linux运行级别:
分成了8种运行级别,其中常用7种。可在/etc/inittab文件中设置。
0 - halt
1 - Single user mode
2 - Multiuser, without NFS
3 - Full multiuser mode
4 - unused
5 - x11
6 - reboot
默认设置为:id:3:initdefault:
每一种动行级别都有自已独立的文件夹,例如:
/etc/rc.d/rc3.d 表示运行级别为3的配置都存放在这个文件侠中。

# chkconfig --list |grep mysql      //检查看是否设置为自启动
mysql 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭
表示: 运行级别2、3、4、5都会自动启动mysql

另一种手动配置自已启动

可能会出现这种情况,如果你试图在/etc/rc.d/rc3.d目录下运行../init.d/mysql start可能会收到如下错误

Starting MySQLCouldn't find MySQL server (./bin/mysqld_safe[失败]

可见mysql.server内部引用了一个相对路径./bin/mysqld_safe,所以这样就导致失败。

这样我们可以直接在rc.local文件中添加启动脚本:

# chkconfig --del mysql

# cd /etc/rc.d

# vi rc.local //添加: /usr/local/mysql/bin/mysqld_safe --user=mysql &

解决办法:在/etc/my.cnf 配置文件中添加:

basedir = /usr/local/mysql

8)、修改MySQL的root用户的密码

# mysql -u root mysql

mysql>use mysql;
mysql>desc user;
mysql> GRANT ALL PRIVILEGES ON *.* TO root@"%" IDENTIFIED BY "root";  //为root添加远程连接的能力。
mysql>update user set Password = password('xxxxxx') where User='root';
mysql>select Host,User,Password  from user where User='root';
mysql>flush privileges;
mysql>exit

重新登录:mysql -u root -p

9)、添加软链接

# ln -s /usr/local/mysql/lib/mysql /usr/lib/mysql
# ln -s /usr/local/mysql/include/mysql /usr/include/mysql

注意事项:

<1>、MySQL5.5 默认使用InnoDB作为存储引擎,所以可以不设置DWITH_MYISAM_STORAGE_ENGINE值

参考:

如何打开MySQL中root账户的远程登录 

mysql> GRANT ALL PRIVILEGES ON *.* TO root@"%" IDENTIFIED BY "root";
注意%对应的密码,如果不相同,可以通过 update user set Password = password('xxxxxx') where User='root'; 修改。

同时注意防火墙是否已关闭或者添加例外。

# /etc/rc.d/init.d/iptables stop

关闭或开启Linux/CentOS上的防火墙 

导出数据库生成SQL脚本
mysqldump -h 192.168.200.18  -u root -p TestDB > TestDB.sql

<2>、测试mysql守护进程。

#cd /usr/local/mysql/mysql-test ;

#perl mysql-test-run.pl

<3>、注意事项:

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:

/usr/local/mysql/bin/mysqladmin -u root password 'new-password'
/usr/local/mysql/bin/mysqladmin -u root -h localhost password 'new-password'

Alternatively you can run:
/usr/local/mysql/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
/usr/local/mysql ; /usr/local/mysql/bin/mysqld_safe &

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

Please report
any problems with the /usr/local/mysql/scripts/mysqlbug script!

参考:

mysql5.5.8安装问题解决方法

posted @ 2011-01-24 18:04 chenjunbiao 阅读(...) 评论(...) 编辑 收藏