app can't connect to local MySQL - osx - mysql

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

Related

How to create new server in mysql on windows?

I installed mysql 8.0. During the process of install, it will guide you to create a server that is registered as window service. For example, by default it will put server related file on the (base)path "C:\ProgramData\MySQL\MySQL Server 8.0" using default port 3306. What if I want to create another server?What should I do? Maybe using mysqld --initialize?
Well make sure you have installed a running and properly configured MySQL Server and Instance. If so, copy the data directory of your running MySQL Instance (Server) in a second folder.
Existing Folder: C:\ProgramData\MySQL\MySQL Server 5.7
New Folder (for example): C:\ProgramData\MySQL\MySQL second Server 5.7
NEXT: Create 2 Option-Files (a ka .txt files, you save as my-opts[Optummber.cnf)]. You may create one file named C:\ProgramData\MySQL\my-opts1.cnf that looks like this:
File 1 Content:
[mysqld]
datadir = C:\ProgramData\MySQL\MySQL Server 5.7\mydata1
port = 3307
Create a second file named C:\ProgramData\MySQL\my-opts2.cnf that looks like this:
[mysqld]
datadir = C:\ProgramData\MySQL\MySQL second Server 5.7\mydata2
port = 3308
NEXT: Use the --defaults-file option to start each server, runnig on its own option file:
C:\> C:\ProgramData\MySQL\bin\mysqld --defaults-file=C:\ProgramData\MySQL\my-opts1.cnf
C:\> C:\ProgramData\MySQL\bin\mysqld --defaults-file=C:\ProgramData\MySQL\my-opts2.cnf
Note: Each will start visibly, with a new prompt until the server exits later), so you will need to issue those two commands in separate console windows.
To shut down the servers, connect to each using the appropriate port number:
C:\> C:\ProgramData\MySQL\bin\mysqladmin --port=3307 --host=127.0.0.1 --user=root --password shutdown
C:\> C:\ProgramData\MySQL\bin\mysqladmin --port=3308 --host=127.0.0.1 --user=root --password shutdown
Servers configured as just described permit clients to connect over TCP/IP. - If you want to do so, please follow the guide on: https://dev.mysql.com/doc/refman/8.0/en/multiple-windows-command-line-servers.html
Note: I wrote the commands in here based on https://dev.mysql.com/doc/refman/8.0/en/multiple-windows-command-line-servers.html. Please modify them according to your file System and MySQL Configuration. I don't take any warranty for them. If the used filepaths seem to complicated for you, please change them to the eased up versions in the linked description. Setting up mySQL Instances might be a challenging process, just do so, if you can handle it.

Changing the default mysql socket path

I'm setting up a new Ubuntu computer, namely mysql. I've various rails app that use mysql on the socket /tmp/mysql.sock, and as they're group projects I can't change their db config options.
"mysqladmin -u root -p variables | grep socket" tells me my socket is /var/run/mysqld/mysqld.sock. I also have a mysql directory /etc/mysql.
I've read that I'm supposed to add the socket path to the my.cnf file in the etc/mysql directory, however it is locked for editing, and somehow reverts ownership immediately if I use chown to try and change it.
Please advise me how I can change where mysql sets its default socket path.
I am also trying to get to the bottom of this, but have yet to find the definitive answer. It looks like the socket path of mysql is different between a mac and a ubuntu machine.
I initially created my app on my mac, where the socket path is:
/tmp/mysql.sock. When I tried to run rake db:create on my ubuntu machine it kept complaining that the socket does not exist. I had a look at what it is called on my ubuntu machine, which was /var/run/mysqld/mysqld.sock. When I switch it out in database.yml it seems to work, but I guess I will have to keep both options in my file and uncomment based on which environment I am working in.
Hope that helps!

How to change the default port of mysql from 3306 to 3360

