1. 使用通用二进制分发包在Unix/Linux中安装MySQL
MySQL的二进制分发包扩展名为.tar.gz,名字格式一般为mysql-VERSION-OS.tar.gz,比如:mysql-5.6.11-linux-glibc2.5-i686.tar.gz是linux下32位的5.6.11版本的二进制分发包。注意区别二进制分发包和源文件分发包。
2. 安装准备
如果之前安装过MySQL,再次安装将产生问题。因此必须确保之前安装的MySQL已经被彻底移除(使用包管理系统),并且任何额外的文件,比如旧版本的数据文件,也要被移除。还应该检查配置文件,如/etc/my.cnf或者/etc/mysql目录,也要被移除。
xsj1987@ubuntu:~$ su
密码:
root@ubuntu:/home/xsj1987# apt-get autoremove --purge mysql-server-5.0
正在读取软件包列表... 完成
正在分析软件包的依赖关系树
正在读取状态信息... 完成
软件包 mysql-server-5.0 还未安装,因而不会被卸载
升级了 0 个软件包,新安装了 0 个软件包,要卸载 0 个软件包,有 515 个软件包未被升级。
root@ubuntu:/home/xsj1987# apt-get remove mysql-server
正在读取软件包列表... 完成
正在分析软件包的依赖关系树
正在读取状态信息... 完成
软件包 mysql-server 还未安装,因而不会被卸载
升级了 0 个软件包,新安装了 0 个软件包,要卸载 0 个软件包,有 515 个软件包未被升级。
root@ubuntu:/home/xsj1987# apt-get autoremove mysql-server
正在读取软件包列表... 完成
正在分析软件包的依赖关系树
正在读取状态信息... 完成
软件包 mysql-server 还未安装,因而不会被卸载
升级了 0 个软件包,新安装了 0 个软件包,要卸载 0 个软件包,有 515 个软件包未被升级。
root@ubuntu:/home/xsj1987# apt-get remove mysql-common
正在读取软件包列表... 完成
正在分析软件包的依赖关系树
正在读取状态信息... 完成
软件包 mysql-common 还未安装,因而不会被卸载
升级了 0 个软件包,新安装了 0 个软件包,要卸载 0 个软件包,有 515 个软件包未被升级。
root@ubuntu:/home/xsj1987# dpkg -l | grep ^rc | awk '{print $2}' | xargs dpkg -Pdpkg: --purge 需要至少一个软件包名作为参数
输入 dpkg --help 可获得安装和卸载软件包的有关帮助 [*];
使用“dselect”或是“aptitude”就能在友好的界面下管理软件包;
输入 dpkg -Dhelp 可看到 dpkg 除错标志的值的列表;
输入 dpkg --force-help 可获得所有强制操作选项的列表;
输入 dpkg-deb --help 可获得有关操作 *.deb 文件的帮助;
输入 dpkg --license 可阅读版权许可证和无担保的说明 (GNU GPL) [*]。
带有 [*] 的选项将会输出较大篇幅的文字 - 可使用管道将其输出连接到“less”或“more”!
3. 创建一个mysql用户和组
如果系统中还没有mysql组,则需要创建一个。名字不仅仅局限于mysql。由于新建的mysql用户仅仅起到所有权的作用,并不是用来登录用的,因此useradd命令使用-r选项来创建不具有登录权限的用户。省略-r选项将允许mysql用户执行登录操作。也可以完全不创建新的用户和组,完全使用当前登录的用户和组,之所以新创建mysql用户和组,优点是:MySQL安装之后的进程所属用户需要对一些目录和文件具有一定的所属权和访问权限,如果用当前用户,考虑到会不经意改变这些目录和文件的访问权限,从而影响了MySQL服务进程的正常运行。因此最好创建无登录权限的mysql用户和组。
root@ubuntu:/home/xsj1987# userdel mysql
userdel: user 'mysql' does not exist
root@ubuntu:/home/xsj1987# groupdel mysql
groupdel: group 'mysql' does not exist
root@ubuntu:/home/xsj1987# groupadd mysql
root@ubuntu:/home/xsj1987# useradd -r -g mysql mysql
4. 解压缩部署
root@ubuntu:/home/xsj1987# cd /usr/local/
root@ubuntu:/usr/local# tar -zxvf /media/UDISK/MySQL/mysql-5.6.11-linux-glibc2.5-i686.tar.gz
root@ubuntu:/usr/local# ln -s /usr/local/mysql-5.6.11-linux-glibc2.5-i686/ mysql
root@ubuntu:/usr/local# ls -l
lrwxrwxrwx 1 root root 44 2013-04-21 09:55 mysql -> /usr/local/mysql-5.6.11-linux-glibc2.5-i686/
root@ubuntu:/usr/local# cd ./mysql
root@ubuntu:/usr/local/mysql# ls -l
总用量 76
drwxr-xr-x 2 root root 4096 2013-04-21 09:53 bin
-rw-r--r-- 1 7161 uucp 17987 2013-04-05 05:27 COPYING
drwxr-xr-x 3 root root 4096 2013-04-21 09:53 data
drwxr-xr-x 2 root root 4096 2013-04-21 09:53 docs
drwxr-xr-x 3 root root 4096 2013-04-21 09:53 include
-rw-r--r-- 1 7161 uucp 7468 2013-04-05 05:27 INSTALL-BINARY
drwxr-xr-x 3 root root 4096 2013-04-21 09:53 lib
drwxr-xr-x 4 root root 4096 2013-04-21 09:53 man
drwxr-xr-x 10 root root 4096 2013-04-21 09:53 mysql-test
-rw-r--r-- 1 7161 uucp 2552 2013-04-05 05:27 README
drwxr-xr-x 2 root root 4096 2013-04-21 09:53 scripts
drwxr-xr-x 28 root root 4096 2013-04-21 09:53 share
drwxr-xr-x 4 root root 4096 2013-04-21 09:53 sql-bench
drwxr-xr-x 3 root root 4096 2013-04-21 09:53 support-files
进入MySQL安装位置的根目录。安装过程中最重要的目录是bin和scripts子目录。bin目录中包含客户端程序和服务器。需要添加此目录的完整路径到PATH环境变量以便shell能够方便地找到MySQL程序。scripts目录包含mysql_install_db脚本,用来安装包含有授权表的mysql数据库,其存储有服务器访问权限。
5. 访问权限设置
确保解压后的安装目录能被mysql用户访问,如果以root用户安装MySQL,必须变更拥有权为mysql用户。
root@ubuntu:/usr/local/mysql# chown -R mysql .
root@ubuntu:/usr/local/mysql# chgrp -R mysql .
root@ubuntu:/usr/local/mysql# ls -l
总用量 76
drwxr-xr-x 2 mysql mysql 4096 2013-04-21 09:53 bin
-rw-r--r-- 1 mysql mysql 17987 2013-04-05 05:27 COPYING
drwxr-xr-x 3 mysql mysql 4096 2013-04-21 09:53 data
drwxr-xr-x 2 mysql mysql 4096 2013-04-21 09:53 docs
drwxr-xr-x 3 mysql mysql 4096 2013-04-21 09:53 include
-rw-r--r-- 1 mysql mysql 7468 2013-04-05 05:27 INSTALL-BINARY
drwxr-xr-x 3 mysql mysql 4096 2013-04-21 09:53 lib
drwxr-xr-x 4 mysql mysql 4096 2013-04-21 09:53 man
drwxr-xr-x 10 mysql mysql 4096 2013-04-21 09:53 mysql-test
-rw-r--r-- 1 mysql mysql 2552 2013-04-05 05:27 README
drwxr-xr-x 2 mysql mysql 4096 2013-04-21 09:53 scripts
drwxr-xr-x 28 mysql mysql 4096 2013-04-21 09:53 share
drwxr-xr-x 4 mysql mysql 4096 2013-04-21 09:53 sql-bench
drwxr-xr-x 3 mysql mysql 4096 2013-04-21 09:53 support-files
6. 初始化授权表
安装MySQL后,必须初始化授权表(grant tables),启动服务,确保服务正常工作。需要设置服务在系统启动或停止时自动启动、停止。需要给授权表中的账户分配密码。授权表由mysql_install_db程序创建,对于其他安装方式一般都会被自动调用。
root@ubuntu:/usr/local/mysql# ./scripts/mysql_install_db --user=mysql
Installing MySQL system tables..../bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory
root@ubuntu:/usr/local/mysql# apt-get install libaio1
正在读取软件包列表... 完成
正在分析软件包的依赖关系树
正在读取状态信息... 完成
下列【新】软件包将被安装:
libaio1
升级了 0 个软件包,新安装了 1 个软件包,要卸载 0 个软件包,有 515 个软件包未被升级。
需要下载 9,512B 的软件包。
解压缩后会消耗掉 77.8kB 的额外空间。
获取:1 http://us.archive.ubuntu.com/ubuntu/ lucid/main libaio1 0.3.107-3ubuntu2 [9,512B]
下载 9,512B,耗时 1秒 (8,400B/s)
选中了曾被取消选择的软件包 libaio1。
(正在读取数据库 ... 系统当前总共安装有 126170 个文件和目录。)
正在解压缩 libaio1 (从 .../libaio1_0.3.107-3ubuntu2_i386.deb) ...
正在设置 libaio1 (0.3.107-3ubuntu2) ...
正在处理用于 libc-bin 的触发器...
ldconfig deferred processing now taking place
root@ubuntu:/usr/local/mysql# ./scripts/mysql_install_db --user=mysql
Installing MySQL system tables...2013-04-21 10:18:05 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2013-04-21 10:18:05 3974 [Note] InnoDB: The InnoDB memory heap is disabled
2013-04-21 10:18:05 3974 [Note] InnoDB: Mutexes and rw_locks use InnoDB's own implementation
2013-04-21 10:18:05 3974 [Note] InnoDB: Compressed tables use zlib 1.2.3
2013-04-21 10:18:05 3974 [Note] InnoDB: Using Linux native AIO
2013-04-21 10:18:05 3974 [Note] InnoDB: Not using CPU crc32 instructions
2013-04-21 10:18:05 3974 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2013-04-21 10:18:05 3974 [Note] InnoDB: Completed initialization of buffer pool
2013-04-21 10:18:05 3974 [Note] InnoDB: The first specified data file ./ibdata1 did not exist: a new database to be created!
2013-04-21 10:18:05 3974 [Note] InnoDB: Setting file ./ibdata1 size to 12 MB
2013-04-21 10:18:05 3974 [Note] InnoDB: Database physically writes the file full: wait...
2013-04-21 10:18:05 3974 [Note] InnoDB: Setting log file ./ib_logfile101 size to 48 MB
2013-04-21 10:18:06 3974 [Note] InnoDB: Setting log file ./ib_logfile1 size to 48 MB
2013-04-21 10:18:07 3974 [Note] InnoDB: Renaming log file ./ib_logfile101 to ./ib_logfile0
2013-04-21 10:18:07 3974 [Warning] InnoDB: New log files created, LSN=45781
2013-04-21 10:18:07 3974 [Note] InnoDB: Doublewrite buffer not found: creating new
2013-04-21 10:18:07 3974 [Note] InnoDB: Doublewrite buffer created
2013-04-21 10:18:07 3974 [Note] InnoDB: 128 rollback segment(s) are active.
2013-04-21 10:18:07 3974 [Warning] InnoDB: Creating foreign key constraint system tables.
2013-04-21 10:18:07 3974 [Note] InnoDB: Foreign key constraint system tables created
2013-04-21 10:18:07 3974 [Note] InnoDB: Creating tablespace and datafile system tables.
2013-04-21 10:18:07 3974 [Note] InnoDB: Tablespace and datafile system tables created.
2013-04-21 10:18:07 3974 [Note] InnoDB: Waiting for purge to start
2013-04-21 10:18:07 3974 [Note] InnoDB: 5.6.11 started; log sequence number 0
2013-04-21 10:18:08 3974 [Note] Binlog end
2013-04-21 10:18:08 3974 [Note] InnoDB: FTS optimize thread exiting.
2013-04-21 10:18:08 3974 [Note] InnoDB: Starting shutdown...
2013-04-21 10:18:09 3974 [Note] InnoDB: Shutdown completed; log sequence number 1625977
OK
Filling help tables...2013-04-21 10:18:09 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2013-04-21 10:18:09 3999 [Note] InnoDB: The InnoDB memory heap is disabled
2013-04-21 10:18:09 3999 [Note] InnoDB: Mutexes and rw_locks use InnoDB's own implementation
2013-04-21 10:18:09 3999 [Note] InnoDB: Compressed tables use zlib 1.2.3
2013-04-21 10:18:09 3999 [Note] InnoDB: Using Linux native AIO
2013-04-21 10:18:10 3999 [Note] InnoDB: Not using CPU crc32 instructions
2013-04-21 10:18:10 3999 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2013-04-21 10:18:10 3999 [Note] InnoDB: Completed initialization of buffer pool
2013-04-21 10:18:10 3999 [Note] InnoDB: Highest supported file format is Barracuda.
2013-04-21 10:18:10 3999 [Note] InnoDB: 128 rollback segment(s) are active.
2013-04-21 10:18:10 3999 [Note] InnoDB: Waiting for purge to start
2013-04-21 10:18:10 3999 [Note] InnoDB: 5.6.11 started; log sequence number 1625977
2013-04-21 10:18:10 3999 [Note] Binlog end
2013-04-21 10:18:10 3999 [Note] InnoDB: FTS optimize thread exiting.
2013-04-21 10:18:10 3999 [Note] InnoDB: Starting shutdown...
2013-04-21 10:18:12 3999 [Note] InnoDB: Shutdown completed; log sequence number 1625987
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 ubuntu 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!
The latest information about MySQL is available on the web at
http://www.mysql.com
Support MySQL by buying support/licenses at http://shop.mysql.com
WARNING: Found existing config file ./my.cnf on the system.
Because this file might be in use, it was not replaced,
but was used in bootstrap (unless you used --defaults-file)
and when you later start the server.
The new default config file was created as ./my-new.cnf,
please compare it with your file and take the changes you need.
运行mysql_install_db程序创建初始MySQL授权表,它包含了权限决定用户如何被允许连接到服务器。通常,mysql_install_db程序需要仅仅在第一次安装MySQL时执行,对于更新安装则没必要执行。mysql_install_db不会覆盖写入一个已经存在的权限表,所以在任何情形下执行都是安全的。有可能需要指定--basedir和--datadir选项,如果mysql_install_db不能识别争取的安装目录和数据目录。mysql_install_db脚本以mysql用户为属主(owner)创建服务器数据目录。在数据目录下,为mysql数据库创建目录来持有授权表以及为test数据库创建目录用来测试MySQL。这个脚本也创建权限表实体(entries)为root账户和多个匿名账户。这些账户初始情况下都没有密码。初始权限中允许MySQL的root账户做任何事情,允许任意账户创建或使用以test或test_开头命名的数据库。保障数据库目录和文件为mysql用户所拥有是重要的,以便服务器能拥有对他们的读写权限。为此,在以root用户身份执行mysql_install_db时,需要显式地添加--user选项,否则必须登录mysql用户,以mysql用户身份执行以省略--user选项。
7. 变更访问权限
大多数的MySQL的安装文件可以变更属主为root(非必要,if you like),但是data目录除外,必须属主为mysql用户。
root@ubuntu:/usr/local/mysql# chown -R root .
root@ubuntu:/usr/local/mysql# chown -R mysql ./data
root@ubuntu:/usr/local/mysql# ls -l
总用量 84
drwxr-xr-x 2 root mysql 4096 2013-04-21 09:53 bin
-rw-r--r-- 1 root mysql 17987 2013-04-05 05:27 COPYING
drwxr-xr-x 5 mysql mysql 4096 2013-04-21 10:18 data
drwxr-xr-x 2 root mysql 4096 2013-04-21 09:53 docs
drwxr-xr-x 3 root mysql 4096 2013-04-21 09:53 include
-rw-r--r-- 1 root mysql 7468 2013-04-05 05:27 INSTALL-BINARY
drwxr-xr-x 3 root mysql 4096 2013-04-21 09:53 lib
drwxr-xr-x 4 root mysql 4096 2013-04-21 09:53 man
-rw-r--r-- 1 root root 943 2013-04-21 10:03 my.cnf
-rw-r--r-- 1 root root 943 2013-04-21 10:18 my-new.cnf
drwxr-xr-x 10 root mysql 4096 2013-04-21 09:53 mysql-test
-rw-r--r-- 1 root mysql 2552 2013-04-05 05:27 README
drwxr-xr-x 2 root mysql 4096 2013-04-21 09:53 scripts
drwxr-xr-x 28 root mysql 4096 2013-04-21 09:53 share
drwxr-xr-x 4 root mysql 4096 2013-04-21 09:53 sql-bench
drwxr-xr-x 3 root mysql 4096 2013-04-21 09:53 support-files
8. 拷贝配置文件
可以将support-files目录中的配置文件拷贝为/etc/my.cnf文件,如果不拷贝,MySQL服务器将以默认的设置进行启动。
root@ubuntu:/usr/local/mysql# cp ./support-files/my-default.cnf /etc/my.cnf
9. 设置开机自启动
修改/etc/rc.local文件。
root@ubuntu:/home/xsj1987# gedit /etc/rc.local
在exit(0)语句之前添加如下语句:
# start MySQL automatically
/bin/sh -c 'cd /usr/local/mysql; ./bin/mysqld_safe --user=mysql &'
如果无法自动启动,需要考虑/bin/sh链接的bash和dash的兼容性问题。
10. 启动MySQL服务
root@ubuntu:/usr/local/mysql# ./bin/mysqld_safe --user=mysql &
[2] 4179
root@ubuntu:/usr/local/mysql# 130421 10:33:05 mysqld_safe Logging to '/usr/local/mysql/data/ubuntu.err'.
130421 10:33:05 mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/data
11. 使用mysqladmin来验证服务正常运行
root@ubuntu:/usr/local/mysql# ./bin/mysqladmin version
./bin/mysqladmin Ver 8.42 Distrib 5.6.11, for linux-glibc2.5 on i686
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Server version 5.6.11
Protocol version 10
Connection Localhost via UNIX socket
UNIX socket /tmp/mysql.sock
Uptime: 3 min 2 sec
Threads: 1 Questions: 2 Slow queries: 0 Opens: 67 Flush tables: 1 Open tables: 60 Queries per second avg: 0.010
12. 验证能够正常停止服务
root@ubuntu:/usr/local/mysql# ./bin/mysqladmin -u root shutdown
root@ubuntu:/usr/local/mysql# ./bin/mysqladmin version
./bin/mysqladmin: connect to server at 'localhost' failed
error: 'Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)'
Check that mysqld is running and that the socket: '/tmp/mysql.sock' exists!
13. 验证能够再次重新启动服务
root@ubuntu:/usr/local/mysql# ./bin/mysqld_safe --user=mysql &
[1] 4403
root@ubuntu:/usr/local/mysql# 130421 10:40:23 mysqld_safe Logging to '/usr/local/mysql/data/ubuntu.err'.
130421 10:40:23 mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/data
^C
root@ubuntu:/usr/local/mysql# ./bin/mysqladmin version
./bin/mysqladmin Ver 8.42 Distrib 5.6.11, for linux-glibc2.5 on i686
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Server version 5.6.11
Protocol version 10
Connection Localhost via UNIX socket
UNIX socket /tmp/mysql.sock
Uptime: 15 sec
Threads: 1 Questions: 2 Slow queries: 0 Opens: 67 Flush tables: 1 Open tables: 60 Queries per second avg: 0.133
14. 运行简单测试来验证能够从服务器获取信息
root@ubuntu:/usr/local/mysql# ./bin/mysqlshow
+--------------------+
| Databases |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
root@ubuntu:/usr/local/mysql# ./bin/mysqlshow mysql
Database: mysql
+---------------------------+
| Tables |
+---------------------------+
| columns_priv |
| db |
| event |
| func |
| general_log |
| help_category |
| help_keyword |
| help_relation |
| help_topic |
| innodb_index_stats |
| innodb_table_stats |
| ndb_binlog_index |
| plugin |
| proc |
| procs_priv |
| proxies_priv |
| servers |
| slave_master_info |
| slave_relay_log_info |
| slave_worker_info |
| slow_log |
| tables_priv |
| time_zone |
| time_zone_leap_second |
| time_zone_name |
| time_zone_transition |
| time_zone_transition_type |
| user |
+---------------------------+
root@ubuntu:/usr/local/mysql# ./bin/mysql -e "SELECT Host, Db, User FROM db" mysql
+------+---------+------+
| Host | Db | User |
+------+---------+------+
| % | test | |
| % | test\_% | |
+------+---------+------+
15. 连接MySQL服务器
初始条件下root账户的密码为空。
root@ubuntu:/usr/local/mysql# ./bin/mysql -u root
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.6.11 MySQL Community Server (GPL)
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)
16. 创建一个数据库
mysql> CREATE DATABASE xsj_test001;
Query OK, 1 row affected (0.00 sec)
mysql> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
| xsj_test001 |
+--------------------+
5 rows in set (0.00 sec)
17. 断开连接MySQL服务器
mysql> QUIT
Bye
root@ubuntu:/usr/local/mysql#
18. 配置PATH环境变量
root@ubuntu:/home/xsj1987# gedit /etc/profile
在umask 022语句之前添加如下语句:
#set mysql environment
MYSQL_HOME=/usr/local/mysql
PATH=$MYSQL_HOME/bin:$MYSQL_HOME/scripts:$PATH
export MYSQL_HOME PATH
从而可以在任意目录访问到MySQL客户端程序,如:mysqladmin、mysqld_safe。