How do I tell DBD::mysql where mysql.sock is? - mysql

Using DBD::mysql with DBI, I am getting the following error when attempting to connect to the database.
DBI connect('database=mydb:host=localhost','someuser',...) failed: Can't connect
to local MySQL server through socket '/tmp/mysql.sock' (2) at ./myscript.pl line 97
Yes MySQL is up and running. The problem is that mysql.sock is not in /tmp.
I know the location of mysql.sock and I currently have it hacked so that it works, I created a soft link to the current location of the mysql.sock file. I would rather not change the MySQL configuration, though this would probably be the easiest thing to do.
Is there a way to go into DBD::mysql and configure it to look for mysql.sock in the correct location?

You can specify the location of the socket in the connect method
my $dbh = DBI->connect("DBI:mysql:database=dbname;host=localhost;mysql_socket=/path/to/mysql.sock","someuser","somepassword", {'RaiseError' => 1});
For further information please have a look at the docs here

$dbh=DBI->connect("DBI:mysql:database=dbname;mysql_socket=/var/lib/mysql/mysql.sock;user=username;password=password");
You don't need to specify host since you're connecting to a socket file.

Related

Manjaro Linux: I can't run Mariadb because missing socket

Well, I have Manjaro/Linux 17.0, and I've tried to install MariaDB but I always get the same error:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/run/mysqld/mysqld.sock' (2 "No such file or directory")
I don't know what to do, I've search in thousands of forums, I can't start the mariadb's deamon with systemctl, I tried to do a touch at /run/mysqld/mysqld.sock but then it refused my connection when I tried to start mariadb, and I tried to uninstall/reinstall MariaDB, but it's the same thing :(
In all my directories I can't find mysql.sock or similar.
Help me please!
I had the same problem, the solution which worked for me is https://serverfault.com/a/813123 , I just cleaned:
sudo rm /var/lib/mysql/*
then initialised the database:
mysql_install_db --user=mysql --basedir=/usr --datadir=/var/lib/mysql
It seems like there might be a difference between what the server is using as the socket and what the client thinks should be the socket.
You can check the process list (ps -ef | grep mysql) and see if the server is running with a different socket setting than you expect. (Look for --socket=.)
Edit your /etc/my.cnf, find or make a [client-server] section, and set socket to a file that makes sense. It should be in a directory that users have access to. The default setting is socket=/tmp/mysql.sock, so that should be a safe bet. Then remove any other socket setting elsewhere in /etc/my.cnf or files in the /etc/my.cnf.d/ directory. Then restart, and it should hopefully work.
Also check if you have a personal config file ~/.my.cnf, look for a socket setting in there, and if so remove it.
For reference, see the socket entry in the MariaDB KB. Also, have a look at the full list of locations where MariaDB config files could be stored here.

Can't connect to local MySQL server through socket (From time to time)

I have a LAMP stack setup. Occasionally, I get the following error message when I open some page from the browser:
Error creating the connection!: SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
I think the server was configured correctly. The problem happens about every two months. Every time when I reboot the Linux server, or restart mysql, the problem was gone. I want to fix this problem permanently. Can anyone give me some idea?
Much appreciated.
EDIT
The problem occurs again and I checked the mysqld.sock file, it was not there. Do you have any idea how to fix the problem? – Ryan Jul 23 at 16:24
If your file my.cnf (usually in the /etc/mysql/ folder) is correctly configured with
socket=/var/run/mysqld/mysqld.sock
modified
#bind-address = 127.0.0.1
to
bind-address = localhost
you can check if mysql is running with the following command:
mysqladmin -u root -p status
try changing your permission to mysql folder. If you are working locally, you can try:
sudo chmod -R 755 /var/run/mysqld/
And then restart the mysql.
Good luck.
Could it be the log file getting too large and rebooting flushes it. See this in docs on server maintenance and logfiles. Also see discussion at digital ocean. Appears to be confirmed by discussion at serverfault
You could try change the permission of your MySQL sock file like this:
chmod 777 '/var/run/mysqld/mysqld.sock'
It is a test to see if whatever user mysqld is using, it will acess your mysqld.sock file. So, reboot your MySQL and change the permission of mysqld.sock. And you need to check that if your sock folder can be accessed through any mysqld process.
If the mysqld.sock file doesn't exist, that is to say your config file is not correct.Check your mysql config file in /etc/mysql/my.conf, find the socket config just as Vanya Avchyan says.
I think the socket config is /var/run/mysqld/mysqld.sock, but in fact your mysql process runs in other place sock file.
I used to met that problem, the real socket file exists in /tmp/mysqld.sock.
So run
sudo find / -name 'mysqld.sock'
to find the real sock file and change my.conf to this real place, restart your mysql. May have work.

app can't connect to local MySQL - osx

I am running some rails app on osx, but when I launch a rails generate command type, I get this message :
/$root/vendor/bundle/ruby/2.0.0/gems/mysql2-0.3.11/lib/mysql2/client.rb:44:in `connect': Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) (Mysql2::Error)
For info, my mamp server is running, and the connection must pass through /Applications/MAMP/Library/bin/mysql, so I aliased it by setting in my bash profile :
alias mysql="/Applications/MAMP/Library/bin/mysql"
Btw, what is weird, is that my rails s command works perfectly, so my app can connect to the apache server in that case
So the trick was to simply /tmp/mysql.sock to /Applications/MAMP/tmp/mysql/mysql.sock
ln -s /Applications/MAMP/tmp/mysql/mysql.sock /tmp/mysql.sock
The error indicates that "mysql is not started/running". From what you describe, looks like the new location pointed to the alias has no mysql or its mysql is not started.
Look my.cnf file with the correct parameters in the new location:
/Applications/MAMP/Library/bin/mysql
Another trick is to list your running processes and look for mysql.
Also, are you sure, "rails c" is not using the sqlite3 vs mysql?
Updated:
*Courtesy of #bfavaretto
MySQL my.cnf location on OS X?
By default, the OS X installation does not use a my.cnf, and MySQL just uses the default values. To set up your own my.cnf, you could just create a file straight in /etc.
OS X provides example configuration files at /usr/local/mysql/support-files/
Update:
Take a look at this:
https://stackoverflow.com/questions/4788381/getting-cant-connect-through-socket-tmp-mysql-when-installing-mysql-on-m

Doctrine 2 command line tool; MAMP and mysql.sock

I grabbed the CI2/Doctrine2 from Wildly Inaccurate. I'm trying to start using it, but before I'm actually getting to work with it, I wanted to make sure everything works. However, when I try the command line tool, I run into the following error:
$ ./doctrine orm:schema-tool:create
ATTENTION: This operation should not be executed in a production environment.
Creating database schema...
Warning: PDO::__construct(): [2002] Socket operation on non-socket (trying to connect via unix:///var/mysql/mysql.sock) in /Users/joris/Desktop/ci2_starter/application/libraries/Doctrine/DBAL/Driver/PDOConnection.php on line 36
[PDOException]
SQLSTATE[HY000] [2002] Socket operation on non-socket
orm:schema-tool:create [--dump-sql]
I'm running this on a cleanly installed MAMP Pro 2.0.1; I'm not really sure what to do to fix this error.. I'm not really knowledgeable on the inner (background) workings of MySQL. Could someone clear this up for me? Thanks a lot.
Update:
MacBook-Pro-van-Joris-Ooms:~ joris$ sudo find / -name 'mysql.sock'
Password:
/Applications/MAMP/tmp/mysql/mysql.sock
find: /dev/fd/3: Not a directory
find: /dev/fd/4: Not a directory
/private/var/mysql/mysql.sock
/private/var/mysql/mysql.sock/mysql.sock
MAMP does use it's own mysql socket while the CLi uses the default one in /var/mysql/mysql.sock.
This could mean you are running the wrong PHP version (not the MAMP one but the default MAC OS X one). Not a big deal since it can deal with your Doctrine Command Line Issues as well.
So you do have several options:
try using the MAMP php version to start the CLi tool by putting the Path to your MAC OS X php version as a prefix i.e.
/Applications/MAMP/bin/php5.3/bin/php ./doctrine orm:schema-tool:create
just set up a link from /var/mysql/mysql.sock to the actual Socket file which should be in /Applications/MAMP//tmp/mysql/mysql.sock
So you go to the terminal while your MAMP is running an type (You will be asked to type in a password):
sudo ln -s /Applications/MAMP//tmp/mysql/mysql.sock /var/mysql/mysql.sock
Find every occurrence of "/Applications/MAMP//tmp/mysql/mysql.sock" in any of your MAMP config files and change it to be /var/mysql/mysql.sock so you actually do use the default address for MySQL Socket. Though, i have never tested if this does work.

Error 2002: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

I'm on Ubuntu 11.04. Everything works PHP, PHPMYADMIN, manual login to MySQL etc.
I have written a C application that uses MySQL. Now, when I start this application I receive the above error. I tried to prevent that from happening by linking the original file into the /tmp/ folder (ln -s /var/run/mysqld/mysqld.sock /tmp/mysql.sock), however, after a while I start getting this error again - because my link has been removed, which is frustating.
What do I need to do to make this work?
The MySQL libraries are compiled with a default of /tmp/mysql.sock. The server is started up with /var/run/mysqld/mysql.sock and your $HOME/.my.cnf does not reflect this value in the [client] section.
If this is run without a valid $HOME you may need to use mysql_options with MYSQL_READ_DEFAULT_FILE.
Try linking the file in other location than /tmp/ and use it from there