2014年8月2日星期六

Linux_074:RHEL下安装MySQL数据库

运行环境:RHEL 6.5 + MySQL Community Server 5.6.20 + MySQL Workbench 6.1.7

1. 下载 MySQL Community Server 5.6.20 for Red Hat Enterprise Linux 6 / Oracle Linux 6 (x86, 64-bit), RPM Bundle
文件名称为MySQL-5.6.20-1.el6.x86_64.rpm-bundle.tar。

2. 解压缩 MySQL-5.6.20-1.el6.x86_64.rpm-bundle.tar
tar -xvf MySQL-5.6.20-1.el6.x86_64.rpm-bundle.tar

3. 以root身份按RPM方式安装MySQL
要按如下顺序安装第2步解压出来的rpm包:
(1)rpm -ivh MySQL-shared-compat-5.6.20-1.el6.x86_64.rpm # RHEL 兼容包

(2)rpm -ivh MySQL-server-5.6.20-1.el6.x86_64.rpm # MySQL服务端程序
安装此步时,会报出错误:
file /usr/share/mysql/czech/errmsg.sys from install of MySQL-server-5.6.20-1.el6.x86_64 conflicts with file from package mysql-libs-5.1.71-1.el6.x86_64
错误原因是因为RHEL 6.5 自带MySQL 5.1.71, 所以需要将其卸载:
rpm -e mysql-libs-5.1.71-1.el6.x86_64 --nodeps
再次执行安装命令,输出如下:
Preparing...                ########################################### [100%]
   1:MySQL-server           ########################################### [100%]
warning: user mysql does not exist - using root
warning: group mysql does not exist - using root
2014-08-02 17:44:35 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2014-08-02 17:44:35 3075 [Note] InnoDB: Using atomics to ref count buffer pool pages
2014-08-02 17:44:35 3075 [Note] InnoDB: The InnoDB memory heap is disabled
2014-08-02 17:44:35 3075 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2014-08-02 17:44:35 3075 [Note] InnoDB: Memory barrier is not used
2014-08-02 17:44:35 3075 [Note] InnoDB: Compressed tables use zlib 1.2.3
2014-08-02 17:44:35 3075 [Note] InnoDB: Using Linux native AIO
2014-08-02 17:44:35 3075 [Note] InnoDB: Not using CPU crc32 instructions
2014-08-02 17:44:35 3075 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2014-08-02 17:44:35 3075 [Note] InnoDB: Completed initialization of buffer pool
2014-08-02 17:44:35 3075 [Note] InnoDB: The first specified data file ./ibdata1 did not exist: a new database to be created!
2014-08-02 17:44:35 3075 [Note] InnoDB: Setting file ./ibdata1 size to 12 MB
2014-08-02 17:44:35 3075 [Note] InnoDB: Database physically writes the file full: wait...
2014-08-02 17:44:35 3075 [Note] InnoDB: Setting log file ./ib_logfile101 size to 48 MB
2014-08-02 17:44:35 3075 [Note] InnoDB: Setting log file ./ib_logfile1 size to 48 MB
2014-08-02 17:44:36 3075 [Note] InnoDB: Renaming log file ./ib_logfile101 to ./ib_logfile0
2014-08-02 17:44:36 3075 [Warning] InnoDB: New log files created, LSN=45781
2014-08-02 17:44:36 3075 [Note] InnoDB: Doublewrite buffer not found: creating new
2014-08-02 17:44:36 3075 [Note] InnoDB: Doublewrite buffer created
2014-08-02 17:44:36 3075 [Note] InnoDB: 128 rollback segment(s) are active.
2014-08-02 17:44:36 3075 [Warning] InnoDB: Creating foreign key constraint system tables.
2014-08-02 17:44:36 3075 [Note] InnoDB: Foreign key constraint system tables created
2014-08-02 17:44:36 3075 [Note] InnoDB: Creating tablespace and datafile system tables.
2014-08-02 17:44:36 3075 [Note] InnoDB: Tablespace and datafile system tables created.
2014-08-02 17:44:36 3075 [Note] InnoDB: Waiting for purge to start
2014-08-02 17:44:36 3075 [Note] InnoDB: 5.6.20 started; log sequence number 0
A random root password has been set. You will find it in '/root/.mysql_secret'.
2014-08-02 17:44:36 3075 [Note] Binlog end
2014-08-02 17:44:36 3075 [Note] InnoDB: FTS optimize thread exiting.
2014-08-02 17:44:36 3075 [Note] InnoDB: Starting shutdown...
2014-08-02 17:44:38 3075 [Note] InnoDB: Shutdown completed; log sequence number 1625977


