• Archive by category "数据库"

Blog Archives

在Ubuntu中安装HBase

R利剑NoSQL系列文章,主要介绍通过R语言连接使用nosql数据库。涉及的NoSQL产品,包括RedisMongoDBHBaseHiveCassandraNeo4j。希望通过我的介绍让广大的R语言爱好者,有更多的开发选择,做出更多地激动人心的应用。

关于作者:

  • 张丹(Conan), 程序员Java,R,PHP,Javascript
  • weibo:@Conan_Z
  • blog: http://blog.fens.me
  • email: bsspirit@gmail.com

转载请注明出处:
http://blog.fens.me/linux-hbase-install/

linux-hbase

前言

HBase是Hadoop家族中的一个分布式数据库产品,HBase支持高并发读写,列式数据存储,高效的索引,自动分片,自动Region迁移等许多优点,已经越来越多的被界业认可并实施。

目录

  1. 在Ubuntu中环境准备
  2. HBase安装
  3. Thrift安装

1 在Ubuntu中环境准备

HBase是基于Java开发的运行Hadoop平台上分布式NoSQL数据库软件,HBase没有提供Windows系统安装版本。我在这里也只介绍HBase在Linux Ubuntu系统中的安装。

由于HBase是运行在Hadoop平台上面的,因此我们需要先安装好Hadoop的环境,Hadoop的安装请参考文章:[Hadoop历史版本安装](http://blog.fens.me/hadoop-history-source-install/)

HBase没有提供apt的软件源安装,我们需要自己去官方网络下载HBase软件包进行安装。HBase下载页:http://www.apache.org/dyn/closer.cgi/hbase/

系统环境:

  • Linux Ubuntu 12.04.2 LTS 64bit server
  • Java JDK 1.6.0_45
  • Hadoop 1.1.2

2 HBase安装

2.1 下载HBase


# 通过wget命令下载
~ wget http://www.gaidso.com/apache/hbase/stable/hbase-0.94.18.tar.gz

# 解压HBase
~ tar xvf hbase-0.94.18.tar.gz

# 移动HBase目录到文件夹
~ mv hbase-0.94.18/ /home/conan/hadoop/

# 进入目录
~ cd /home/conan/hadoop/hbase-0.94.18

2.2 配置HBase

2.2.1 修改启动文件hbase-env.sh


~ vi conf/hbase-env.sh

#打开注释
export JAVA_HOME=/home/conan/toolkit/jdk16
export HBASE_CLASSPATH=/home/conan/hadoop/hadoop-1.1.2/conf
export HBASE_MANAGES_ZK=true

2.2.2 修改配置文件 hbase-site.xml


~ vi conf/hbase-site.xml

<configuration>
<property>
<name>hbase.rootdir</name>
<value>hdfs://master:9000/hbase</value>
</property>

<property>
<name>hbase.cluster.distributed</name>
<value>true</value>
</property>

<property>
<name>dfs.replication</name>
<value>1</value>
</property>

<property>
<name>hbase.zookeeper.quorum</name>
<value>master</value>
</property>

<property>
<name>hbase.zookeeper.property.clientPort</name>
<value>2181</value>
</property>

<property>
<name>hbase.zookeeper.property.dataDir</name>
<value>/home/conan/hadoop/hdata</value>
</property>
</configuration>

复制hadoop环境的配置文件和类库


~ cp ~/hadoop/hadoop-1.1.2/conf/hdfs-site.xml conf/
~ cp ~/hadoop/hadoop-1.1.2/hadoop-core-1.1.2.jar lib/
~ mkdir /home/conan/hadoop/hdata

2.3 启动hadoop和hbase


~ /home/conan/hadoop/hadoop-1.1.2/bin/start-all.sh
~ /home/conan/hadoop/hbase-0.94.18/bin/start-hbase.sh

# 查看hbase进程
~ jps
13838 TaskTracker
13541 JobTracker
15946 HMaster
16756 Jps
12851 NameNode
13450 SecondaryNameNode
13133 DataNode
15817 HQuorumPeer
16283 HRegionServer

2.4 打开HBase命令行客户端访问Hbase


~ bin/hbase shell
HBase Shell; enter 'help' for list of supported commands.
Type "exit" to leave the HBase Shell
Version 0.94.18, r1577788, Sat Mar 15 04:46:47 UTC 2014

hbase(main):002:0> help
HBase Shell, version 0.94.18, r1577788, Sat Mar 15 04:46:47 UTC 2014
Type 'help "COMMAND"', (e.g. 'help "get"' -- the quotes are necessary) for help on a specific command.
Commands are grouped. Type 'help "COMMAND_GROUP"', (e.g. 'help "general"') for help on a command group.

COMMAND GROUPS:
  Group name: general
  Commands: status, version, whoami

  Group name: ddl
  Commands: alter, alter_async, alter_status, create, describe, disable, disable_all, drop, drop_all, enable, enable_all, exists, is_disabled, is_enabled, list, show_filters

  Group name: dml
  Commands: count, delete, deleteall, get, get_counter, incr, put, scan, truncate

  Group name: tools
  Commands: assign, balance_switch, balancer, close_region, compact, flush, hlog_roll, major_compact, move, split, unassign, zk_dump

  Group name: replication
  Commands: add_peer, disable_peer, enable_peer, list_peers, list_replicated_tables, remove_peer, start_replication, stop_replication

  Group name: snapshot
  Commands: clone_snapshot, delete_snapshot, list_snapshots, restore_snapshot, snapshot

  Group name: security
  Commands: grant, revoke, user_permission

SHELL USAGE:
Quote all names in HBase Shell such as table and column names.  Commas delimit
command parameters.  Type  after entering a command to run it.
Dictionaries of configuration used in the creation and alteration of tables are
Ruby Hashes. They look like this:

  {'key1' => 'value1', 'key2' => 'value2', ...}

and are opened and closed with curley-braces.  Key/values are delimited by the
'=>' character combination.  Usually keys are predefined constants such as
NAME, VERSIONS, COMPRESSION, etc.  Constants do not need to be quoted.  Type
'Object.constants' to see a (messy) list of all constants in the environment.

If you are using binary keys or values and need to enter them in the shell, use
double-quote'd hexadecimal representation. For example:

  hbase> get 't1', "key\x03\x3f\xcd"
  hbase> get 't1', "key\003\023\011"
  hbase> put 't1', "test\xef\xff", 'f1:', "\x01\x33\x40"

The HBase shell is the (J)Ruby IRB with the above HBase-specific commands added.
For more on the HBase Shell, see http://hbase.apache.org/docs/current/book.html

2.5 HBase简单命令操作


#创建一个新表student
hbase(main):003:0> create 'student','info'
0 row(s) in 1.2680 seconds

#查看所有的表
hbase(main):004:0> list
TABLE
student
1 row(s) in 0.0330 seconds

#查看student的表结构
hbase(main):005:0> describe 'student'
DESCRIPTION                                                 ENABLED
 'student', {NAME => 'info', DATA_BLOCK_ENCODING => 'NONE', true
  BLOOMFILTER => 'NONE', REPLICATION_SCOPE => '0', VERSIONS
  => '3', COMPRESSION => 'NONE', MIN_VERSIONS => '0', TTL =
 > '2147483647', KEEP_DELETED_CELLS => 'false', BLOCKSIZE =
 > '65536', IN_MEMORY => 'false', ENCODE_ON_DISK => 'true',
  BLOCKCACHE => 'true'}
1 row(s) in 0.1100 seconds

#同student表中插入一条数据
hbase(main):007:0> put 'student','mary','info:age','19'
0 row(s) in 0.0490 seconds

#从student表中取出mary的数据
hbase(main):008:0> get 'student','mary'
COLUMN                   CELL
 info:age                timestamp=1396366643298, value=19
1 row(s) in 0.0190 seconds

#让student表失效
hbase(main):009:0> disable 'student'
0 row(s) in 1.2400 seconds

#列出所有表
hbase(main):010:0> list
TABLE
student
1 row(s) in 0.0310 seconds

#删除student表
hbase(main):013:0>  drop 'student'
0 row(s) in 1.1100 seconds

#列出所有表
hbase(main):014:0> list
TABLE
0 row(s) in 0.0400 seconds

3 Thrift安装

安装完成HBase后,我们还需要安装Thrift,因为其他语言调用HBase的时候,是通过Thrift连接的。

Thrift是需要本地编译的,官方没有提供二进制安装包,首先下载thrift-0.9.1,Thrift下载页:http://thrift.apache.org/download

3.1 下载thrift

下载Thrift有两种方式,直接下载源代码发行包,或者通过git下载源代码,请选择其中一种方式下载。

3.1.1 直接下载源代码发行包 thrift-0.9.1.tar.gz


~ wget http://apache.fayea.com/apache-mirror/thrift/0.9.1/thrift-0.9.1.tar.gz
~ tar xvf thrift-0.9.1.tar.gz
~ mv thrift-0.9.1/ /home/conan/hadoop/
~ cd /home/conan/hadoop/

注:后文中的各种错误,都是这个包引起的

3.1.2 通过git下载源代码


~ git clone https://git-wip-us.apache.org/repos/asf/thrift.git thrift-git
~ mv thrift-git/ /home/conan/hadoop/
~ cd /home/conan/hadoop/

为了避免各种出错,建议使用git下载源代码安装

3.2 通过thrift-0.9.1.tar.gz 发行包安装Thrift

Thrift是需要本地编译的,在Thrift解压目录输入./configure,会列Thrift在当前机器所支持的语言环境。

3.2.1 安装Thrift的依赖包


sudo apt-get install libboost-dev libboost-test-dev libboost-program-options-dev libevent-dev automake libtool flex bison pkg-config g++ libssl-dev

如果只是为了连接rhbase,默认配置就可以了。如果除了希望支持rhbase访问,还支持PHP,Python,C++等语言的访问,就需要在系统中,装一些额外的类库。大家可以根据自己的要求,安装对应的软件包并设置Thrift的编译参数。

生成配置脚本


~  ./configure

//省略部分日志输出

thrift 0.9.1

Building C++ Library ......... : yes
Building C (GLib) Library .... : no
Building Java Library ........ : yes
Building C# Library .......... : no
Building Python Library ...... : yes
Building Ruby Library ........ : no
Building Haskell Library ..... : no
Building Perl Library ........ : no
Building PHP Library ......... : no
Building Erlang Library ...... : no
Building Go Library .......... : no
Building D Library ........... : no

C++ Library:
   Build TZlibTransport ...... : yes
   Build TNonblockingServer .. : yes
   Build TQTcpServer (Qt) .... : no

Java Library:
   Using javac ............... : javac
   Using java ................ : java
   Using ant ................. : /home/conan/toolkit/ant184/bin/ant

Python Library:
   Using Python .............. : /usr/bin/python

If something is missing that you think should be present,
please skim the output of configure to find the missing
component.  Details are present in config.log.

我本机的已支持C++, Java与Thrift的通信。

3.2.2 增加Python语言的通信

虽然Python已被显示支持与Thrift但在后面编译过程中,还是缺少一些Python的库,我们需要再增加Python的依赖库

安装Python的依赖包


sudo apt-get install python-all python-all-dev python-all-dbg

3.2.3 增加PHP语言的通信

安装PHP的依赖包


sudo apt-get install php5-dev php5-cli phpunit

生成配置脚本


~  ./configure --enable-thrift_protocol

//省略部分日志输出

thrift 0.9.1

Building C++ Library ......... : yes
Building C (GLib) Library .... : no
Building Java Library ........ : yes
Building C# Library .......... : no
Building Python Library ...... : yes
Building Ruby Library ........ : no
Building Haskell Library ..... : no
Building Perl Library ........ : no
Building PHP Library ......... : yes
Building Erlang Library ...... : no
Building Go Library .......... : no
Building D Library ........... : no

C++ Library:
   Build TZlibTransport ...... : yes
   Build TNonblockingServer .. : yes
   Build TQTcpServer (Qt) .... : no

Java Library:
   Using javac ............... : javac
   Using java ................ : java
   Using ant ................. : /home/conan/toolkit/ant184/bin/ant

Python Library:
   Using Python .............. : /usr/bin/python

PHP Library:
   Using php-config .......... : /usr/bin/php-config

If something is missing that you think should be present,
please skim the output of configure to find the missing
component.  Details are present in config.log.

我们看到Thrift的配置中,增加了对PHP语言的支持。

3.2.4 编译和安装


# 编译Thrift
~ make

//省略部分日志

make[5]: 正在进入目录 `/home/conan/hadoop/thrift-0.9.1/lib/php/src/ext/thrift_protocol'
make[5]: *** 没有指明目标并且找不到 makefile。 停止。
make[5]:正在离开目录 `/home/conan/hadoop/thrift-0.9.1/lib/php/src/ext/thrift_protocol'
make[4]: *** [src/ext/thrift_protocol/modules/thrift_protocol.so] 错误 2
make[4]:正在离开目录 `/home/conan/hadoop/thrift-0.9.1/lib/php'
make[3]: *** [all-recursive] 错误 1
make[3]:正在离开目录 `/home/conan/hadoop/thrift-0.9.1/lib/php'
make[2]: *** [all-recursive] 错误 1
make[2]:正在离开目录 `/home/conan/hadoop/thrift-0.9.1/lib'
make[1]: *** [all-recursive] 错误 1
make[1]:正在离开目录 `/home/conan/hadoop/thrift-0.9.1'
make: *** [all] 错误 2

在make生成过程,出现PHP的编译错误。从Thrift的错误列表中,我们可以找到错误描述( https://issues.apache.org/jira/browse/THRIFT-2265 ),这是由于Thrift-0.9.1发行包,打包时缺少了PHP扩展文件造成的错误,并在Thrift-0.9.2版本中修复。

所以,我们如果还想继续使用Thrift-0.9.1版本,则不能支持PHP语言。


# 生成配置信息,不包括PHP模块
~ ./configure --without-php_extension

# 编译Thrift
~ make

编译过程中,又出现了C++编译错误。


Makefile:832: 警告:覆盖关于目标“gen-cpp/ThriftTest.cpp”的命令
Makefile:829: 警告:忽略关于目标“gen-cpp/ThriftTest.cpp”的旧命令
/bin/bash ../../libtool --tag=CXX   --mode=link g++ -Wall -g -O2 -L/usr/lib   -o libtestgencpp.la  ThriftTest_constants.lo ThriftTest_types.lo ../../lib/cpp/libthrift.la -lssl -lcrypto -lrt -lpthread
libtool: link: ar cru .libs/libtestgencpp.a .libs/ThriftTest_constants.o .libs/ThriftTest_types.o
ar: .libs/ThriftTest_constants.o: No such file or directory
make[3]: *** [libtestgencpp.la] 错误 1
make[3]:正在离开目录 `/home/conan/hadoop/thrift-0.9.1/test/cpp'
make[2]: *** [all-recursive] 错误 1
make[2]:正在离开目录 `/home/conan/hadoop/thrift-0.9.1/test'
make[1]: *** [all-recursive] 错误 1
make[1]:正在离开目录 `/home/conan/hadoop/thrift-0.9.1'
make: *** [all] 错误 2

对于上面的2个编译错误,我决定换成git源代码的版本重新操作。

3.2 通过git源代码安装Thrift

运行安装命令


# 进行thrift-git目录
~ cd /home/conan/hadoop/thrift-git

# 复制0.9.1标签到新分支thrift-0.9.1
~ git checkout -b thrift-0.9.1 0.9.1

# 产生配置脚本
~ ./bootstrap.sh

# 生成配置信息
~ ./configure

# 编译Thrift
~ make

# 安装Thrift
~ sudo make install

走了许多弯路,终于使用git源代码版本安装好了Thrift。

查看thrift版本


~ thrift -version
Thrift version 0.9.1

接下来,我们启动HBase的Thrift Server服务


# 启动HBase的Thrift服务
~ /home/conan/hadoop/hbase-0.94.18/bin/hbase-daemon.sh start thrift
starting thrift, logging to /home/conan/hadoop/hbase-0.94.18/bin/../logs/hbase-conan-thrift-master.out

# 检查系统进程
~ jps
13838 TaskTracker
13541 JobTracker
15946 HMaster
32120 Jps
12851 NameNode
13450 SecondaryNameNode
13133 DataNode
32001 ThriftServer
15817 HQuorumPeer
16283 HRegionServer

我们看到ThriftServer已被启动,后面我们就可以使用多种语言,通过Thrift来访问HBase了,这样就完成了HBase的安装。

转载请注明出处:
http://blog.fens.me/linux-hbase-install/

打赏作者

在Ubuntu中安装MongoDB

MongoDB部署实验系列文章,MongoDB做为NoSQL数据库,最近几年持续升温,越来越多的企业都开始尝试用MongoDB代替原有Database做一些事情。MongoDB也在集群,分片,复制上也有相当不错的的表现。我通过将做各种MongoDB的部署实验进行介绍。

关于作者:

  • 张丹(Conan), 程序员Java,R,PHP,Javascript
  • weibo:@Conan_Z
  • blog: http://blog.fens.me
  • email: bsspirit@gmail.com

转载请注明出处:
http://blog.fens.me/linux-mongodb-install/

linux-mongo-install

前言

MongoDB作为一种文档型的NoSQL数据库,使用起来非常灵活,回避了关系型数据库前期的复杂数据库设计。MongoDB存储基于JSON格式,同时用Javascript做为数据库操作语言,给了使用者无限想象的空间,可以通过编程在MongoDB服务器中解决非常复杂的条件查询的问题。

目录

  1. MongoDB在Windows中安装
  2. MongoDB在Linux Ubuntu中安装
  3. 通过命令行客户端访问MongoDB

1 MongoDB在Windows中安装

在Windows系统上安装MongoDB数据库是件非常简单的事情,下载可执行安装文件(exe),双击安装即可。下载地址:http://www.mongodb.org/downloads

  • MongoDB服务器运行命令:MongoDB安装目录/bin/mongod.exe
  • MongoDB客户端运行命令:MongoDB安装目录/bin/mongo.exe

2 MongoDB在Linux Ubuntu中安装

本文使用的Linux是Ubuntu 12.04.2 LTS 64bit的系统,安装MongoDB数据库软件包可以通过apt-get实现。但我们修要安装官方提供MongoDB软件源。

修改apt的source.list文件,增加10gen的设置。


# 下载密钥文件
~  sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
Executing: gpg --ignore-time-conflict --no-options --no-default-keyring --secret-keyring /tmp/tmp.kVFab9XYw0 --trustdb-name /etc/apt/trustdb.gpg --keyring /etc/apt/trusted.gpg --primary-keyring /etc/apt/trusted.gpg --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
gpg: 下载密钥‘7F0CEB10’,从 hkp 服务器 keyserver.ubuntu.com
gpg: 密钥 7F0CEB10:公钥“Richard Kreuter ”已导入
gpg: 没有找到任何绝对信任的密钥
gpg: 合计被处理的数量:1
gpg:               已导入:1  (RSA: 1)

# 在source.list中增加MongoDB源的配置
~ echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | sudo tee /etc/apt/sources.list.d/mongodb.list
deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen

# 更新软件源
~ sudo apt-get update

在Linux Ubuntu中安装MongoDB数据库


#安装MongoDB服务器端
~ sudo apt-get install mongodb-10gen

安装完成后,MongoDB服务器会自动启动,我们检查MongoDB服务器程序


# 检查MongoDB服务器系统进程
~  ps -aux|grep mongo
mongodb   6870  3.7  0.4 349208 39740 ?        Ssl  10:27   2:23 /usr/bin/mongod --config /etc/mongodb.conf

# 通过启动命令检查MongoDB服务器状态
~  netstat -nlt|grep 27017
tcp        0      0 0.0.0.0:27017           0.0.0.0:*               LISTEN

# 通过启动命令检查MongoDB服务器状态
~ sudo /etc/init.d/mongodb status
Rather than invoking init scripts through /etc/init.d, use the service(8)
utility, e.g. service mongodb status

Since the script you are attempting to invoke has been converted to an
Upstart job, you may also use the status(8) utility, e.g. status mongodb
mongodb start/running, process 6870

# 通过系统服务检查MongoDB服务器状态
~ sudo service mongodb status
mongodb start/running, process 6870

通过web的控制台,查看MongoDB服务器的状态。在浏览器输入 http://ip:28017 ,就可以打开通过web的控制台了。

mongodb-web

3. 通过命令行客户端访问MongoDB

安装MongoDB服务器,会自动地一起安装MongoDB命令行客户端程序。

在本机输入mongo命令就可以启动,客户端程序访问MongoDB服务器。


~ mongo
MongoDB shell version: 2.4.9
connecting to: test
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
        http://docs.mongodb.org/
Questions? Try the support group
        http://groups.google.com/group/mongodb-user

# 查看命令行帮助
> help
        db.help()                    help on db methods
        db.mycoll.help()             help on collection methods
        sh.help()                    sharding helpers
        rs.help()                    replica set helpers
        help admin                   administrative help
        help connect                 connecting to a db help
        help keys                    key shortcuts
        help misc                    misc things to know
        help mr                      mapreduce

        show dbs                     show database names
        show collections             show collections in current database
        show users                   show users in current database
        show profile                 show most recent system.profile entries with time >= 1ms
        show logs                    show the accessible logger names
        show log [name]              prints out the last segment of log in memory, 'global' is default
        use                 set current database
        db.foo.find()                list objects in collection foo
        db.foo.find( { a : 1 } )     list objects in foo where a == 1
        it                           result of the last line evaluated; use to further iterate
        DBQuery.shellBatchSize = x   set default number of items to display on shell
        exit                         quit the mongo shell

MongoDB服务器,默认情况下是允许外部访问的。这样单节的MongoDB,我们已经成功地安装在Linux Ubuntu系统中。

转载请注明出处:
http://blog.fens.me/linux-mongodb-install/

打赏作者

在Ubuntu中安装Cassandra

R利剑NoSQL系列文章,主要介绍通过R语言连接使用nosql数据库。涉及的NoSQL产品,包括RedisMongoDBHBaseHiveCassandraNeo4j。希望通过我的介绍让广大的R语言爱好者,有更多的开发选择,做出更多地激动人心的应用。

关于作者:

  • 张丹(Conan), 程序员Java,R,PHP,Javascript
  • weibo:@Conan_Z
  • blog: http://blog.fens.me
  • email: bsspirit@gmail.com

转载请注明出处:
http://blog.fens.me/linux-cassandra-install/

linux-cassandra

前言

Cassandra是由Facebook开发,然后开源的一套分步式数据存储系统,用于海量数据的高伸展性存储。Cassandra的无中心设计,一致性哈希,BloomFilter等技术是亮点。

目录

  1. 在Ubuntu中环境准备
  2. 下载cassandra软件包
  3. 配置Cassandra
  4. 设置环境变量
  5. 启动cassandra服务器
  6. 用客户端访问cassandra

1. 在Ubuntu中环境准备

Cassandra是基于Java开发的NoSQL数据库软件,Cassandra没有提供Windows系统安装版本。我在这里也只介绍Cassandra在Linux Ubuntu系统中的安装。

由于Cassandra是用Java开发的,因此我们需要先安装好Java的环境,Java的安装请参考文章:在Ubuntu中安装Java(JDK)

Cassandra没有提供apt的软件源安装,我们需要自己去官方网络下载Cassandra软件包进行安装。Cassandra下载页:http://cassandra.apache.org/download/

下载Cassandra时,发现cassandra有两个版本在并行发布。因此我们有2种选择,最新2.0分支的版本2.0.6(2014-03-10发布);或者1.2分支的版本1.2.15(2014-02-07)。

由于我不清楚2.0分支,就底有哪些改进;所以本文将以1.2分支1.2.15版本为例,进行单机的安装和配置。

系统环境:

  • Linux Ubuntu 12.04.2 LTS 64bit server
  • Java JDK 1.6.0_45

2. 下载cassandra软件包

下载cassandra


# 1.2.15版本下载
~ wget http://apache.dataguru.cn/cassandra/1.2.15/apache-cassandra-1.2.15-bin.tar.gz

# 解压软件包
~ tar xvf apache-cassandra-1.2.15-bin.tar.gz

# 查看文件和目录
~ tree -L 2
.
├── apache-cassandra-1.2.15
│   ├── bin
│   ├── CHANGES.txt
│   ├── conf
│   ├── interface
│   ├── javadoc
│   ├── lib
│   ├── LICENSE.txt
│   ├── NEWS.txt
│   ├── NOTICE.txt
│   ├── pylib
│   ├── README.txt
│   └── tools
├── apache-cassandra-1.2.15-bin.tar.gz

# 对Cassandra解压目录改名
~ mv apache-cassandra-1.2.15/ cassandra1215

# 进行目录
~ cd cassandra1215/

3 配置Cassandra

设置cassandra数据目录

  • data_file_directories:为数据文件目录
  • commitlog_directory:为日志文件目录
  • saved_caches_directory:为缓存文件目录

用vi打开cassandra的配置文件cassandra.yaml


~ vi conf/cassandra.yaml

data_file_directories:
    - /var/lib/cassandra/data
commitlog_directory: /var/lib/cassandra/commitlog
saved_caches_directory: /var/lib/cassandra/saved_caches

确认操作系统中,这几个目录已被创建。
同时确认/var/log/cassandra/目录,对于操作用户是可写的。


# 新建目录
~ sudo mkdir -p /var/lib/cassandra/data
~ sudo mkdir -p /var/lib/cassandra/saved_caches
~ sudo mkdir -p /var/lib/cassandra/commitlog
~ sudo mkdir -p /var/log/cassandra/

# 把目录归属改成操作用户
~ sudo chown -R conan:conan /var/lib/cassandra
~ sudo chown -R conan:conan /var/log/cassandra/

# 相看目录权限
~ ll /var/lib/cassandra
drwxr-xr-x  2 conan conan 4096  3月 22 06:23 commitlog/
drwxr-xr-x  2 conan conan 4096  3月 22 06:23 data/
drwxr-xr-x  2 conan conan 4096  3月 22 06:23 saved_caches/

4 设置环境变量


~ sudo vi /etc/environment
CASSANDRA_HOME=/home/conan/tookit/cassandra1215

# 让环境变量生效
~ . /etc/environment

#查看环境变量
~ echo $CASSANDRA_HOME
/home/conan/tookit/cassandra1215

5 启动cassandra服务器

通过命令,启动cassandra服务器


#注:-f参数是绑定到console,不加-f则是后台启动。
~ bin/cassandra

 INFO 06:28:08,777 Logging initialized
 INFO 06:28:08,795 JVM vendor/version: Java HotSpot(TM) 64-Bit Server VM/1.6.0_45
 INFO 06:28:08,799 Heap size: 2051014656/2051014656
 INFO 06:28:08,799 Classpath: bin/../conf:bin/../build/classes/main:bin/../build/classes/thrift:bin/../lib/antlr-3.2.jar:bin/../lib/apache-cassandra-1.2.15.jar:bin/../lib/apache-cassandra-clientutil-1.2.15.jar:bin/../lib/apache-cassandra-thrift-1.2.15.jar:bin/../lib/avro-1.4.0-fixes.jar:bin/../lib/avro-1.4.0-sources-fixes.jar:bin/../lib/commons-cli-1.1.jar:bin/../lib/commons-codec-1.2.jar:bin/../lib/commons-lang-2.6.jar:bin/../lib/compress-lzf-0.8.4.jar:bin/../lib/concurrentlinkedhashmap-lru-1.3.jar:bin/../lib/guava-13.0.1.jar:bin/../lib/high-scale-lib-1.1.2.jar:bin/../lib/jackson-core-asl-1.9.2.jar:bin/../lib/jackson-mapper-asl-1.9.2.jar:bin/../lib/jamm-0.2.5.jar:bin/../lib/jbcrypt-0.3m.jar:bin/../lib/jline-1.0.jar:bin/../lib/json-simple-1.1.jar:bin/../lib/libthrift-0.7.0.jar:bin/../lib/log4j-1.2.16.jar:bin/../lib/lz4-1.1.0.jar:bin/../lib/metrics-core-2.2.0.jar:bin/../lib/netty-3.6.6.Final.jar:bin/../lib/servlet-api-2.5-20081211.jar:bin/../lib/slf4j-api-1.7.2.jar:bin/../lib/slf4j-log4j12-1.7.2.jar:bin/../lib/snakeyaml-1.6.jar:bin/../lib/snappy-java-1.0.5.jar:bin/../lib/snaptree-0.1.jar:bin/../lib/jamm-0.2.5.jar
 INFO 06:28:08,801 JNA not found. Native methods will be disabled.
 INFO 06:28:08,813 Loading settings from file:/home/conan/tookit/cassandra1215/conf/cassandra.yaml
 
// 省略日志

查看cassandra系统进程


# 查看cassandra系统进程
~ ps -axu|grep cassandra

conan     5983 18.1  2.1 4499456 172832 pts/1  Sl   06:31   0:05 java -ea -javaagent:bin/../lib/jamm-0.2.5.jar -XX:+UseThreadPriorities -XX:ThreadPriorityPolicy=42 -Xms1996M -Xmx1996M -Xmn400M -XX:+HeapDumpOnOutOfMemoryError -Xss256k -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -XX:+CMSParallelRemarkEnabled -XX:SurvivorRatio=8 -XX:MaxTenuringThreshold=1 -XX:CMSInitiatingOccupancyFraction=75 -XX:+UseCMSInitiatingOccupancyOnly -XX:+UseTLAB -Djava.net.preferIPv4Stack=true -Dcom.sun.management.jmxremote.port=7199 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false -Dlog4j.configuration=log4j-server.properties -Dlog4j.defaultInitOverride=true -cp bin/../conf:bin/../build/classes/main:bin/../build/classes/thrift:bin/../lib/antlr-3.2.jar:bin/../lib/apache-cassandra-1.2.15.jar:bin/../lib/apache-cassandra-clientutil-1.2.15.jar:bin/../lib/apache-cassandra-thrift-1.2.15.jar:bin/../lib/avro-1.4.0-fixes.jar:bin/../lib/avro-1.4.0-sources-fixes.jar:bin/../lib/commons-cli-1.1.jar:bin/../lib/commons-codec-1.2.jar:bin/../lib/commons-lang-2.6.jar:bin/../lib/compress-lzf-0.8.4.jar:bin/../lib/concurrentlinkedhashmap-lru-1.3.jar:bin/../lib/guava-13.0.1.jar:bin/../lib/high-scale-lib-1.1.2.jar:bin/../lib/jackson-core-asl-1.9.2.jar:bin/../lib/jackson-mapper-asl-1.9.2.jar:bin/../lib/jamm-0.2.5.jar:bin/../lib/jbcrypt-0.3m.jar:bin/../lib/jline-1.0.jar:bin/../lib/json-simple-1.1.jar:bin/../lib/libthrift-0.7.0.jar:bin/../lib/log4j-1.2.16.jar:bin/../lib/lz4-1.1.0.jar:bin/../lib/metrics-core-2.2.0.jar:bin/../lib/netty-3.6.6.Final.jar:bin/../lib/servlet-api-2.5-20081211.jar:bin/../lib/slf4j-api-1.7.2.jar:bin/../lib/slf4j-log4j12-1.7.2.jar:bin/../lib/snakeyaml-1.6.jar:bin/../lib/snappy-java-1.0.5.jar:bin/../lib/snaptree-0.1.jar org.apache.cassandra.service.CassandraDaemon

# 查看系统端口
~ netstat -nlt|grep 9160
tcp        0      0 127.0.0.1:9160          0.0.0.0:*               LISTEN

6 用客户端访问cassandra

通过客户端程序访问Cassandra服务器


~ bin/cassandra-cli
Connected to: "Test Cluster" on 127.0.0.1/9160
Welcome to Cassandra CLI version 1.2.15

Type 'help;' or '?' for help.
Type 'quit;' or 'exit;' to quit.

# 查看命令行帮助
[default@unknown] ?

Getting around:
?                       Display this help.
help;                   Display this help.
help ;         Display command-specific help.
exit;                   Exit this utility.
quit;                   Exit this utility.

Commands:
assume                  Apply client side validation.
connect                 Connect to a Cassandra node.
consistencylevel        Sets consisteny level for the client to use.
count                   Count columns or super columns.
create column family    Add a column family to an existing keyspace.
create keyspace         Add a keyspace to the cluster.
del                     Delete a column, super column or row.
decr                    Decrements a counter column.
describe cluster        Describe the cluster configuration.
describe                Describe a keyspace and its column families or column family in current keyspace.
drop column family      Remove a column family and its data.
drop keyspace           Remove a keyspace and its data.
drop index              Remove an existing index from specific column.
get                     Get rows and columns.
incr                    Increments a counter column.
list                    List rows in a column family.
set                     Set columns.
show api version        Show the server API version.
show cluster name       Show the cluster name.
show keyspaces          Show all keyspaces and their column families.
show schema             Show a cli script to create keyspaces and column families.
truncate                Drop the data in a column family.
update column family    Update the settings for a column family.
update keyspace         Update the settings for a keyspace.
use                     Switch to a keyspace.

单节的Cassandra,我们已经成功能安装在Linux Ubuntu系统中。

转载请注明出处:
http://blog.fens.me/linux-cassandra-install/

打赏作者

在Ubuntu中安装Redis

R利剑NoSQL系列文章,主要介绍通过R语言连接使用nosql数据库。涉及的NoSQL产品,包括RedisMongoDBHBaseHiveCassandraNeo4j。希望通过我的介绍让广大的R语言爱好者,有更多的开发选择,做出更多地激动人心的应用。

关于作者:

  • 张丹(Conan), 程序员Java,R,PHP,Javascript
  • weibo:@Conan_Z
  • blog: http://blog.fens.me
  • email: bsspirit@gmail.com

转载请注明出处:
http://blog.fens.me/linux-redis-install/

ubuntu-redis

前言

Redis是常用基于内存的Key-Value数据库,比Memcache更先进,支持多种数据结构,高效,快速。用Redis可以很轻松解决高并发的数据访问问题;做为时时监控信号处理也非常不错。

目录

  1. Redis在Windows中安装
  2. Redis在Linux Ubuntu中安装
  3. 通过命令行客户端访问Redis
  4. 修改Redis的配置

1. Redis在Windows中安装

在Windows系统上安装Redis数据库是件非常简单的事情,下载可执行安装文件(exe),双击安装即可。下载地址:https://github.com/rgl/redis/downloads

  • Redis服务器运行命令:Redis安装目录/redis-server.exe
  • Redis客户端运行命令:Redis安装目录/redis-cli.exe

2. Redis在Linux Ubuntu中安装

本文使用的Linux是Ubuntu 12.04.2 LTS 64bit的系统,安装Redis数据库软件包可以通过apt-get实现。

在Linux Ubuntu中安装Redis数据库


#安装Redis服务器端
~ sudo apt-get install redis-server

安装完成后,Redis服务器会自动启动,我们检查Redis服务器程序


# 检查Redis服务器系统进程
~ ps -aux|grep redis
redis     4162  0.1  0.0  10676  1420 ?        Ss   23:24   0:00 /usr/bin/redis-server /etc/redis/redis.conf
conan     4172  0.0  0.0  11064   924 pts/0    S+   23:26   0:00 grep --color=auto redis

# 通过启动命令检查Redis服务器状态
~ netstat -nlt|grep 6379
tcp        0      0 127.0.0.1:6379          0.0.0.0:*               LISTEN

# 通过启动命令检查Redis服务器状态
~ sudo /etc/init.d/redis-server status
redis-server is running

3. 通过命令行客户端访问Redis

安装Redis服务器,会自动地一起安装Redis命令行客户端程序。

在本机输入redis-cli命令就可以启动,客户端程序访问Redis服务器。


~ redis-cli
redis 127.0.0.1:6379>

# 命令行的帮助
redis 127.0.0.1:6379> help
redis-cli 2.2.12
Type: "help @" to get a list of commands in 
      "help " for help on 
      "help " to get a list of possible help topics
      "quit" to exit


# 查看所有的key列表
redis 127.0.0.1:6379> keys *
(empty list or set)

基本的Redis客户端命令操作

增加一条字符串记录key1


# 增加一条记录key1
redis 127.0.0.1:6379> set key1 "hello"
OK

# 打印记录
redis 127.0.0.1:6379> get key1
"hello"

增加一条数字记录key2


# 增加一条数字记录key2
set key2 1
OK

# 让数字自增
redis 127.0.0.1:6379> INCR key2
(integer) 2
redis 127.0.0.1:6379> INCR key2
(integer) 3

# 打印记录
redis 127.0.0.1:6379> get key2
"3"

增加一条列表记录key3


# 增加一个列表记录key3
redis 127.0.0.1:6379> LPUSH key3 a
(integer) 1

# 从左边插入列表
redis 127.0.0.1:6379> LPUSH key3 b
(integer) 2

# 从右边插入列表
redis 127.0.0.1:6379> RPUSH key3 c
(integer) 3

# 打印列表记录,按从左到右的顺序
redis 127.0.0.1:6379> LRANGE key3 0 3
1) "b"
2) "a"
3) "c"

增加一条哈希表记录key4


# 增加一个哈希记表录key4
redis 127.0.0.1:6379> HSET key4 name "John Smith"
(integer) 1

# 在哈希表中插入,email的Key和Value的值
redis 127.0.0.1:6379> HSET key4 email "abc@gmail.com"
(integer) 1

# 打印哈希表中,name为key的值
redis 127.0.0.1:6379> HGET key4 name
"John Smith"

# 打印整个哈希表
redis 127.0.0.1:6379> HGETALL key4
1) "name"
2) "John Smith"
3) "email"
4) "abc@gmail.com"

增加一条哈希表记录key5


# 增加一条哈希表记录key5,一次插入多个Key和value的值
redis 127.0.0.1:6379> HMSET key5 username antirez password P1pp0 age 3
OK

# 打印哈希表中,username和age为key的值
redis 127.0.0.1:6379> HMGET key5 username age
1) "antirez"
2) "3"

# 打印完整的哈希表记录
redis 127.0.0.1:6379> HGETALL key5
1) "username"
2) "antirez"
3) "password"
4) "P1pp0"
5) "age"
6) "3"

删除记录


# 查看所有的key列表
redis 127.0.0.1:6379> keys *
1) "key2"
2) "key3"
3) "key4"
4) "key5"
5) "key1"

# 删除key1,key5
redis 127.0.0.1:6379> del key1
(integer) 1
redis 127.0.0.1:6379> del key5
(integer) 1

# 查看所有的key列表
redis 127.0.0.1:6379> keys *
1) "key2"
2) "key3"
3) "key4"

4. 修改Redis的配置

4.1 使用Redis的访问账号

默认情况下,访问Redis服务器是不需要密码的,为了增加安全性我们需要设置Redis服务器的访问密码。设置访问密码为redisredis。

用vi打开Redis服务器的配置文件redis.conf


~ sudo vi /etc/redis/redis.conf

#取消注释requirepass
requirepass redisredis

4.2 让Redis服务器被远程访问

默认情况下,Redis服务器不允许远程访问,只允许本机访问,所以我们需要设置打开远程访问的功能。

用vi打开Redis服务器的配置文件redis.conf


~ sudo vi /etc/redis/redis.conf

#注释bind
#bind 127.0.0.1

修改后,重启Redis服务器。


~ sudo /etc/init.d/redis-server restart
Stopping redis-server: redis-server.
Starting redis-server: redis-server.

未使用密码登陆Redis服务器


~ redis-cli

redis 127.0.0.1:6379> keys *
(error) ERR operation not permitted

发现可以登陆,但无法执行命令了。

登陆Redis服务器,输入密码


~  redis-cli -a redisredis

redis 127.0.0.1:6379> keys *
1) "key2"
2) "key3"
3) "key4"

登陆后,一切正常。

我们检查Redis的网络监听端口


检查Redis服务器占用端口
~ netstat -nlt|grep 6379
tcp        0      0 0.0.0.0:6379            0.0.0.0:*               LISTEN

我们看到从之间的网络监听从 127.0.0.1:3306 变成 0 0.0.0.0:3306,表示Redis已经允许远程登陆访问。

我们在远程的另一台Linux访问Redis服务器


~ redis-cli -a redisredis -h 192.168.1.199

redis 192.168.1.199:6379> keys *
1) "key2"
2) "key3"
3) "key4"

远程访问正常。通过上面的操作,我们就把Redis数据库服务器,在Linux Ubuntu中的系统安装完成。

转载请注明出处:
http://blog.fens.me/linux-redis-install/

打赏作者

在Ubuntu中安装MySQL

Ubuntu实用工具系列文章,将介绍基于Linux ubuntu的各种工具软件的配置和使用。有些工具大家早已耳熟能详,有些工具经常用到但确依然陌生。我将记录我在使用操作系统时,安装及配置工具上面的一些方法,把使用心得记录下来也便于自己的以后查找和回忆。

关于作者:

  • 张丹(Conan), 程序员Java,R,PHP,Javascript
  • weibo:@Conan_Z
  • blog: http://blog.fens.me
  • email: bsspirit@gmail.com

转载请注明出处:
http://blog.fens.me/linux-mysql-install/

ubuntu-mysql-install

前言

安装MySQL是个老话题,我安装MySQL服务器已不下百次了,为了博客文章结构的连贯性,还是再写一篇做为环境基础,同时也给自己一个备忘。

目录

  1. MySQL在Windows中安装
  2. MySQL在Linux Ubuntu中安装
  3. 通过命令行客户端访问MySQL
  4. 修改MySQL服务器的配置
  5. 新建数据库并设置访问账号
  6. 改变数据存储位置

1. MySQL在Windows中安装

在Windows系统上安装MySQl数据库是件非常简单的事情,下载压缩包,解压即可。下载地址:http://dev.mysql.com/downloads/mysql/

  • MySQL服务器运行命令:MySQL安装目录/bin/mysqld.exe
  • MySQL客户端运行命令:MySQL安装目录/bin/mysql.exe

2. MySQL在Linux Ubuntu中安装

本文使用的Linux是Ubuntu 12.04.2 LTS 64bit的系统,安装MySQL数据库软件包可以通过apt-get实现。

在Linux Ubuntu中安装MySQL数据库


#安装MySQL服务器端
~ sudo apt-get install mysql-server

安装过程会弹出提示框,输入root用户的密码,我在这里设置密码为mysql。

安装完成后,MySQL服务器会自动启动,我们检查MySQL服务器程序


# 检查MySQL服务器系统进程
~ ps -aux|grep mysql
mysql     3205  2.0  0.5 549896 44092 ?        Ssl  20:10   0:00 /usr/sbin/mysqld
conan     3360  0.0  0.0  11064   928 pts/0    S+   20:10   0:00 grep --color=auto mysql

# 检查MySQL服务器占用端口
~ netstat -nlt|grep 3306
tcp        0      0 127.0.0.1:3306          0.0.0.0:*               LISTEN

# 通过启动命令检查MySQL服务器状态
~ sudo /etc/init.d/mysql status
Rather than invoking init scripts through /etc/init.d, use the service(8)
utility, e.g. service mysql status

Since the script you are attempting to invoke has been converted to an
Upstart job, you may also use the status(8) utility, e.g. status mysql
mysql start/running, process 3205

# 通过系统服务命令检查MySQL服务器状态
~ service mysql status
mysql start/running, process 3205

3. 通过命令行客户端访问MySQL

安装MySQL服务器,会自动地一起安装MySQL命令行客户端程序。

在本机输入mysql命令就可以启动,客户端程序访问MySQL服务器。


~ mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 42
Server version: 5.5.35-0ubuntu0.12.04.2 (Ubuntu)

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>

使用户名和密码,登陆服务器


~ mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 37
Server version: 5.5.35-0ubuntu0.12.04.2 (Ubuntu)

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>

MySQL的一些简单的命令操作。


# 查看所有的数据库
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| test               |
+--------------------+
2 rows in set (0.00 sec)

# 切换到information_schema库
mysql> use information_schema
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed

# 查看information_schema库中所有的表
mysql> show tables;
+---------------------------------------+
| Tables_in_information_schema          |
+---------------------------------------+
| CHARACTER_SETS                        |
| COLLATIONS                            |
| COLLATION_CHARACTER_SET_APPLICABILITY |
| COLUMNS                               |
| COLUMN_PRIVILEGES                     |
| ENGINES                               |
| EVENTS                                |
| FILES                                 |
| GLOBAL_STATUS                         |
| GLOBAL_VARIABLES                      |
| KEY_COLUMN_USAGE                      |
| PARAMETERS                            |
| PARTITIONS                            |
| PLUGINS                               |
| PROCESSLIST                           |
| PROFILING                             |
| REFERENTIAL_CONSTRAINTS               |
| ROUTINES                              |
| SCHEMATA                              |
| SCHEMA_PRIVILEGES                     |
| SESSION_STATUS                        |
| SESSION_VARIABLES                     |
| STATISTICS                            |
| TABLES                                |
| TABLESPACES                           |
| TABLE_CONSTRAINTS                     |
| TABLE_PRIVILEGES                      |
| TRIGGERS                              |
| USER_PRIVILEGES                       |
| VIEWS                                 |
| INNODB_BUFFER_PAGE                    |
| INNODB_TRX                            |
| INNODB_BUFFER_POOL_STATS              |
| INNODB_LOCK_WAITS                     |
| INNODB_CMPMEM                         |
| INNODB_CMP                            |
| INNODB_LOCKS                          |
| INNODB_CMPMEM_RESET                   |
| INNODB_CMP_RESET                      |
| INNODB_BUFFER_PAGE_LRU                |
+---------------------------------------+
40 rows in set (0.01 sec)

# 查看数据库的字符集编码
mysql> show variables like '%char%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | utf8                       |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | latin1                     |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)

4. 修改MySQL服务器的配置

接下来,我需要做一些配置,让MySQL符合基本的开发要求。

4.1 将字符编码设置为UTF-8

默认情况下,MySQL的字符集是latin1,因此在存储中文的时候,会出现乱码的情况,所以我们需要把字符集统一改成UTF-8。

用vi打开MySQL服务器的配置文件my.cnf


~ sudo vi /etc/mysql/my.cnf

#在[client]标签下,增加客户端的字符编码
[client]
default-character-set=utf8

#在[mysqld]标签下,增加服务器端的字符编码
[mysqld]
character-set-server=utf8
collation-server=utf8_general_ci

4.2 让MySQL服务器被远程访问

默认情况下,MySQL服务器不允许远程访问,只允许本机访问,所以我们需要设置打开远程访问的功能。

用vi打开MySQL服务器的配置文件my.cnf


~ sudo vi /etc/mysql/my.cnf

#注释bind-address
#bind-address            = 127.0.0.1

修改后,重启MySQL服务器。


~ sudo /etc/init.d/mysql restart
Rather than invoking init scripts through /etc/init.d, use the service(8)
utility, e.g. service mysql restart

Since the script you are attempting to invoke has been converted to an
Upstart job, you may also use the stop(8) and then start(8) utilities,
e.g. stop mysql ; start mysql. The restart(8) utility is also available.
mysql start/running, process 3577

重新登陆服务器


~ mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 37
Server version: 5.5.35-0ubuntu0.12.04.2 (Ubuntu)

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 variables like '%char%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | utf8                       |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | utf8                       |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)

我们检查MySQL的网络监听端口


# 检查MySQL服务器占用端口
~ netstat -nlt|grep 3306
  tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN

我们看到从之间的网络监听从 127.0.0.1:3306 变成 0 0.0.0.0:3306,表示MySQL已经允许远程登陆访问。通过root账号远程访问,是非常不安全的操作,因此我们下一步,将新建一个数据库,再新建一个用户进行远程访问。

5. 新建数据库并设置访问账号

通过root账号登陆MySQl服务器


~ mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 39
Server version: 5.5.35-0ubuntu0.12.04.2 (Ubuntu)

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.

# 新建数据库abc
mysql> CREATE DATABASE abc;

# 使用数据库abc
mysql> use abc;
Database changed

# 在数据库abc中,新建一张表a1
mysql> create table a1(id int primary key,name varchar(32) not null);
Query OK, 0 rows affected (0.05 sec)

# 新建book用户,密码为book,允许book可以远程访问abc数据库,授权book对abc进行所有数据库
mysql> GRANT ALL ON abc.* to book@'%' IDENTIFIED BY 'book';
Query OK, 0 rows affected (0.00 sec)

#允许book可以本地访问abc数据库,授权book对abc进行所有数据库
mysql> GRANT ALL ON abc.* to book@localhost IDENTIFIED BY 'book';
Query OK, 0 rows affected (0.00 sec)

我们在本地使用book用户登陆


# 使用book用户登陆
~ mysql -ubook -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 40
Server version: 5.5.35-0ubuntu0.12.04.2 (Ubuntu)

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.

#进行abc数据库
mysql> use abc;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed

#查看abc数据库的表
mysql> show tables;
+---------------+
| Tables_in_abc |
+---------------+
| a1            |
+---------------+
1 row in set (0.00 sec)

我们在远程的另一台Linux使用book用户登陆


~ mysql -ubook -p -h 192.168.1.199
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 41
Server version: 5.5.35-0ubuntu0.12.04.2 (Ubuntu)

Copyright (c) 2000, 2012, 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> use abc
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+---------------+
| Tables_in_abc |
+---------------+
| a1            |
+---------------+
1 row in set (0.00 sec)

5. 改变数据存储位置

有时候我们可能还需要改变MySQL数据存储的位置,一种方法是直接修改配置文件 /etc/mysql/my.cnf,找到datadir属性修改目录。


~ vi /etc/mysql/my.cnf

[mysqld]
datadir         = /var/lib/mysql

如果通过这种方法修改,那么其他的调用存储路径的地方,我们也都需要进行修改,比如 用到了/usr/bin/mysql_install_db 命令,文件中ldata的属性也需要修改,关于mysql_install_db 命令的使用可以参考文章,[MySQL优化]为MySQL数据文件ibdata1瘦身

还有另一种修改存储位置的方法,就是通过Linux系统的软连(ln -s)接来做的。当我们新挂载一块硬盘,停止MySQL服务,然后把/var/lib/mysql目录移动到新的硬盘存储,在/var/lib/mysql处建立指定新位置的软连接就行了。


# 停止MySQL服务器
~ /etc/init.d/mysql stop

# 挂载硬盘
~ mount -t ext4 /dev/vdb1 /vdb1

# 建立新存储目录
~ mkdir /vdb1/data

# 移动MySQL数据目录到新目录
~ mv /var/lib/mysql /vdb1/data/

# 软连接
~ ln -s /vdb1/data/mysql /var/lib/mysql

修改apparmor的别名定义文件


~ vi /etc/apparmor.d/tunables/alias

alias /var/lib/mysql/ -> /vdb1/data/mysql/,

注:如果没有修改apparmor的配置,MySQL会启动不了,并一直提示是权限的问题。


# 重启apparmor服务
~ /etc/init.d/apparmor restart

# 重启MySQL服务器
~ /etc/init.d/mysql start

这样就完成了,MySQL数据存储位置修改。

通过上面的操作,我们就把MySQL数据库服务器,在Linux Ubuntu中的系统安装完成。

转载请注明出处:
http://blog.fens.me/linux-mysql-install/

打赏作者