Francis's Octopress Blog

A blogging framework for hackers.

解决Illegal Mix of Collations (latin1_swedish_ci,IMPLICIT) and (utf8_general_ci,COER

解决Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and (utf8_general_ci,COER

部署完项目,测试一下,诶,数据出来了 没有多大问题(暗舒一口气)。继续测吧,一点新建完了,报错了,看看什么错误

一看完了 java.sql.SQLException: Illegal mix of collations (latin1_swedish_ci,IMPLICIT)

and (utf8_general_ci,COERCIBLE) for operation ‘=’ 是这个错误 ,什么原因呢,第一次遇到。

头大了。去查文档说是:结果集中有两种字符集。 我晕了 ,怎么会这样呢,看看表结构,一种啊。 继续查吧。

 代码如下 复制代码
SHOW VARIABLES LIKE ‘character_set_%’; 查看一下 显示+————————–+—————————-+ | Variable_name | Value | +————————–+—————————-+ | character_set_client | utf8| | character_set_connection | utf8| | character_set_database | latin1 | | character_set_results | utf8| | character_set_server | latin1 | | character_set_system | utf8 | | character_sets_dir | /home/jh/mysql/share/mysql/charsets |

+—————————————+——————————————+

再用 SHOW VARIABLES LIKE ‘collation_%’; 查看一下 显示

 代码如下 复制代码
+———————-+——————-+| Variable_name | Value | +———————-+——————-+ | collation_connection | utf8_swedish_ci | | collation_database | latin1_swedish_ci | | collation_server | latin1_swedish_ci | +———————-+——————-+

原来如此啊 哈哈 知道错在哪里 剩下的就好办了

解决方法:

依次执行:

 代码如下 复制代码
set character_set_database =utf8;   set character_set_results =utf8;   set character_set_server =utf8;   set character_set_system =utf8; –此处utf-8也可以   然后执行:   SET collation_server = utf8_general_ci     SET collation_database = utf8_general_ci

执行完之后,请检查mysql下每个数据库,表,字段是否都是utf8,不是则改过来,这样子就不会出现

 

最笨的方法是重装一下数据库。(一般不要用这种方法呀)

最终解决方法:

1.1 如果是windows版本的mysql,那么在安装的时候,系统就会提示用哪种编码。

如果安装的时候设置错误了,修改mysql安装目录下的my.ini文件:

 代码如下 复制代码
[mysql]default-character-set=utf8 … # The default character set that will be used when a new schema or table is # created and no character set is defined default-character-set=utf8

配置好后,重启mysql。

1.2 如果是linux版本的mysql

 

修改mysql的配置文件,使数据库与服务器操作系统的字符集设置一致。

vi /etc/my.cnf 设置(如果没有发现这个文件,就新建1个)

 代码如下 复制代码
[mysqld]datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock default-character-set=utf8

(增加的关键一句,使得数据库缺省以utf8存储)

当然,修改后,要重启数据库。(这样设置后对新建的数据库表才起作用)

 

 代码如下 复制代码
用SHOW VARIABLES LIKE ‘character_set_%’;命令查看到如下内容:+————————–+———————————————————————–+ | 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 | /home/jh/mysql/share/mysql/charsets | +————————–+———————————————————————–+

发现关键项目已经用了utf8,但这样还不够,还要保证客户端也是用utf8的字符集来操作的。

登录的时候,要用以下命令:mysql —default-character-set=utf8 -u root -p

再次用SHOW VARIABLES LIKE ‘character_set_%’;命令查看,结果变成了:

 代码如下 复制代码
+————————–+———————————————————————–+| 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 | /home/jh/mysql/share/mysql/charsets/ |

+—————————————+———————————————————————————————————–+

这样才能保证客户端所发命令都是基于utf8格式的,比如说建立数据库和表,默认就会以utf8编码,而无须再次指定。(再次说一句对新建的数据库和表起作用)。

另外:

第三种方法:网上看到的,先记录一下。

1.如果安装mysql的编码已不能更改,很多朋友是购买虚拟主机建立网站,无权更改MYSQL的安装编码,这一关我们可以跳过,因为只要后面的步聚正确,一样能解决乱码问题 2.修改数据库编码,如果是数据库编码不正确: 可以在phpmyadmin 执行如下命令: ALTER DATABASE test DEFAULT CHARACTER SET utf8 COLLATE utf8_bin 以上命令就是将test数据库的编码设为utf8 3.修改表的编码:

 代码如下 复制代码
ALTER TABLE `category` DEFAULT CHARACTER SET utf8 COLLATE utf8_bin

以上命令就是将一个表category的编码改为utf8 4.修改字段的编码:

 代码如下 复制代码
mysql> use askjane_development;
Database changed
mysql> alter database mydb character set utf8;
ERROR 1 (HY000): Can't create/write to file './mydb/db.opt' (Errcode: 2)
mysql> ALTER TABLE tags CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;Query OK, 2 rows affected (0.35 sec)

以上命令就是将test表中 dd的字段编码改为utf8 5.如果是这种情况容易解决,只需检查下页面,修改源文件的charset即可 , //这个正确就无问题了 6.这种情况也是修改页面charset即可.

 

Logs Are Streams, Not Files

Logs Are Streams, Not Files

Server daemons (such as PostgreSQL or Nginx) and applications (such as a Rails or Django app) sometimes offer a configuration parameter for a path to the program’s logfile. This can lead us to think of logs as files.

But a better conceptual model is to treat logs as time-ordered streams: there is no beginning or end, but rather an ongoing, collated collection of events which we may wish to view in realtime as they happen (e.g. via tail -f or heroku logs —tail) or which we may wish to search in some time window (e.g. via grep or Splunk).

Using the power of unix for logs

Unix provides some excellent tools for handling streams. There are two default output streams, stdout andstderr, available automatically to all programs. Streams can be turned into files with a redirect operator, but they can also be channeled in more powerful ways, such as splitting the streams to multiple locations or pipelining the stream to another program for further processing.

A program that uses stdout for its logging can easily log to any file you wish:

$ mydaemon >> /var/log/mydaemon.log

(Typically you would not invoke this command directly, but would run this from an init program such as Upstart or Systemd.)

Programs that send their logs directly to a logfile lose all the power and flexibility of unix streams. What’s worse is that they end up reinventing some of these capabilities, badly. How many programs end up re-writing log rotation, for example?

Distributed logging with syslog

Logging on any reasonably large distributed system will generally end up using the syslog protocol to send logs from many components to a single location. Programs that treat logs as files are now on the wrong path: if they wisht to log to syslog, each program needs to implement syslog internally – and provide yet more logging configuration options to set the various syslog fields.

A program using stdout for logging can use syslog without needing to implement any syslog awareness into the program, by piping to the standard logger command available on all modern unixes:

$ mydaemon | logger

Perhaps we want to split the stream and log to a local file as well as syslog:

$ mydaemon | tee /var/log/mydaemon.log | logger

A program which uses stdout is equipped to log in a variety of ways without adding any weight to its codebase or configuration format.

Other distributed logging protocols

Syslog is an entrenched standard for distributed logging, but there are other, more modern options as well.Splunk, fast becoming a indispensable tool for anyone running a large software service, can accept syslog; but it also has its own custom protocol which offers additional features like authentication and encryption.Scribe is another example of a modern logging protocol.

Programs that log to stdout can be adapted to work with a new protocol without needing to modify the program. Simply pipe the program’s output to a receiving daemon just as you would with the logger program for syslog. Treating your logs as streams is a form of future-proofing for your application.

Logging in the Ruby world

Most Rack frameworks (Sinatra, Ramaze, etc) and Rack webservers (Mongrel, Thin, etc) do the right thing: they log to stdout. If you run them in the foreground, as is typical of development mode, you see the output right in your terminal. This is exactly what you want. If you run in production mode, you can redirect the output to a file, to syslog, to both, or to any other logging system that can accept an input stream.

Unfortunately, Rails stands out as a major exception to this simple principle. It creates its own log directory and writes various files into it; some plugins even take it upon themselves to write their own, separate logfiles. This hurts the local development experience: what you see in your terminal isn’t complete, so you have to open a separate window with tail -f log/*.log to get the information you want. But it hurts the deployment experience even more, because you end up having to tinker around with a bunch of Rails logger configuration options to get your logs from all your web machines to merge into a single stream.

Logging on Heroku

The need to treat application logs as a stream is especially poignant with Heroku’s new logging system. On the backend, we route logs with a syslog router written in Erlang called Logplex.

Logplex handles input streams (which we call “sinks”) from many different sources: all the dynos running on the app, system components like our HTTP router, and (currently in alpha) logs from add-on providers. Sinks are merged together into channels (each app has its own channel) which is a unified stream of all logs relevant to the app. This allows developers to see a holistic view of everything happening with their app, or to filter down to logs from a particular type of sink (for example: just logs from the HTTP router, or just logs from worker processes).

Further, log streams can also be sent outbound, which we call “drains.” Users can configure syslog drains, and we’re currently working up a technical design for how add-on providers can automatically add drains. This latter item will enable a new class of log search and archival add-on, most notably the emerging syslog-as-a-service products like Loggly and Papertrail.

This logging system works quite well, and it gets even better with the new features on the way – but it only works where all programs output their logs as streams. Programs that write logfiles, such as Rails in its default configuration, don’t make sense in this world.

As a workaround, Heroku injects the rails_log_stdout plugin into Rails apps at deploy time. We’d prefer not to have to do this (injecting code is a dicey way to solve problems), but it’s the best way to get Rails logs into the app’s logstream without requiring extra configuration from the app developer.

Conclusion

Logs are a stream, and it behooves everyone to treat them as such. Your programs should log to stdoutand/or stderr and omit any attempt to handle log paths, log rotation, or sending logs over the syslog protocol. Directing where the program’s log stream goes can be left up to the runtime container: a local terminal or IDE (in development environments), an Upstart / Systemd launch script (in traditional hosting environments), or a system like Logplex/Heroku (in a platform environment).

Configuring Gedit for Rails

Configuring Gedit for Rails

I haven’t kept my feelings about IDEs hidden, I’m a big believer in using text editors instead.

I know, I should earn my chops and become a Vim guy, and some day I hope to sit down and make that conversion, but for now it’s all about Gedit for me. With a few plugins and a little TLC gedit can be a lighter version of the more powerful, more intensive ide.

The first thing we’re going to install gMate, an addon designed to make Gedit run like TextMate

sudo add-apt-repository ppa:ubuntu-on-rails/ppa sudo apt-get update
sudo apt-get install gedit-gmate

Additionally we’re going to install the standard plugins package

sudo apt-get install gedit-plugins

Now lets fire up Gedit and turn on our preferences.

To get to our plugins go to Edit > Preferences > Plugins.

We’re going to enable the following options:

  • Snippets
  • Code Comment
  • Embedded Terminal
  • Find in Files
  • Rails Extract Partial
  • Rails File Loader
  • Session Saver (Optional)
  • Smart Indent (Optional)
  • Tab Switch (Optional)
  • TextMate Style AutoCompletion

This is going to enable a lot of different functionality, and while this is the setup I use, it may be more than you need.

Now if you’re in a ruby file and you type def, tabbing over will add the end and place you straight on the method name, control+tab will switch between documents, and syntax highlighting will work correctly in html.erb files.

Also, going to view > bottom pane will display a terminal window that I find convenient for running irb.

While these instructions will enable a lot of different useful environments, and the target of this post was rails, I do have to sadly add that Google Go, which I cover quite often, does not, to my knowledge, have a plugin for Gedit currently.

创建基于Rails 3的纯净Ajax CRUD程序

创建基于Rails 3的纯净Ajax CRUD程序

Rails 3利用scaffold可以很容易地创建CRUD程序,但那是多页面的,现在很多场景需要使用Ajax在一个页面上实现CRUD。这对Rails 来说也是很简单的,下面就来创建一个符合Rails风格的Ajax CRUD程序。整个过程大概15分钟,建议先把整个代码照应敲一遍,然后再慢慢理解。

目标:

只有1个页面,CRUD全部基于Ajax在一个页面完成。使用无入侵式的Javascript风格。

平台:

适用于Rails 3.,使用jQuery+sqlite。 Rails 3.1开始默认自带jQuery,如果是Rails 3.0.需要手动添加jQuery。 如果想用Prototype要把那些js做相应修改。

Let’s GO:

1、创建项目

rails new AjaxCRUD
# 创建scaffold
rails g scaffold Entry name:string address:text phone:string email:string
# 建立数据库和表
rake db:create
rake db:migrate

2、修改controller

因为创建的是基于Ajax的CRUD,所以controller返回的数据要支持‘.js’格式,可以把默认的json格式删掉,其实除了index会返回网页,其它都是返回js,所以index外的html格式返回也可以删除。

respond_to do |format|
format.html
format.js
end

基于个思想,修改后的controller如下:

class EntriesController < ApplicationController
  def index
    @entries = Entry.all
    @entry = Entry.new

    respond_to do |format|
      format.html # index.html.erb
      format.js
    end
  end

  def show
    @entry = Entry.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.js
    end
  end

  def new
    @entry = Entry.new

    respond_to do |format|
      format.html # new.html.erb
      format.js
    end
  end

  def edit
    @entry = Entry.find(params[:id])

    respond_to do |format|
      format.html
      format.js
    end
  end

  def create
    @entry = Entry.new(params[:entry])

    respond_to do |format|
      if @entry.save
        format.html { redirect_to @entry, notice: 'Entry was successfully created.' }
        format.js
      else
        format.html { render action: "new" }
        format.js { render action: "new" }
      end
    end
  end

  def update
    @entry = Entry.find(params[:id])

    respond_to do |format|
      if @entry.update_attributes(params[:entry])
        format.html { redirect_to @entry, notice: 'Entry was successfully updated.' }
        format.js
      else
        format.html { render action: "edit" }
        format.js { render action: "edit" }
      end
    end
  end

  def destroy
    @entry = Entry.find(params[:id])
    @entry.destroy

    respond_to do |format|
      format.html { redirect_to entries_url }
      format.js
    end
  end
end

3、修改Views

修改index页面(app/views/entries/index.html.erb)来显示Entry表单。

Listing entries

Name Phone Email Address

Entry form

"form" %>

注意:为了后面用jQuery操作DOM,table设置id=”entries”。

修改partial(app/views/entries/_form.html.erb),注意在form_for中添加remote以发送异步请求。

<%= form_for(@entry, :remote => true) do |f| %>
  <% if @entry.errors.any? %>
    

<%= pluralize(@entry.errors.count, "error") %> prohibited this entry from being saved:

    <% @entry.errors.full_messages.each do |msg| %>
  • <%= msg %>
  • <% end %>
<% end %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :phone %>
<%= f.text_field :phone %>
<%= f.label :email %>
<%= f.text_field :email %>
<%= f.label :address %>
<%= f.text_area :address, :rows => 3 %>
<%= f.submit %>
<% end %>

新建Entry partial(app/views/entries/_entry.html.erb)。 为了发送AJAX请求,edit,destroy都设置remote=true。 为了区分不同的entry,调用dom_id来根据entry生成不同的id


  <%= entry.name %>
  <%= entry.phone %>
  <%= entry.email %>
  <%= simple_format entry.address %>
  <%= link_to 'Show', entry %>
  <%= link_to 'Edit', edit_entry_path(entry), :remote => true %>
  <%= link_to 'Destroy', entry, confirm: 'Are you sure?', method: :delete, :remote => true %>

4、添加js.erb来响应Ajax请求

下面是关键,首先先说下操作流程:浏览器发起Ajax请求(CRUD)–>controller收到请求,并调用model更新数据–>返回js代码–>浏览器收到js代码,使用jQuery来更新index页面中的DOM对象,包括列表和表单。

新建 app/views/entries/create.js.erb 响应添加Entry的请求 1、在index页面的entry列表中添加刚才新增的entry对象。 2、清空index页面中添加entry表单中的数据。

$('<%= escape_javascript(render(:partial => @entry)) %>').appendTo('#entries').hide().fadeIn();
$("#new_entry")[0].reset();

escape_javascript(render(:partial => @entry))可以缩写成:j render @entry。

新建 app/views/entries/edit.js.erb 处理点击编辑时的请求 在index页面的新建entry的form中填充要编辑entry的数据。

$("#form > form").replaceWith("<%= j render "form" %>");

新建 app/views/entries/update.js.erb 来更新列表中的Entry对象。新建Entry并清空form

$("#<%= dom_id @entry %>").replaceWith("<%= j render @entry %>");
<% @entry = Entry.new # reset for new form %>
$(".edit_entry").replaceWith("<%= j render "form" %>")
$(".new_entry")[0].reset();

新建 app/views/entries/destroy.js.erb 来删除list中的Entry

$("#<%= dom_id @entry %>").remove();

5、设置主页

设置entries页面为主页。修改 config/routes.rb

AjaxCRUD::Application.routes.draw do
  resources :entries
  root :to => "entries#index"
end

删除静态首页文件。

rm public/index.html

6、启动程序

rails s

此时打开 http://127.0.0.1:3000 就可以使用了。

源代码托管在 https://github.com/camelsong/AjaxCRUD

英文原文地址:http://codefundas.blogspot.com/2010/12/create-ajax-based-curd-using-rails-3.html

LVM (简体中文)

LVM (简体中文)

 介绍

LVM 是一个应用于 Linux 内核的本地卷管理器 (Logical Volume Manager)。 使用 LVM 你可以抽象你的存储空间,并且可以有很容易更改的“虚拟分区”。LVM的基本模块如下:

  • Physical volume (PV): 物理卷,例如一个硬盘,或一个Software RAID设备; 硬盘的一个分区 (或者甚至硬盘本身或者回环文件),在它上面可以建立卷组。It has a special header and is divided into physical extents. Think of physical volumes as big building blocks which can be used to build your hard drive.
  • Volume group (VG): 卷组,将一组物理卷收集为一个管理单元;Group of physical volumes that are used as storage volume (as one disk). They contain logical volumes. Think of volume groups as hard drives.
  • Logical volume(LV): 逻辑卷,等同于传统分区,可看作便准的块设备,以容纳文件系统;A “virtual/logical partition” that resides in a volume group and is composed of physical extents. Think of logical volumes as normal partitions.
  • Physical extent (PE): 物理块,划分物理卷的数据块;A small part of a disk (usually 4MB) that can be assigned to a logical Volume. Think of physical extents as parts of disks that can be allocated to any partition.

使用 LVM 你可以比正常的硬盘分区更容易的管理硬盘分区(逻辑卷)。例如,你可以:

  • 使用卷组(VG),使众多硬盘空间看起来像一个大硬盘。
  • 使用逻辑卷(LV),可以创建跨越众多硬盘空间的分区。
  • 可以根据需要,对分区(LV)和硬盘空间(VG)进行创建、删除、调整大小等操作。(it doesn’t depend on position of the logical volumes within volume groups as with normal partitions)
  • Resize/create/delete partitions(LV) and disks(VG) online (filesystems on them still need to be resized, but some support online resizing)
  • Name your disks(VG) and partitions(LV) as you like
  • Create small partitions(LV) and resize them ”dynamically” as they get more filled (growing must be still done by hand, but you can do it online with some filesystems)

示例:

两块物理硬盘

  硬盘1 (/dev/sda):
     _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
    |分区1 50GB (Physical volume) |分区2 80GB (Physical volume)     |
    |/dev/sda1                   |/dev/sda2                       |
    |_ _ _ _ _ _ _ _ _ _ _ _ _ _ |_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ |

  硬盘2 (/dev/sdb):
     _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
    |分区1 120GB (Physical volume)                 |
    |/dev/sdb1                                    |
    | _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _|
LVM方式

  卷组VG1 (/dev/MyStorage/ = /dev/sda1 + /dev/sda2 + /dev/sdb1):
     _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _  
    |逻辑卷lv1 15GB         |逻辑卷lv2 35GB              |逻辑卷lv3 200GB               |
    |/dev/MyStorage/rootvol|/dev/MyStorage/usrvol     |/dev/MyStorage/homevol       |
    |_ _ _ _ _ _ _ _ _ _ _ |_ _ _ _ _ _ _ _ _ _ _ _ _ |_ _ _ _ _ _ _ _ _ _ _ _ _ _ _|

总而言之: With LVM you can use all your storage space as one big disk (volume group) and have more flexibility over your partitions (logical volumes).

Advantages

Here are some things you can do with LVM that you can’t (or can’t do easily) with just mdadm, MBR partitions, GPT partitions, parted/gparted and a file-level tool like rsync.

  1. Online/live partition resizing
  2. No need for an extended partition (not relevant for GPT)
  3. Resize partitions regardless of their order on disk (no need to ensure surrounding available space)
  4. Online/live migration of partitions being used by services without having to restart services

These can be very helpful in a server situation, desktop less so, but you must decide if the features are worth the abstraction.

安装

在做其他工作之前,我们需要加载合适的模块:

# modprobe dm-mod

如果你已经安装好了操作系统,并只是想利用增加或尝试一个LVM分区,请跳到这 partition disks.

在 LVM 上安装 Arch Linux

在开始安装arch之前(即输入:/arch/setup之前),先使用cfdisk等工具来规划分区。因为grub不能从LVM逻辑卷引导启动 (版本1.0时),所以需要先创建一个/boot引导区,100MB应该够了。另外的解决办法就是使用lilo或者高于1.95版本的grub。

创建 LVM 分区

接下来,要创建LVM将使用的分区。文件类型使用’Linux LVM’,所以使用分区id 0x8e (文件系统类型:8E)。在需要使用LVM的每块硬盘上,各创建一个LVM分区。 Your logical volumes will reside inside these partitions so size them accordingly. If you will use only LVM and no other external partitions, use all of free space on each disk.

警告: /boot不能包含在LVM分区里,因为grub (version<1.95)不能从LVM中引导grub。
小贴士: 所有硬盘的所有LVM分区可以被设置成看起来就像一个大硬盘。

创建物理卷(PV)

接下来,要加载使用lvm所需的相应模块:

# modprobe dm-mod

用命令’fdisk -l’查看那个分区的文件系统类型是’Linux LVM’,然后在其上创建一个物理卷组pv(假设是/dev/sda2),输入如下命令:

# pvcreate /dev/sda2

Substitute /dev/sda2 with all your partitions to create physical volumes on all of them. This command creates a header on each partition so it can be used for LVM. 查看物理卷情况:

# pvdisplay

创建卷组(VG)

创建完成物理卷之后,就是开始创建卷组了。 如果有两个以上的物理卷pv(比如下面例子,有两个/dev/sda2和/dev/sdb1),首先必须先在其中一个创建一个卷组vg,然后让该卷组vg 扩大到其他所有的物理卷pv(这里假设你只使用一个卷组vg来管理其他所有的物理卷pv。):

# vgcreate VolGroup00 /dev/sda2
# vgextend VolGroup00 /dev/sdb1

其中,“VolGroup00”名字换成你自己起的名字即可。接下来看看卷组情况:

# vgdisplay
注意: 可以创建多于一个的卷组,但因此将让你的硬盘空间看起来不像一块硬盘。

创建逻辑卷(LV)

创建完卷组vg之后,就可以开始创建逻辑卷了。输入下面命令:

# lvcreate -L 10G VolGroup00 -n lvolhome

其中10G是大小,VolGroup00是卷组vg名称,lvolhome是逻辑卷lv名称,这些都可以根据你自己喜欢设定,以后可以使用/dev/mapper/Volgroup00-lvolhome 或者 /dev/VolGroup00/lvolhome来操作.

查看逻辑卷情况:

# lvdisplay
注意: You may need to load the device-mapper kernel module (modprobe dm-mod) for the above commands to succeed.
小贴士: 一开始可以创建小一点的逻辑卷lv,然后留一部分未使用空间在卷组vg里,以后可以根据需要再扩展各个逻辑卷。

建立文件系统与挂载逻辑卷

Your logical volumes should now be located in /dev/mapper/ and /dev/YourVolumeGroupName. If you can’t find them use the next commands to bring up the module for creating device nodes and to make volume groups availabile:

# modprobe dm-mod
# vgscan
# vgchange -ay

Now you can create filesystems on logical volumes and mount them as normal partitions (if you are installing Arch linux, skip this step):

# mkfs.ext3 /dev/mapper/VolGroup00-lvolhome
# mount /dev/mapper/VolGroup00-lvolhome /home

如果你正在安装Archlinux,到 Prepare Hard Drive 这一步时,转到第三项 Set Filesystem Mountpoints ,请 在进入安装前,阅读下面的 重要部分 !

重要

有几点在使用/安装带有 LVM 的 Arch Linux 时你需要特别注意的地方。(括号里是相关的安装过程中的菜单):

设置文件系统挂载点

  • 当选择挂载点时(除了/boot),千万不要选择实际存在的逻辑卷(比如:/dev/sda2),只需选择由lv创建的逻辑卷(比如: /dev/mapper/Volgroup00-lvolhome)。

配置系统

  • 确保在 /etc/rc.conf 中,把USELVM="no"修改成 USELVM="yes"rc.sysinit 脚本处理 USELVM 变量时只会识别yes 或者 YES,不支持大小写混合。
  • 确保 lvm2/etc/mkinitcpio.conf HOOKS 部分的 filesystems 前面,这样您的内核就可以在启动时找到 LVM 卷。
  • If your root filesystem ( “/” ) is put onto a logical volume, make sure regenerate kernel image based on above modified /etc/mkinitcpio.conf by using below command so that bootloader can find your root during booting
     cd /boot 
     mkinitcpio -p linux
  • 确保为 root 使用了正确的卷。
对于 GRUB,编辑/boot/grub/menu.lst
     ...
     (0) Arch Linux
     title  Arch Linux
     root   (hd0,0)
     kernel /vmlinuz-linux root=/dev/mapper/VolGroup00-lvolroot resume=/dev/mapper/VolGroup00-lvolswap ro
     initrd /initramfs-linux.img
     ...
For SYSLINUX, edit /boot/syslinux/syslinux.cfg:
     ...
     # (0) Arch Linux
     LABEL arch
       MENU LABEL Arch Linux
       LINUX ../vmlinuz-linux
       APPEND root=/dev/mapper/VolGroup00-lvolroot ro
       INITRD ../initramfs-linux.img
对于 LILO ,检查 /etc/lilo.conf:
     ...
     image=/boot/vmlinuz-linux
       label=arch
       append="root=/dev/mapper/VolGroup00-lvolroot resume=/dev/mapper/VolGroup00-lvolswap ro"
       initrd=/boot/initramfs-linux.img
     ...

配置

扩大逻辑卷

To grow a logical volume you first need to grow the logical volume and then the filesystem to use the newly created free space. Let’s say we have a logical volume of 15GB with ext3 on it and we want to grow it to 20G. We need to do the following steps:

# lvextend -L 20G VolGroup00/lvolhome (or lvresize -L +5G VolGroup00/lvolhome)
# resize2fs /dev/VolGroup00/lvolhome

You may use lvresize insted of lvextend.

If you want to fill all the free space on a volume group use the next command:

# lvextend -l +100%FREE VolGroup00/lvolhome
警告: 并非所有的文件系统都支持无损或在线扩大逻辑卷。
注意: If you do not resize your filesystem, you will still have a volume with the same size as before (volume will be bigger but partly unused).

缩小逻辑卷

Because your filesystem is probably as big as the logical volume it resides on, you need to shrink the filesystem first and then shrink the logical volume. Depending on your filesystem, you may need to unmount it first. Let us say we have a logical volume of 15GB with ext3 on it and we want to shrink it to 10G. We need to do the following steps:

# resize2fs /dev/VolGroup00/lvolhome 9G
# lvreduce -L 10G VolGroup00/lvolhome (or lvresize -L -5G VolGroup00/lvolhome)
# resize2fs /dev/VolGroup00/lvolhome

Here we shrunk the filesystem more than needed so that when we shrunk the logical volume we did not accidentally cut off the end of the filesystem. After that we normally grow the filesystem to fill all free space left on logical volume. You may use lvresize instead of lvreduce.

Warning:
  • Do not reduce the filesystem size to less than the amount of space occupied by data or you risk data loss.
  • Not all filesystems support shrinking without loss of data and/or shrinking online.
Note: It is better to reduce the filesystem to a smaller size than the logical volume, so that after resizing the logical volume, we do not accidentally cut off some data from the end of the filesystem.

Remove logical volume

Warning: Before you remove a logical volume, make sure to move all data that you want to keep somewhere else, otherwise it will be lost!

First, find out the name of the logical volume you want to remove. You can get a list of all logical volumes installed on the system with:

# lvs

Next, look up the mountpoint for your chosen logical volume…:

$ df -h

… and unmount it:

# umount /your_mountpoint

Finally, remove the logical volume:

# lvremove /dev/yourVG/yourLV

Confirm by typing “y” and you are done.

Dont forget, to update /etc/fstab:

# sudo nano /etc/fstab

You can verify the removal of your logical volume by typing “lvs” as root again (see first step of this section).

添加分区到卷组中

To add a partition to your volume group you must first make its type ‘Linux LVM’ (for example with cfdisk). Then you need to create a physical volume on it and extend the volume group over it:

# pvcreate /dev/sdb1
# vgextend VolGroup00 /dev/sdb1

Now you have free space in your volume group that can be used by logical volumes in this group.

Tip: You can add partitions from any disks to volume groups.

从卷组中移除卷

All of the data on that partition needs to be moved to another partition. Fortunately, LVM makes this easy:

# pvmove /dev/sdb1

If you want to have the data on a specific physical volume, specify that as the second argument to pvmove:

# pvmove /dev/sdb1 /dev/sdf1

Then the physical volume needs to be removed from the volume group:

# vgreduce myVg /dev/sdb1

Or remove all empty physical volumes:

# vgreduce --all vg0

And lastly, if you want to use the partition for something else, and want to avoid LVM thinking that the partition is a physical volume:

# pvremove /dev/sdb1

快照功能

介绍

LVM可以给系统创建一个快照,由于使用了COW (copy-on-write) 策略,相比传统的备份更有效率。 The initial snapshot you take simply contains hard-links to the inodes of your actual data. So long as your data remains unchanged, the snapshot merely contains its inode pointers and not the data itself. Whenever you modify a file or directory that the snapshot points to, LVM automatically clones the data, the old copy referenced by the snapshot, and the new copy referenced by your active system. 这样的话,如果你只修改了不超过2G数据(包括原始的和快照的),你将可以只使用2G的空间,就能快照一个有35G的数据的系统。

配置

You create snapshot logical volumes just like normal ones.

# lvcreate --size 100M --snapshot --name snap01 /dev/mapper/vg0-pv

With that volume, you may modify less than 100M of data, before the snapshot volume fills up.

Todo: scripts to automate snapshots of root before updates, to rollback… updating menu.lst to boot snapshots (separate article?)

snapshots are primarily used to provide a frozen copy of a filesystem to make backups; a backup taking two hours provides a more consistent image of the filesystem than directly backing up the partition.

常见问题

LVM 命令不起作用

try preceeding commands with lvm like this:

# lvm pvdisplay

设定文件系统挂载点的页面不显示逻辑卷

If you are installing on a system where there is an existing volume group, you may find that even after doing “modprobe dm-mod” you don’t see the list of logical volumes.

In this case, you may also need to do:

# vgchange -ay <volgroup>

in order to activate the volume group and make the logical volumes available.

Receiving Input/Output Errors after plugging in a removable device with LVM partitions

Symptoms:

~$ sudo vgscan
 Reading all physical volumes.  This may take a while...
 /dev/backupdrive1/backup: read failed after 0 of 4096 at 319836585984: Input/output error
 /dev/backupdrive1/backup: read failed after 0 of 4096 at 319836643328: Input/output error
 /dev/backupdrive1/backup: read failed after 0 of 4096 at 0: Input/output error
 /dev/backupdrive1/backup: read failed after 0 of 4096 at 4096: Input/output error
 Found volume group "backupdrive1" using metadata type lvm2
 Found volume group "networkdrive" using metadata type lvm2

Cause:

Removing an external LVM drive without deactivating the volume group(s) first. Before you disconnect, make sure to:
# vgchange -an <volume group name>

Fix: (assuming you already tried to activate the volume group with vgchange -ay <vg>, and are receiving the Input/output errors

# vgchange -an <volume group name>
Unplug the external drive and wait a few minutes
# vgscan
# vgchange -ay <volume group name>

技巧

更多资源

archwiki的其他关于LVM的文章:

外部资源:

Rails - Devise - Handling - Devise_error_messages

rails – Devise – Handling – devise_error_messages

in my user edit page, there is a line as follows:

<%= devise_error_messages! %> 

The problem is this does not output errors the stand way that the rest of the app does:

<% flash.each do |key, value| %>     <divpun" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; border-image: initial; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: rgb(0, 0, 0); "><%= key %>"><%= value %></div> <% end %> 

My question, is how to I get the devise error message to work like the others that use the flash.each?

Thanks

I’m trying to figure this out myself. I just found this issue logged on Githubhttps://github.com/plataformatec/devise/issues/issue/504/#comment_574788

Jose is saying that devise_error_messsages! method is just a stub (though it contains implementation) and that we’re supposed to override/replace it. It would have been nice if this was pointed out somewhere in the wiki, which is why i guess there are a few people like us that have been guessing.

So I’m going to try reopening the module and redefine the method, effectively overriding the default implementation. I’ll let you know how it goes.

Update

Yep, that works. I created app/helpers/deivse_helper.rb and overrode it like so:

module DeviseHelper   def devise_error_messages!     'KABOOM!'   end end 

So knowing this, I can modify the method to display error messages the way I want it to.

To help you solve your original problem: Here’s the original devise_helper.rb on Github. Take a look at how the error messages are being traversed, specifically Line 5.

messages = resource.errors.full_messages.map { |msg| content_tag(:li, msg) }.join 

That should help you get started. :)

Another update

The resource object is actually the model that is being used by devise (go figure).

resource.class         #=> User resource.errors.class  #=> ActiveModel::Error 

It also appears to be defined in a higher scope (probably coming from the controller), so it can be accessed in a variety of places.

Anywhere in your Helper

module DeviseHelper   def devise_error_messages1!     resource.errors.full_messages.map { |msg| content_tag(:li, msg) }.join   end   def devise_error_messages2!     resource.errors.full_messages.map { |msg| content_tag(:p, msg) }.join   end end 

Your View

<div><%= resource.errors.inspect %></div>

More info look at http://stackoverflow.com/questions/4101641/rails-devise-handling-devise-error-messages

HOWTO : Easily Create a Boot-Info Summary

HOWTO : easily create a Boot-Info summary

Boot-Repair is a simple tool to repair frequent boot issues you may encounter in Ubuntu like when you can’t boot Ubuntu after installing Windows or another Linux distribution, or when you can’t boot Windows after installing Ubuntu, or when GRUB is not displayed anymore, some upgrade breaks GRUB, etc.

Boot-Repair lets you fix these issues with a simple click, which (generally reinstalls GRUB and) restores access to the operating systems you had installed before the issue.

Boot-Repair also has advanced options to backup table partitions, backup bootsectors, create a Boot-Info-Summary (to get help by email or forum), or change the default repair parameters: configure GRUB, add kernel options (acpi=off …), purge GRUB, change the default OS, restore a Windows-compatible MBR, repair a broken filesystem, specify the disk where GRUB should be installed, etc.

Boot-Repair is a free software, licensed under GNU-GPL. Boot-Repair should be soon included in Ubuntu official repositories, until then use it at your own risks.

http://pix.toile-libre.org/upload/original/1335260967.png

 

Getting Boot-Repair

 

1st option : get a CD including Boot-Repair

The easiest way to use Boot-Repair is to burn one of the following disks and boot on it.

  • Boot-Repair-Disk is a CD starting Boot-Repair automatically. (English only, 32&64bits compatible, based on Debian-live so Wifi drivers are not recent).
  • Boot-Repair is also included in Ubuntu-Secure-Remix (multi-languages, ok for Wifi, based on Ubuntu 12.04 LTS, run Boot-Repair from the Dash)

Remark : you can also install the ISO on a live-USB (eg via UnetBootin, LiliUSB or MultiSystem).

 

2nd option : install Boot-Repair in Ubuntu

Remark: this can also be performed from a live-CD or live-USB.

Either add ‘ppa:yannubuntu/boot-repair’ to your Software Sources via the Software Centre or, for speeds-sake, add it using a new Terminal session:

 

sudo add-apt-repository ppa:yannubuntu/boot-repair && sudo apt-get update
sudo apt-get install -y boot-repair && boot-repair

Boot-Repair can be installed & used from any Ubuntu session (normal session, or live-CD, or live-USB). PPA packages are available for Ubuntu 10.04, 10.10, 11.04, 11.10 and 12.04.

 

Adding this PPA to your system

You can update your system with unsupported packages from this untrusted PPA by adding ppa:yannubuntu/boot-repair to your system’s Software Sources. (Read about installing)

This PPA can be added to your system manually by copying the lines below and adding them to your system’s software sources.

Display sources.list entries for:
deb http://ppa.launchpad.net/yannubuntu/boot-repair/ubuntu precise main
deb-src http://ppa.launchpad.net/yannubuntu/boot-repair/ubuntu precise main
Signing key:
1024R/60D8DA0B (What is this?)

Using Boot-Repair

 

  • launch Boot-Repair from either :
    • the dash (Unity)
    • System->Administration->Boot-Repair menu (Gnome)
    • by typing ‘boot-repair’ in a terminal
  • Then try “Recommended repair” button. When repair is finished, note the URL that appeared on a paper, then reboot and check if you recovered access to your OSs.

If the repair did not succeed, indicate the URL to people who help you by email or forum.

 

Advanced options

http://pix.toile-libre.org/upload/img/1335263156.png http://pix.toile-libre.org/upload/img/1335263804.png http://pix.toile-libre.org/upload/img/1335263271.png http://pix.toile-libre.org/upload/img/1335263417.png http://pix.toile-libre.org/upload/img/1335263366.png

 

External Links

Find_city

find_city

  1. base on sina api

 

  require ‘json’

class FindCity CityHash = JSON.parse curl http://api.t.sina.com.cn/provinces.json def initialize

@cityhash=CityHash
@municipality=[6,11,12,50,81,82]

end def getcity(province_id=nil, city_id=nil)

province_id.to_i
city_id.to_i
if @municipality.include? province_id
  @cityhash["provinces"].each do |provinces|
    if provinces["id"] == province_id
      return provinces["name"]
    end
  end
else
  @cityhash["provinces"].each do |provinces|
    if provinces["id"] == province_id
      provinces["citys"].each do |city|
        return city[city_id.to_s] if city[city_id.to_s]
      end
    end
  end
end
return nil

end end

if FILE==$0 #*******以下测试代码 time_start = Time.now city = FindCity.new list=[[6,2],[11,1],[43,2]]

list.each do |l|

puts city.getcity l[0],l[1]

end

puts “total time:#{Time.now-time_start}” end

Parse a Web Page and Extract Some Json Arrays

Parse a web page and extract some json arrays

So I have some basic code below, which fetches the json from http://www.highcharts.com/demo/. But I want to be able to extract a hash, more specifically this:

series: [{
                    name: 'Tokyo',
                    data: [7.0, 6.9, 9.5, 14.5, 18.2, 21.5, 25.2, 26.5, 23.3, 18.3, 13.9, 9.6]
                }, {
                    name: 'New York',
                    data: [-0.2, 0.8, 5.7, 11.3, 17.0, 22.0, 24.8, 24.1, 20.1, 14.1, 8.6, 2.5]
                }, {
                    name: 'Berlin',
                    data: [-0.9, 0.6, 3.5, 8.4, 13.5, 17.0, 18.6, 17.9, 14.3, 9.0, 3.9, 1.0]
                }, {
                    name: 'London',
                    data: [3.9, 4.2, 5.7, 8.5, 11.9, 15.2, 17.0, 16.6, 14.2, 10.3, 6.6, 4.8]
                }]
            });

 

Into to a hash so that I can access the different data points. Currently the script just spits out everything. Code below:

require "json"
require "open-uri"


$LOAD_PATH << File.dirname(__FILE__)

result = JSON.parse(open("http://www.highcharts.com/demo/").read)

 

There is no direct conversion since source is JavaScript code (not even valid JSON). There are many ways to accomplish this task (one ways more strict than others, generic escaping control may be tricky), but that’s how I’d do it: HTML –> JS –> JSON –> Ruby array.

require 'open-uri'
require 'json'

html = open("http://www.highcharts.com/demo/").read
js = html.match(/series: (\[\{.*?\}\])/m)[1]
json = js.gsub(/(\w+):/i, '"\1":').gsub(/'/, '"')
series = JSON.parse(json)
# => [{"name"=>"Tokyo", "data"=>[7.0, 6.9, 9.5, 14.5, 18.2, ... 

 

Devise Logout Link Non-functional

Devise logout link non-functional

Resources: http://stackoverflow.com/questions/6110047/rails-devise-override-sessionscontroller

the problem: In a nutshell, when I try to install a logout link to my app it fails to work. Here’s as much context as I can think to put here (if you want anything else, please poke me)…

I’ve got this in a haml view:

= link_to("Logout", destroy_user_session_path, :method => :delete) 

It generates this in the view:

<a href="/users/sign_out" data-method="delete" rel="nofollow">Logout</a> 

I verified that in my config/initializers/devise.rb I have this setting uncommented and correct:

config.sign_out_via = :delete 

I validated the following route:

destroy_user_session DELETE /users/sign_out(.:format) {:action=>"destroy", :controller=>"devise/sessions"} 

I also have this bit of trickery in my routes.rb, and I suspect this is related to my issue:

devise_for :users, :controllers => {:sessions => "devise/sessions", :registrations => "users"} resources :users 

This last bit is because I want to manage (edit, create and delete) users in my own controller.

The error message I’m getting is as follows:

ActiveRecord::RecordNotFound in UsersController#show Couldn't find User with ID=sign_out Rails.root: /home/jaydel/projects/mbsquared-projects/Wilson-Goldrick app/controllers/users_controller.rb:16:in `show' 

In my server logs I see this for the request:

Started GET "/users/sign_out" for 127.0.0.1 at 2011-08-04 13:08:51 -0500   Processing by UsersController#show as HTML   Parameters: {"id"=>"sign_out"} 

Outside it’s slightly overcast and in the mid 80s. I’m wearing a red shirt.

Anyone have any ideas?

1

The problem lies in the fact that in your logs the signout request is a GET request.
Started GET "/users/sign_out" 
But the signout route is a DELETE
destroy_user_session DELETE /users/sign_out(.:format) 
The reason why you are getting the exception is that is it getting confused with one of the routes created by resources :users which would be something like
edit_user GET /users/(:id)(.:format) {:action=>"edit", :controller=>"users"} 
Basically ‘sign_out’ is being mistaken as a id. I’m not sure why the delete link is not going through as a DELETE request. Though changing
config.sign_out_via = :delete 
to be :get might solve the problem.

2

I think the more correct way to fix this, REST-wise, would be to change your logout links to use the DELETE method. It’s a very easy fix, changing this: link_to “Log out”, destroy_user_session_path to this: link_to “Log out”, destroy_user_session_path, :method => :delete

3

I had the same problem with rails 3.2 when I deleted from application.js this line:
//= require jquery_ujs 
So, I think you have to insert this line in your application.js if you haven’t it there. PS. This behavior means that rails adapter for jquery doesn’t function. So you should make sure if it is loaded in your html in browser. You should test it in development mode because you will have compressed js in production and it will be very difficult to find something there.