unable to connect to mysql socket from C program - mysql

I am developing a C program that accesses mysql. I have the following line in my code:
sts = connect_to_server(&mysql, "localhost", "my_username",
"my_password, "my_databasename");
with the actual values replaced, of course. I get:
Failed to connect to MySQL: Error: Can't connect to local MySQL server
through socket '/tmp/mysql.sock' (2)
I have seen other questions like this, and the answer is usually that the server is down. In this case it is not. PHPMyAdmin can access the server just fine.
And many of the other responses said to check for the socket file. It is present
(/Applications/AMPPS/mysql/tmp/mysql.sock).
Could this be something to do with paths or something? It isn't adding the path to "/temp/mysql.sock" and therefore can't find it?
I know very little about unix. I have been running OSX for nearly 10 years, but I almost never go down to that level.

Take a look into the mysql configuration file (typical my.cnt somehwhere under /etc) and adjust the value for socket to fit your needs.
Alternativly you could just link /tmp/mysql.sock to /Applications/AMPPS/mysql/tmp/mysql.sock:
ln -s /Applications/AMPPS/mysql/tmp/mysql.sock /tmp/mysql.sock

I think wrong my.ini is getting loaded. By default it looks at /etc/my.ini so if it is present just rename or delete it. Restart your MySQL Server.

Related

Trying to connect Django to MySQL. Connection error when migrating (Xampp, Linux)

I've been working for several weeks on Django. So far I have made 2 small projects. Now I tried to connect Django to the MySQL database.
First, I started a new project.
Then made a new app and updated the app in settings.py.
After that I changed the database in the same file settings.py with my settings.
Made a small model (in the file models.py).
Installed all the necessary apps, (pip install) mysqlclient etc,
Made the first migration successfully with
python manage.py makemigrations
So far so good!
Next, I tried to make the second migration (python manage.py migrate), as in the instructions.
Then the error message appears:
'error 111, can't connect to database'
Made some changes in the settings file, including SQL mode strict_trans_tables.
Now it shows me the error:
Can't connect to local MySQL server through socket ... Missing file .../mysql.sock.
I looked up the directory and there was no such file.
In the file my.cnf the filename and path is different!!! And exists! (It is using this existing file in my.cnf, but after executing the commang migrate it is searching for this file in a place, where it does not exist!)
To clarify things a little bit, the exact message of the error:
django.db.utils.OperationalError: (2002, "Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)")
There is no such file in the current directory (as I have checked).
my.cnf file contains the following path and filename:
socket=/opt/lampp/var/mysql/mysql.sock
There is a file in the current directory. MySQL works, as well.
What is going on and what to do?
=================================
Since my issue has been resolved, I how start thinking about what could cause such issue in the first place!
Apparently Django didn't know the path to the my.cnf file, even if it exists, so it had to be implemented manually from the settings.py file with the short code I have written below.
Recently I have been working on a similar projects, involving usage of javascript and node.js as a mean to create a website with access to database from mysql of the Xampp. I am using Linux for both projects, thus it might be related to the specifications of the system. Again in the node.js project I had to implement manually the path to the socked file, as follows:
const connection = mysql.createConnection({
host: process.env.DB_HOST,
user: process.env.DB_USERNAME,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
port: process.env.DB_PORT,
// THE PATH TO THE SOCKET FILE:
socketPath: process.env.DB_SOCKET_PATH
});
And thus I stated wondering why this issue has happened in the first place?? As I said, I could only suppose it might be related to the Linux version I am using (Linux Mint 20.3), or the way it was installed. When you start working with computers it is much easier to know what caused the problem, rather than only the correct solution.
Thank you very much in advance!
You have reached an issue which suggests a MySQL problem. It is wise to try and connect to MySQL directly at this point, because if that's successful, then you know for sure that your issues are on the application settings' side and if it's unsuccessful, then you need to solve MySQL's connectivity first and only then switch to your app's settings.
Now, the error clearly suggests that MySQL's socket file cannot be found.
First, let's check whether MySQL is running at all. In Ubuntu Linux the command to do so is
sudo service mysql status
If you use something else as an OS, then of course you will need to operate slightly differently.
In Ubuntu, if the command above yields something like
Unit mysql.service could not be found.
, then the service does not exist and you need to install it.
Otherwise, if the service exists but is not running, then you will need to start it if you are able. If not, then you have some maintenance to do.
Finally, if the MySQL service is successfully running, try connecting to it via cli, like:
mysql -u <youruser> -p
The -p at the end signifies that a password will be used, so after running the command above you will need to type in the password (if everything goes well).
If you get the same socket file-related error (which presumably will happen), then you will need to find the location of the socket file (.sock) and update MySQL's cnf file (/etc/my.cnf in Linux) so that it will take into account the correct location of the socket file:
socket=/var/lib/mysql/mysql.sock
Finally the Django project was able to start using the MySQL database!!!
The changes in the file setting.py are the same as before, as follows:
'ENGINE': 'django.db.backends.mysql',
'NAME': 'django_db',
'USER': 'username',
'PASSWORD': 'password',
'HOST': 'localhost',
'PORT': '3306',
the only thing that had to be done in addition to this, was to include the path of file my.cnf manually:
'OPTIONS': {
'read_default_file': '/opt/lampp/etc/my.cnf',
},
Obviously, this was the reason in the first place the path to the socket file to be mistaken.
But why in the first place this error occurs, I don't know. Perhaps somewhere after the installation some other path was given, but I don't know where is this path, pointing to other directory of the socket file separately, or to other path for the my.cnf file.
With the PostgreSQL database the connection was made so simply, it required only the 1st changes in the settings.py file.
After that, additional migrations were made without any disturbance, as well as the superuser account.
The source website from which I was able to use as a cheat sheet for the complete process:
https://www.digitalocean.com/community/tutorials/how-to-create-a-django-app-and-connect-it-to-a-database

Wamp- command line "mysqld" doesn't work

When I write the command line C:\wamp\bin\mysql\mysql5.6.17\bin>mysqld
nothing happens. It just shows the path again C:\wamp\bin\mysql\mysql5.6.17\bin>
and when I write this:
C:\wamp\bin\mysql\mysql5.6.17\bin>mysql
it shows an error: ERROR 2003 (HY000): Can't connect to mysql server on 'localhost' (10061)
I tried a lot to find an answer to this, but I couldn't find anything useful. Why isn't the server working?
I installed the same program for my friend, too and it works fine for her.
The error ERROR 2003 (HY000): Can't connect to mysql server on 'localhost' (10061) means that the MySQL server daemon is not running and so the client mysql can not connect. That makes sense, because mysqld doesn't start.
There are plenty of reasons, often its simply a misconfiguration or port collision (3306 already used?).
The first step is to make sure that the daemon is working without a configuration file. Please try to start the daemon by using mysqld --no-defaults.
no-defaults means that mysql will not read any option files, before starting.
If it starts up, you know that the configuration file(s) are to blame.
Then search in your my.ini for "wrong" or "old and deprecated" entries and fix them, to allow a normal startup. Its also helpful to enable logging.
If you are in doubt, go with one of the default configuration files: https://dev.mysql.com/doc/refman/5.6/en/server-default-configuration-file.html
You can check the ports with a tool like CurrPorts:
http://www.nirsoft.net/utils/cports.zip

No memsql.sock file in /var/lib/memsql

The error mentioned here: http://docs.memsql.com/latest/tshoot/2002/ I was getting using Ubuntu 14.04.2. The MySQL client and the latest MemSQL advised to point the 'my.cnf' config to the 'memsql.sock' file located in '/var/lib/memsql', however there is no such file.
After much searching and troubleshooting, there are similar errors but without the proper file they're irrelevant in as much that without the 'memsql.sock' file there can be no connection. I'm considering using MemSQL in production soon.
There are multiple reasons why you might observe this behavior.
First of all, note that the documentation suggests that the file is under /var/lib/memsql/data, while based on your question you might be looking for it under /var/lib/memsql.
It could also be that your MemSQL is not installed in /var/lib/memsql, but in some other place. In this case the memsql.sock file will be in the data subdirectory of the directory where your MemSQL is installed.
Finally, it could be that your memsql.cnf is missing the socket argument. You need to configure both the client and the server to use the same socket. The documentation page you pointed to suggests to change /etc/mysql/my.cnf -- that's the client config. Your server config is in the memsql.cnf file, which is most likely in /var/lib/memsql/memsql.cnf. Make sure it has the following line:
socket = memsql.sock
Then as you restart MemSQL, it will create the memsql.sock file.
If none of this works, you can also just connect to 127.0.0.1 instead of localhost, in which case the memsql.sock file will not be used.
If you use the mysql client without -h 127.0.0.1 it will try to connect to the socket, which by default is set to /var/run/mysqld/mysqld.sock. This is typically set in /etc/mysqld/my.cnf or wherever your my.conf file is. If you change your $HOME/.my.cnf file to have
[client]
port = 3306
socket = /var/lib/memsql/data/memsql.sock
this should work. Note, this is a behavior of the mysql client not MemSQL.

How do I get SQL database into R from local host?

I just created my first SQL database using MAMP. (It is simple- just a list of pets.) I would like to load it into R. Here is what I wrote:
install.packages("dbConnect")
library(dbConnect)
mypets=dbConnect(MySQL(),user="root",
host="localhost1234/DatabaseGrace")
This error is returned:
Error in mysqlNewConnection(drv, ...) :
RS-DBI driver: (Failed to connect to database: Error: Unknown MySQL server host 'localhost1234/DatabaseGrace' (2))
Any idea what this means or how I can solve it?
Thank you #duffymo and #Lorenz.
Summary:
The database is stored on my computer, so host did need to be localhost as you suggested.
Here is what ended up working.
install.packages("RMySQL")
install.packages("dbConnect")
library(dbConnect)
dbGrace=dbConnect(MySQL(),user="root",
host="localhost",
dbname="DatabaseGrace",
password="root",
unix.sock="/Applications/MAMP/tmp/mysql/mysql.sock")
Thanks, all!
I think the host name should be "localhost", without the 1234.
If you intended that to mean the port, I'd check to make sure that MySQL is indeed listening on that port. The default value is 3306; the admin would have to make a conscious decision to change that.
The usual notation is "localhost:3306" in Java. If you must include the port number, try separating it from the host name with a colon.
Can you connect to MySQL using the admin shell?
Have you GRANTed permission to localhost to connect to database named DatabaseGrace? If not, MySQL won't allow you to connect.
http://dev.mysql.com/doc/refman/5.1/en/grant.html
localhost suggests to me that the database is running on your local machine. Is that true? If not, localhost isn't the right host name. It might also mean that you have a firewall between your machine and the database which prevents you from connecting.
I get a lot of mileage out of cutting & pasting any errors I get into Google to see if anyone else has ever experienced my problem. Here's the first hit.
The host is the computer name. So put simply localhost.
Select the correct database in a second step.
this works for me with XAMPP on MAC:
library(RMySQL)
con=dbConnect(MySQL(),
host='localhost',
unix.sock='/Applications/XAMPP/xamppfiles/var/mysql/mysql.sock',
user='root',
password='...'
)
df=dbGetQuery(con,'SHOW DATABASES')
print(df)

phpmyadmin (wamp) not working anymore

I've played a little with php and data bases a few weeks ago and it worked perfectly. Now, when I tested the same thing again, I realized that it is not runnable anymore. More exactly, when I have to access the data base, I get the following type of notice:
Warning: mysqli_connect() [function.mysqli-connect]: [2002] A connection attempt failed because the connected party did not (trying to connect via tcp://localhost:3306) in E:\wamp\www\test\login.php on line 18
When I try to open http://localhost/phpmyadmin/ I get a blank screen
Please give me the possible explanations
You probably have another application using port 3306, another MySQL running perhaps?
The Possible explanation is
i suspect you have installed and uninstalled wamp? in that case you need to check you driver/etc/.hosts file under system32 ("Google it for exact path") :) and you have to make sure there exist only one value for
127.0.0.1 localhost
As it said above you need to check your running port has already used by another instance by checking
netstat -a and see if 3306 is Listening
Check on windows services that MySQL ,Apache are running or able to run automatically or manually from the service
suggest uninstall properly ==> delete all inside the wamp folder except www --takecare