2014-08-02 17:44:38 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2014-08-02 17:44:38 3097 [Note] InnoDB: Using atomics to ref count buffer pool pages
2014-08-02 17:44:38 3097 [Note] InnoDB: The InnoDB memory heap is disabled
2014-08-02 17:44:38 3097 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2014-08-02 17:44:38 3097 [Note] InnoDB: Memory barrier is not used
2014-08-02 17:44:38 3097 [Note] InnoDB: Compressed tables use zlib 1.2.3
2014-08-02 17:44:38 3097 [Note] InnoDB: Using Linux native AIO
2014-08-02 17:44:38 3097 [Note] InnoDB: Not using CPU crc32 instructions
2014-08-02 17:44:38 3097 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2014-08-02 17:44:38 3097 [Note] InnoDB: Completed initialization of buffer pool
2014-08-02 17:44:38 3097 [Note] InnoDB: Highest supported file format is Barracuda.
2014-08-02 17:44:38 3097 [Note] InnoDB: 128 rollback segment(s) are active.
2014-08-02 17:44:38 3097 [Note] InnoDB: Waiting for purge to start
2014-08-02 17:44:38 3097 [Note] InnoDB: 5.6.20 started; log sequence number 1625977
2014-08-02 17:44:38 3097 [Note] Binlog end
2014-08-02 17:44:38 3097 [Note] InnoDB: FTS optimize thread exiting.
2014-08-02 17:44:38 3097 [Note] InnoDB: Starting shutdown...
2014-08-02 17:44:39 3097 [Note] InnoDB: Shutdown completed; log sequence number 1625987

A RANDOM PASSWORD HAS BEEN SET FOR THE MySQL root USER !
You will find that password in '/root/.mysql_secret'.

You must change that password on your first connect,
no other statement but 'SET PASSWORD' will be accepted.
See the manual for the semantics of the 'password expired' flag.

Also, the account for the anonymous user has been removed.

In addition, you can run:

  /usr/bin/mysql_secure_installation

which will also give you the option of removing the test database.
This is strongly recommended for production servers.

See the manual for more instructions.

Please report any problems at http://bugs.mysql.com/

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

New default config file was created as /usr/my.cnf and
will be used by default by the server when you start it.
You may edit this file to change server settings


(3)rpm -ivh MySQL-client-5.6.20-1.el6.x86_64.rpm # MySQL客户端程序
(4)rpm -ivh MySQL-devel-5.6.20-1.el6.x86_64.rpm # MySQL的库和头文件
(5)rpm -ivh MySQL-shared-5.6.20-1.el6.x86_64.rpm # MySQL的共享库

4. 设置MySQL
(1)查看临时生成的MySQL的root密码:# cat /root/.mysql_secret
# The random password set for the root user at Sat Aug  2 17:44:36 2014 (local time): u949oNxlwhrl1eC3
(2)启动MySQL:# service mysql start
Starting MySQL.                                            [确定]
(3)用root账户连接MySQL:# mysql -uroot -p
Enter password: //这里输入临时生成的root密码
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.20

Copyright (c) 2000, 2014, 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> SET PASSWORD FOR root@localhost=PASSWORD('welcome1'); // 修改root密码为welcome1
Query OK, 0 rows affected (0.00 sec)