I want to change the default port number of MySQL server presently it is 3306. I want to change it to 3360.
I have tried:
-- port=3360
But things are not working for me. Please provide query to change port not any configuration. I am using Windows 8 64 bit.
You need to edit your my.cnf file and make sure you have the port set as in the following line:
port = 3360
Then restart your MySQL service and you should be good to go. There is no query you can run to make this change because the port is not a dynamic variable (q.v. here for MySQL documentation showing a table of all system variables).
If you're on Windows, you may find the config file my.ini it in this directory
C:\ProgramData\MySQL\MySQL Server 5.7\
You open this file in a text editor and look for this section:
# The TCP/IP Port the MySQL Server will listen on
port=3306
Then you change the number of the port, save the file.
Find the service MYSQL57 under Task Manager > Services and restart it.
On newer (for example 8.0.0) the simplest solution is (good choice for a scripted start-up for example):
mysqld --port=23306
When server first starts the my.ini may not be created where everyone has stated. I was able to find mine in C:\Documents and Settings\All Users\Application Data\MySQL\MySQL Server 5.6
This location has the defaults for every setting.
# CLIENT SECTION
# ----------------------------------------------------------------------
#
# The following options will be read by MySQL client applications.
# Note that only client applications shipped by MySQL are guaranteed
# to read this section. If you want your own MySQL client program to
# honor these values, you need to specify it as an option during the
# MySQL client library initialization.
#
[client]
# pipe
# socket=0.0
port=4306 !!!!!!!!!!!!!!!!!!!Change this!!!!!!!!!!!!!!!!!
[mysql]
no-beep
default-character-set=utf8
Go to installed mysql path and find bin folder,open my.ini and search 3306 after that change 3306 to 3360
Actually, you can just run the service using /mysqld --PORT 1234, it would force mysql to run on the specified port without change the cnf/ini file.
I just cought a case that cnf didn't work. It was weired... so I just use the cmd line as the shortcut and it works!
try changing the connection port to 8012
open xampp as administrator
Mysql config => my.ini change the port from 3306 to 8012
close and run it again
I hope it will work.
If you are using windows and installed the database as a service, which is the default, you should find your configuration file by opening your services management console.
For instance: win + r and then type services.msc
Look for a service called MySQL or MariaDB.
On the general tab of the properties of this service you can find a path to your mysqld.exe file and the arguments to start the exe. The --defaults-file argument should point to your configuration file.
Edit your configuration file and restart the MySQL service.
In Windows 8.1 x64 bit os, Currently I am using MySQL version :
Server version: 5.7.11-log MySQL Community Server (GPL)
For changing your MySQL port number, Go to installation directory, my installation directory is :
C:\Program Files\MySQL\MySQL Server 5.7
open the my-default.ini Configuration Setting file in any text editor.
search the line in the configuration file.
# port = .....
replace it with :
port=<my_new_port_number>
like my self changed to :
port=15800
To apply the changes don't forget to immediate either restart the MySQL Server or your OS.
Hope this would help many one.
Change my.cnf file and add this line or change it port=3360
at my fedora 34
sudo vi /etc/my.cnf
add This line ==> port=3360
The best way to do this is take backup of required database and reconfigure the server.
Creating A Backup
The mysqldump command is used to create textfile “dumps” of databases managed by MySQL. These dumps are just files with all the SQL commands needed to recreate the database from scratch. The process is quick and easy.
If you want to back up a single database, you merely create the dump and send the output into a file, like so:
mysqldump database_name > database_name.sql
Multiple databases can be backed up at the same time:
mysqldump --databases database_one database_two > two_databases.sql
In the code above, database_one is the name of the first database to be backed up, and database_two is the name of the second.
It is also simple to back up all of the databases on a server:
mysqldump --all-databases > all_databases.sql
After taking the backup, remove mysql and reinstall it. After reinstalling with the desired port number.
Restoring a Backup
Since the dump files are just SQL commands, you can restore the database backup by telling mysql to run the commands in it and put the data into the proper database.
mysql database_name < database_name.sql
In the code above, database_name is the name of the database you want to restore, and database_name.sql is the name of the backup file to be restored..
If you are trying to restore a single database from dump of all the databases, you have to let mysql know like this:
mysql --one-database database_name < all_databases.sql

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