0

I have mysql server installed and it contains data also. I was trying to connect to mysql from ruby code. For that purpose I installed mysql gem using gem install mysql. The connection part in my program is giving below

require 'rubygems'
require 'mysql'
require 'csv'
$user_name = "root"
$pas_wrd = "password"
def mysql_connect(db_name)
begin
  db = Mysql.real_connect("localhost", "#{$user_name}", "#{$pas_wrd}", "#{db_name}")
  return db
rescue Mysql::Error => e
  puts "Error code: #{e.errno}"
  puts "Error message: #{e.error}"
  puts "Error SQLSTATE: #{e.sqlstate}" if e.respond_to?("sqlstate")
end
end 

It was written some day before and done the job. But when I try to use the same code It shows a mysql gem error,something like 'initialize' not found. By searching I found a solution like re-installing the gem. for re-installing I user the command rvmsudo gem install mysql. The above problem was solved. But it showing another error like

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

I tried manually using mysql -u root -p

it is also showing the same error.

My mysql my.cnf content is given below.

[client]
port            = 3306
socket          = /var/run/mysqld/mysqld.sock

[mysqld_safe]
socket          = /var/run/mysqld/mysqld.sock
nice            = 0

[mysqld]
user            = mysql
pid-file        = /var/run/mysqld/mysqld.pid
socket          = /var/run/mysqld/mysqld.sock
port            = 3306
basedir         = /usr
datadir         = /var/lib/mysql
tmpdir          = /tmp
lc-messages-dir = /usr/share/mysql
tmpdir          = /home/presoft/Documents
skip-external-locking

The mysql is not running in my system.

ps aux | grep mysql
presoft   3823  0.0  0.0   4368   816 pts/0    S+   10:41   0:00 grep --color=auto mysql

I tried to start using using sudo /etc/init.d/mysql start which gave the result of

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

Since the script you are attempting to invoke has been converted to an
Upstart job, you may also use the start(8) utility, e.g. start mysql
start: Job failed to start

Can any one help me to find a solution because db containing large amount of valuable data.

Thanks

Regards.

4

2 回答 2

0

利用

service mysql start

这将启动 mysql 服务。较新的 linux 机器将 mysql 视为服务

于 2013-02-14T05:26:42.547 回答
0

我从上面的答案提示中解决了这个问题。问题是我的 mysql 服务器已损坏。

所以我首先运行命令

sudo apt-get purge mysql-common mysql-server

它将删除所有 mysql 文件和配置而不删除数据。运行后请仔细观察过程并做需要充分的任何警告或错误来。我收到了类似的警告

dpkg: warning: while removing mysql-common, directory '/etc/mysql' not empty so not removed.

所以我删除了文件并重新运行上述命令。执行成功后。我使用重新安装了mysql

sudo apt-get install mysql-common mysql-server 

然后设置root密码。进入服务器后数据是安全的。

于 2013-02-14T07:46:47.960 回答