一、准备工作
注:由于是在一台已经存在并经过多方使用的服务器上安装,所以需要先确认下该台服务器上是否已经安装Mysql
1、groupadd mysql 时提示 mysql 已存在,useradd -g mysql mysql 时也提示已存在,基本上可以确认已经安装过mysql;
切换到mysql下查看mysql进程,结果显示没有则表示当前 mysql 未启动;
输入 mysql start 时出现错误提示:ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2), 先不研究这个错误了,准备重新安装mysql。
输入 rpm -qa|grep mysql ,无任何信息显示,表明之前不是以这种方式安装的,则不需要卸载,直接进行新的安装。
2、系统环境确认:
输入命令: uname -a 得到信息如下: Linux localhost.localdomain 2.6.9-55.ELsmp #1 SMP Fri Apr 20 17:03:35 EDT 2007 i686 i686 i386 GNU/Linux ,表明系统是32位;
3、下载Mysql 安装包:
从官网(http://www.mysql.com/downloads/mysql/ )下载相应的安装包(我的是 mysql-5.1.51-linux-i686-glibc23.tar.gz);
二、安装
1、新建 mysql 组和用户(已建,略过);
2、用 root 用户给 mysql 安装路径授权,依次执行:
MYSQL_BASE=/opt/mysql mkdir -p $MYSQL_BASE/product/5.1 mkdir -p $MYSQL_BASE/mysqldata/mysqllog mkdir -p /var/run/mysqld/ mkdir -p /etc/mysql chmod -R 777 $MYSQL_BASE chown -R mysql:mysql $MYSQL_BASE chown -R mysql:mysql /etc/mysql chown -R mysql:mysql /var/run/mysqld/
注:以上路径是我准备安装 mysql 的路径,部分路径与实际环境相关
3、切换到mysql 用户,设置 mysql 用户环境变量:编辑 /home/mysql/.bash_profile 文件,增加如下内容:
export MYSQL_BASE=/opt/mysql export BASEDIR=$MYSQL_BASE/product/5.1 export DATADIR=$MYSQL_BASE/mysqldata export LD_LIBRARY_PATH=$BASEDIR/lib:/lib:/usr/lib:/usr/local/lib export TMPDIR=/tmp export PATH=$PATH:/usr/bin:/bin:/usr/bin/X11:/usr/local/bin export PATH=$PATH:$BASEDIR/bin:$MYSQL_BASE:$BASEDIR:$LD_LIBRARY_PATH
4、将安装包上传到 mysql 用户主目录下,依次执行:
cd $BASEDIR tar -zxvf /home/mysql/mysql-5.1.51-linux-i686-glibc23.tar.gz cd $BASEDIR cd mysql-5.1.51-linux-i686-glibc23 mv * ../ cd .. rm -r mysql-5.1.51-linux-i686-glibc23
5、开始安装 mysql:
1) 执行:
cd $BASEDIR ./scripts/mysql_install_db --basedir=$BASEDIR --datadir=$DATADIR --user=mysql
出现类似如下文字则表示成功:
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: /opt/mysql/product/5.1/bin/mysqladmin -u root password 'new-password' /opt/mysql/product/5.1/bin/mysqladmin -u root -h localhost.localdomain password 'new-password' Alternatively you can run: /opt/mysql/product/5.1/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 /opt/mysql/product/5.1 ; /opt/mysql/product/5.1/bin/mysqld_safe & You can test the MySQL daemon with mysql-test-run.pl cd /opt/mysql/product/5.1/mysql-test ; perl mysql-test-run.pl Please report any problems with the /opt/mysql/product/5.1/scripts/mysqlbug script!
2) 切换到 root 用户, 依次执行:
cd /opt/mysql/product/5.1 cp ./support-files/my-medium.cnf /etc/my.cnf cp ./support-files/mysql.server /etc/init.d/mysql chkconfig --add mysql chkconfig --level 345 mysql on
3) 编辑 /etc/my.cnf 文件,找到如下片段:
[mysqld] port = 3306 socket = /tmp/mysql.sock skip-locking key_buffer_size = 16M max_allowed_packet = 1M table_open_cache = 64 sort_buffer_size = 512K net_buffer_length = 8K read_buffer_size = 256K read_rnd_buffer_size = 512K myisam_sort_buffer_size = 8M
在下面增加:
user = mysql pid-file = /var/run/mysqld/mysqld.pid basedir = /opt/mysql/product/5.1 datadir = /opt/mysql/mysqldata lower_case_table_names = 1 default-storage-engine = INNODB innodb_additional_mem_pool_size = 16M innodb_buffer_pool_size = 2G innodb_data_file_path = ibdata1:1000M:autoextend innodb_autoextend_increment = 64 innodb_file_per_table = 1 innodb_data_home_dir = /opt/mysql/mysqldata innodb_file_io_threads = 4 innodb_thread_concurrency = 16 innodb_flush_log_at_trx_commit = 2 innodb_log_buffer_size = 8M innodb_log_file_size = 512M innodb_log_files_in_group = 3 innodb_max_dirty_pages_pct = 40 innodb_lock_wait_timeout = 120 innodb_locks_unsafe_for_binlog = 1 innodb_autoinc_lock_mode = 2
修改 [client] 和 [mysqld] 下的 socket 那行为:
socket = /var/run/mysqld/mysqld.sock
注:Linux环境是区分大小写的,所以要加 lower_case_table_names = 1 这个配置
about lower_case_table_names:
0 - table names are stored as specified and comparisons are case sensitive.
1 - table names are stored in lowercase on disk and comparisons are not case sensitive.
2 - table names are stored as given but compared in lowercase. This option also applies to database names and table aliases.
4) 启动mysql, 用 mysql 用户执行: /etc/init.d/mysql start 显示:
Starting MySQL........................................................[ 确定 ]
则表示启动成功。
执行 ps ux 进行查看,有类似如下进程:
/opt/mysql/product/5.1/bin/mysqld --basedir=/opt/mysql/product/5.1 --datadir=/opt/mysql/mysqldata --user=mysql2 --log-error=/opt/mysql/mysqldata/localhost.localdomain.err --open-files-limit=8192 --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/run/mysqld/mysqld.sock --port=3306
如需关闭数据库,则执行: /etc/init.d/mysql stop
三、初始设置
1、设置数据库root用户密码:
/opt/mysql/product/5.1/bin/mysqladmin -u root password 'new-password'
/opt/mysql/product/5.1/bin/mysqladmin -u root -p -h localhost.localdomain password 'new-password'
四、创建数据库
1、新建数据库用户 test ,密码也是 test,依次执行:
mysql -u root -p mysql> insert into mysql.user(Host,User,Password) values("localhost","test",password("test")); mysql> flush privileges; mysql> exit;
2、创建数据库 testdb,并赋予 test 用户操作修改该数据库的权限,依次执行:
mysql -u root -p mysql> create database testdb default character set utf8; mysql> grant all privileges on testdb.* to test@localhost identified by 'test'; mysql> flush privileges; mysql> exit;
3、用 test 用户登录,创建数据表等:
mysql -u test -p mysql> use testdb; ...
至此,基本完成。