mysql> quit
Bye

(4)重启MySQL:# service mysql restart

5.  下载 MySQL Workbench 6.1.7 for Red Hat Enterprise Linux 6 / Oracle Linux 6 (x86, 64-bit), RPM Package
文件名称为mysql-workbench-community-6.1.7-1.el6.x86_64.rpm。

6. 以root身份按RPM方式安装MySQL Workbench

直接执行rpm -ivh mysql-workbench-community-6.1.7-1.el6.x86_64.rpm,会报以下错误:
error: Failed dependencies:
    libtinyxml.so.0()(64bit) is needed by mysql-workbench-community-6.1.7-1.el6.x86_64
    libzip.so.1()(64bit) is needed by mysql-workbench-community-6.1.7-1.el6.x86_64

分别从http://pkgs.org/altlinux-sisyphus/classic-x86_64/tinyxml-2.6.1-alt1.x86_64.rpm.html和
http://pkgs.org/altlinux-sisyphus/classic-x86_64/tinyxml-2.6.1-alt1.x86_64.rpm.html中找到
(1)# rpm -ivh tinyxml-2.6.1-alt1.x86_64.rpm
(2)# rpm -ivh libzip-0.9-3.1.el6.x86_64.rpm
再执行rpm -ivh mysql-workbench-community-6.1.7-1.el6.x86_64.rpm,错误消失,安装成功。

(3)Workbench可以从应用程序->编程启动。

7. 远程连接MySQL数据库

7.1 允许远程使用root账户连接
(1)在安装MySQL的机器上运行:# mysql -uroot -p
(2)连接mysql数据库:mysql> use mysql
(3)查看user表:mysql>  select host,user,password from user;
+-----------------------+------+-------------------------------------------+
| host                  | user | password                                  |
+-----------------------+------+-------------------------------------------+
| localhost             | root | *1E97552497867D450D5C30F3441CD95BADF54751 |
| localhost.localdomain | root | *B7FA491461C8ED3A47025834E5078D71936B9BC1 |
| 127.0.0.1             | root | *B7FA491461C8ED3A47025834E5078D71936B9BC1 |
| ::1                   | root | *B7FA491461C8ED3A47025834E5078D71936B9BC1 |
+-----------------------+------+-------------------------------------------+
(4)更新user表:mysql> update user set host = ’%’ where user = ’root’;
ERROR 1062 (23000): Duplicate entry '%-root' for key 'PRIMARY'
不用理会上面的错误。
(5)再次查看user表:mysql>  select host,user,password from user;
+-----------------------+------+-------------------------------------------+
| host                  | user | password                                  |
+-----------------------+------+-------------------------------------------+
| %                     | root | *1E97552497867D450D5C30F3441CD95BADF54751 |
| localhost.localdomain | root | *B7FA491461C8ED3A47025834E5078D71936B9BC1 |
| 127.0.0.1             | root | *B7FA491461C8ED3A47025834E5078D71936B9BC1 |
| ::1                   | root | *B7FA491461C8ED3A47025834E5078D71936B9BC1 |
+-----------------------+------+-------------------------------------------
(6)mysql> flush privileges;
现在可以远程使用root账户连接了:mysql -h 192.168.0.111 -u root -p

7.2 创建其它账户,并允许远程连接
(1)在安装MySQL的机器上运行:# mysql -uroot -p
(2)创建数据库:create database demodb; 
(3)创建demo账户:create user demo identified by 'demo';
(4)赋予权限:grant all privileges on demodb.* to 'demo'@'%' with grant option;
(5)flush privileges;

参考文献:
1. http://www.linuxidc.com/Linux/2013-12/93507.htm
2. http://happy-xp.blog.163.com/blog/static/2324170201203035925942/
3. http://www.cnblogs.com/cnblogsfans/archive/2009/09/21/1570942.html
4. http://www.jb51.net/article/31902.htm

没有评论: