问题描述
我试图查询我的mysql数据库。我使用数据库
菜单,并可以与我的数据库建立连接。我试图查询我的数据库的信息,现在的问题是如何存储比信息,所以我可以访问它在另一个资源。查询的结果存储在哪里?这是我的食谱:
I am trying to query my mysql database. I am using the database
cookbook and can setup a connection with my database. I trying to query my database for information so now the question is how do I store than information so I can access it in another resource. Where do the results of the query get stored? This is my recipe:
mysql_database "Get admin users" do
connection mysql_connection_info
sql "Select * from #{table_name}"
action :query
end
推荐答案
如果你没有Ruby的经验,这可能会让人困惑。没有办法返回来自Chef资源的提供程序的结果。 mysql_database
是一个 Chef :: Recipe
DSL方法,转换为 Chef :: Provider :: Database :: Mysql
。此提供者是中定义的
If you don't have experience with Ruby, this might be really confusing. There's no way to "return" the result of a provider from a Chef resource. The mysql_database
is a Chef::Recipe
DSL method that gets translated to Chef::Provider::Database::Mysql
at runtime. This provider is defined in the cookbook.
如果你花一些时间来深入这个提供者,你可以看到它如何执行查询,使用。要获取查询的结果,您需要在配方中创建自己的连接对象,并对其执行命令。例如
If you take some time to dive into that provider, you'll can see how it executes queries, using the db
object. In order to get the results of a query, you'll need to create your own connection object in the recipe and execute a command against it. For example
require 'mysql'
db = ::Mysql.new('host', 'username', 'password', nil, 'port', 'socket') # varies with setup
users = db.query('SELECT * FROM users')
#
# You might need to manipulate the result into a more manageable data
# structure by splitting on a carriage return, etc...
#
# Assume the new object is an Array where each entry is a username.
#
file '/etc/group' do
contents users.join("\n")
end
这篇关于存储mysql查询信息chef的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!