开发者

Ruby on Rails - get MySql DB size

开发者 https://www.devze.com 2023-03-01 23:57 出处:网络
I want to know the current size of my MySql DB, to get that data I use the following methods: def self.calculate_total_db_size

I want to know the current size of my MySql DB, to get that data I use the following methods:

def self.calculate_total_db_size
    sql = "开发者_StackOverflowSELECT table_schema AS 'database',
                  sum( data_length + index_length ) / ( 1024 *1024 ) AS size
                  FROM information_schema.TABLES
                  WHERE ENGINE=('MyISAM' || 'InnoDB' )
                  AND table_schema = '#{get_current_db_name}'"
    return  perform_sql_query(sql)
  end



def self.get_current_db_name    
    return Rails.configuration.database_configuration[Rails.env]["database"]
end

def self.perform_sql_query(query)
     result = []
     mysql_res = ActiveRecord::Base.connection.execute(query)
     mysql_res.each_hash{ |res| result << res }
     return result
end

this works great in my development and staging environment, but from some reason when i run it in production the query doesnt return any value, if i take the MySql query and run it manually on my production DB I get the correct values. why cant i do it through the application in production?

any thoughts?


I added some more logs and that helped me pinpoint the problem, I was using: Rails.configuration.database_configuration[Rails.env]["database"] which returns an empty string when I was in production and not in any other environment, I guess it is because in my database.yml there's a link to the development setting under production (what makes the production settings the same as the dev). anyway, since i dont want to change my database.yml file i just changed the way im getting the database name. now it works great.


I'll wager $1 it's a permissions issue in production. Log into your production server and fire up the db console (go into your rails dir and type rails db to ensure you're using the same user as your app) and try to select from tables in the information_schema database.

The mysql account you're using in production most likely doesn't have access to the records you want in the information_schema database.

0

精彩评论

暂无评论...
验证码 换一张
取 消