Create MySQL database in OS X Lion - mysql

I am trying to set up a dev environment on my Mac laptop, and I have run into some trouble when installing mysql. When I try to create a database, I get the following error:
ERROR 1006 (HY000): Can't create database 'SummerGypsy_development'
(errno: 2
From what I have gathered, this means there was some problem in creating the database on the disc.
Here is how I installed mysql:
First, using Homebrew, I executed:
brew install mysql
Once that finished, I tried running mysql_install_db, but got an error. To remedy this error, I ended up running
mysql_install_db --basedir=/usr/local/
I can run mysql, but creating a database does not work. I ran
mysqladmin variables | grep datadir
To find where mysql was trying to create the database. The output was: /usr/local/mysql/data
/usr/local/mysql and /usr/local/mysql/data directories do not actually exist, and I have a feeling that the datadir is pointing here because of the basedir I passed into mysql_install_db. As a quick fix, I tried creating both directories, and then running
chown -R mysql:mysql /usr/local/mysql
to give mysql permissions in this directory. This did not fix the problem, though.
Does anyone have any suggestions?

you need to change the permissions in the data.
chown -R mysql:mysql /usr/local/mysql/data

You might want to try installing from the .dmg file (installer, system preferences pane, and startup script) that is distributed directly from MySQL. There is a thread on Getting MySQL work on OSX 10.7 Lion that you might want to take a look at. These steps helped me get MySQL running on OSX Lion. Hope this helps.

I tried lot of the suggestions including the file permission reset with no luck. But i then restarted the mysql using the command "./mysqld_safe &" (excluding quotes) and it worked.

Related

How to reset mysql to factory settings?

So I dropped all the users in mysql.user and restarted the mysql database. Now I can't seem to get into the mysql again or how to reset it as if it was a clean install. I tried uninstalling it with brew uninstall mariadb and then reinstalling it with brew install mariadb, but that didn't fix it. The only resources I can find is about how to restore a user from inside mysql (which I am struggling to get into) or how to delete mysql completely by deleting files in certain directories (which don't exist in the first place).
There are two methods:
method 1:
1.Uninstall the mysql database, delete the data files in the /data directory, and then reinstall
Method 2:
1.Stop the mysql service (systemctl stop mysqld)
2.Delete the files in the /data directory (rm -fr /data/*)
3.Initialize mysql (mysqld --defaults-file=/mysql/my.cnf --initialize --user=mysql --basedir=/mysql/app/mysql --datadir=/mysql/data/3306/data/)
PS1: The path may be different from yours, you need to change it according to your own data directory
I did end up finding a solution to my problem. I didn't end up getting it from one source, but more cobbled a bunch of different stuff together until I figured out that this worked.
Stop the server: `brew services stop mariadb
Start it with this command: mysql --skip-grant-tables
Run this command to fix the table with no users: mysql_upgrade --force
Force kill mysql: ps -ef | grep mysql followed by kill -9 <pid>
Start up the server again: brew services start mariadb
I did first try mysql_install_db instead of mysql_upgrade --force as some site suggested, but that didn't work since mysql.user table still existed. It was just empty.

Mysql Server not start after Migration Assistant in MacOs

I Used Migration Assistant from ios to put my things in another mac.
But, after that, my MySQL Server does not run!
In System Preferences, my computer say:
the mysql server instance is not running
Whats happing?
I try this:
brew services start mysql#5.7
And receive:
Error: Formula `mysql#5.7` is not installed.
I do not know why, but my system automatically renamed the MySQL directory during the migration process.
I identified that the usr/local/mysql/ directory was now called usr/local/mysql-5.7.21-macos10.13-x86_64/
What I did that solved the problem, was to create a symbolic link
sudo ln -s mysql-5.7.21-macos10.13-x86_64 mysql
I rebooted my mac, and everything worked correctly.
you will probably need to identify the new directory name with the command
ls usr/local

MySQL - Can't connect to local MYSQL server...ERROR 2002?

Hi I've looked all over the internet to find an answer to this problem but nothing seems to work. I get the below problem when I try to log in to mysql on terminal in OSX El Capitan.
The server is running.
What should I do, I've spent 5 hours trying to figure this out before posting this. I cannot find the my.cnf file in /etc and I'm not sure what to do with it. Someone please help.
Edit:
I've downloaded and installed(just double clicking - no terminal) this:
When everything is installed and i go into /usr/local/mysql/bin/ and try mysql -u root -p I get the following error:
This is my my.cnf file in /etc:
When I do sudo /usr/local/mysql/bin/mysqld_safe I get this:
df -h returns this, could it be something to do with space?
If you feel more comfortable installing it through the GUI here is an instruction guide on how to do that.
http://dev.mysql.com/doc/refman/5.7/en/osx-installation-pkg.html
Make sure you install the MySQLStartupItem.pkg and MySQL.prefPane. After you have done that you can verify your installation by opening up Terminal and running:
cd /usr/local
hitting enter then typing:
ls
hit enter again and verify the existence of two directories, one will just be MySQL and the other will be MySQL-(version number you installed).
At this stage I would restart your computer and then on restart the mysql server will start up.
Once it's restarted fire up terminal window again and type this:
/usr/local/mysql/bin/mysql -u root -p
EDIT
Open your /etc/my.cnf file and edit these two configurations.
[mysqld]
socket=/var/mysql/mysql.sock
[client]
socket=/var/mysql/mysql.sock
Hope this helps.

Can't connect to local MySQL server through socket homebrew

I recently tried installing MySQL with homebrew (brew install mysql) and when I try to run it I get the following error:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
There is no /tmp/mysql.sock nor a /var/lib/mysql.sock.
I've searched and haven't found any mysql.sock file.
How can I fix this?
When you got the server running via
mysql.server start
you should see the socket in /tmp/mysql.sock. However, the system seems to expect it in /var/mysql/mysql.sock. To fix this, you have to create a symlink in /var/mysql:
sudo mkdir /var/mysql
sudo ln -s /tmp/mysql.sock /var/mysql/mysql.sock
This solved it for me. Now my phpMyAdmin works happily with localhost and 127.0.0.1.
Credit goes to Henry
Warning, this will wipe your databases, take a backup if you wish to keep them
I had some directories left from another mysql(8.0) installation, that were not removed.
I solved this by doing the following:
First uninstall mysql
brew uninstall mysql#5.6
Delete the folders/files that were not removed
rm -rf /usr/local/var/mysql
rm /usr/local/etc/my.cnf
Reinstall mysql and link it
brew install mysql#5.6
brew link --force mysql#5.6
Enable and start the service
brew services start mysql#5.6
Looks like your mysql server is not started. I usually run the stop command and then start it again:
mysqld stop
mysql.server start
Same error, and this works for me.
Try to connect using "127.0.0.1" instead "localhost".
If you are able to see "mysql stopped" when you run below command;
brew services list
and if you are able to start mysql with below command;
mysql server start
this means; mysql is able to start manually, but it doesn't start automatically when the operating system is started. Adding mysql to services will fix this problem. To do so, you can run below command;
brew services start mysql
After that, you may restart your operating system and try connecting to mysql to see if it started automatically. I did the same and stop receiving below error;
ERROR 2002 (HY000): Can't connect to local MySQL server through socket
'/tmp/mysql.sock' (2)
I hope this helps.
The file /tmp/mysql.sock is probably a Named-Pipe, since it's in a temporary folder. A named pipe is a Special-File that never gets permanently stored.
If we make two programs, and we want one program to send a message to another program, we could create a text file. We have one program write something in the text file and the other program read what our other program wrote. That's what a pipe is, except it doesn't write the file to our computer hard disk, IE doesn't permanently store the file (like we do when we create a file and save it.)
A Socket is the exact same as a Pipe. The difference is that Sockets are usually used over a network -- between computers. A Socket sends information to another computer, or receives information from another computer. Both Pipes and Sockets use a temporary file to share so that they can 'communicate'.
It's difficult to discern which one MySql is using in this case. Doesn't matter though.
The command mysql.server start should get the 'server' (program) running its infinite loop that will create that special-file and wait for changes (listen for writes).
After that, a common issue might be that the MySql program doesn't have permission to create a file on your machine, so you might have to give it root privileges
sudo mysql.server start
Since I spent quite some time trying to solve this and always came back to this page when looking for this error, I'll leave my solution here hoping that somebody saves the time I've lost. Although in my case I am using mariadb rather than MySql, you might still be able to adapt this solution to your needs.
My problem
is the same, but my setup is a bit different (mariadb instead of mysql):
Installed mariadb with homebrew
$ brew install mariadb
Started the daemon
$ brew services start mariadb
Tried to connect and got the above mentioned error
$ mysql -uroot
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
My solution
find out which my.cnf files are used by mysql (as suggested in this comment):
$ mysql --verbose --help | grep my.cnf
/usr/local/etc/my.cnf ~/.my.cnf
order of preference, my.cnf, $MYSQL_TCP_PORT,
check where the Unix socket file is running (almost as described here):
$ netstat -ln | grep mariadb
.... /usr/local/mariadb/data/mariadb.sock
(you might want to grep mysql instead of mariadb)
Add the socket file you found to ~/.my.cnf (create the file if necessary)(assuming ~/.my.cnf was listed when running the mysql --verbose ...-command from above):
[client]
socket = /usr/local/mariadb/data/mariadb.sock
Restart your mariadb:
$ brew services restart mariadb
After this I could run mysql and got:
$ mysql -uroot
ERROR 1698 (28000): Access denied for user 'root'#'localhost'
So I run the command with superuser privileges instead and after entering my password I got:
$ sudo mysql -uroot
MariaDB [(none)]>
Notes:
I'm not quite sure about the groups where you have to add the socket, first I had it [client-server] but then I figured [client] should be enough. So I changed it and it still works.
When running mariadb_config | grep socket I get:
--socket [/tmp/mysql.sock]
which is a bit confusing since it seems that /usr/local/mariadb/data/mariadb.sock is the actual place (at least on my machine)
I wonder where I can configure the /usr/local/mariadb/data/mariadb.sock to actually be /tmp/mysql.sockso I can use the default settings instead of having to edit my .my.cnf (but I'm too tired now to figure that out...)
At some point I also did things mentioned in other answers before coming up with this.
After installing macos mojave, had to wipe mysql folder under /usr/local/var/mysql and then reinstall via brew install mysql otherwise permission related things would come up all over the place.
In my case, the culprit was found in the logfiles:
$ tail /usr/local/var/mysql/<hostname>.lan.err
2019-09-19 7:32:21 0 [ERROR] InnoDB: redo log file './ib_logfile0' exists. Creating system tablespace with existing redo log files is not recommended. Please delete all redo log files before creating new system tablespace.
2019-09-19 7:32:21 0 [ERROR] InnoDB: Database creation was aborted with error Generic error. You may need to delete the ibdata1 file before trying to start up again.
So I renamed ib_logfile0 to get rid of the error (I had to do the same with ib_logfile1 afterwards).
mv /usr/local/var/mysql/ib_logfile0 /usr/local/var/mysql/ib_logfile0_bak
mv /usr/local/var/mysql/ib_logfile1 /usr/local/var/mysql/ib_logfile1_bak
brew services restart mariadb
I got the same error and this is what helped me:
$ln -sfv /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents
$launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
$mysql -uroot
mysql>
I faced the same problem on my mac and solved it, by following the following tutorials
https://mariadb.com/resources/blog/installing-mariadb-10116-mac-os-x-homebrew
But don't forget to kill or uninstall the old version before continuing.
Commands:
brew uninstall mariadb
xcode-select --install
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" - See more at: https://mariadb.com/resources/blog/installing-mariadb-10116-mac-os-x-homebrew#sthash.XQoxRoJp.dpuf
brew doctor
brew update
brew info mariadb
brew install mariadb
mysql_install_db
mysql.server start
Just to add to these answers, In my case I had no local mySQL server, it was running inside a docker container. So the socket file does not exist and will not be accessible for the "mysql" client.
The sock file gets created by mysqld and mysql uses this to communicate with it.
However if your mySql server is not running local, it does not require the sock file.
By specifying a host name/ip the sock file is not required e.g.
mysql --host=127.0.0.1 --port=3306 --user=xyz --password=xyz
If "mysqld" IS running, it's possible your data is corrupted. Try running this:
mysqld
Read through the wall of data, and check if mysqld is reporting that the database is corrupted. Corruption can present in many unintuitive ways:
mysql -uroot returns "ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)".
mysql.server start returns "ERROR! The server quit without updating PID".
Sequel Pro and MySQL Workbench responds that they can't connect to MySQL on localhost or 127.0.0.1.
To recover your data, open my.cnf and add the following line in the [mysqld] section:
innodb_force_recovery=1
Restart mysqld:
$ brew services restart mysql#5.6
Now you can connect to it, but it’s in limited read-only mode.
If you're using InnoDB, run this to export all your data:
$ mysqldump -u root -p --all-databases --add-drop-database --add-drop-table > data-recovery.sql
The file is created in your ~ dir. It may take some time.
Once finished, remove innodb_force_recovery=1 from my.cnf, then restart mysql in normal mode:
$ brew services restart mysql#5.6
Drop all the databases. I did this using Sequel Pro. This deletes all your original data. Make sure your data-recovery.sql looks good before doing this. Also consider backing up /usr/local/var/mysql to be extra careful.
Then restore the databases, tables, and data with this:
$ mysql -uroot < ~/data-recovery.sql
This can be a long import/restoration process. Once complete, you’re good to go!
Thanks go to https://severalnines.com/database-blog/my-mysql-database-corrupted-what-do-i-do-now for the recovery instructions. The link has further instructions on MyISAM recovery.
You'll need to run mysql_install_db - easiest way is if you're in the install directory:
$ cd /usr/local/Cellar/mysql/<version>/
$ mysql_install_db
Alternatively, you can feed mysql_install_db a basedir parameter like the following:
$ mysql_install_db --basedir="$(brew --prefix mysql)"
After a restart I could not connect with the local mariadb, a search also brought me to this page and I wanted to share my solution with you.
I noticed that the directory my.cnf.d in /usr/local/etc/ is missing.
This is a known bug with homebrew that is described and solved there. https://github.com/Homebrew/homebrew-core/issues/36801
fast way to fix: mkdir /usr/local/etc/my.cnf.d
When running mysql_secure_installation and entering the new password I got:
Error: Can't connect to local MySQL server through socket
'/tmp/mysql.sock' (2)
I noticed when trying the following from this answer:
netstat -ln | grep mysql
It didn't return anything, and I took that to mean that there wasn't a .sock file.
So, I added the following to my my.cnf file (either in /etc/my.cnf or in my case, /usr/local/etc/my.cnf).
Under:
[mysqld]
socket=/tmp/mysql.sock
Under:
[client]
socket=/tmp/mysql.sock
This was based on this post.
Then stop/start mysql again and retried mysql_secure_installation which finally let me enter my new root password and continue with other setup preferences.
This works for me:
brew upgrade mysql
If brew does not complete "postinstall"
I only have to use:
sudo chown -R $(whoami) /usr/local/*
then completed postinstall by:
brew postinstall mysql#5.7
next step just start mysql#5.7 service.
It worked for me on MacOS Monterey.
I also ran into this... it seemingly resulted from some leftover artifacts of multiple mysql installs on my dev machine. Every time I attempted to start or restart the mysql service it would crash. Ultimately, working through the err file helped me solve my issue.
Setup:
M1 Macbook Pro running OS 12 (Monterey)
Homebrew install of mysql#5.7
At various points during troubleshooting I tried uninstalling everything mysql in my Homebrew list, deleting /opt/homebrew/var/mysql, and reinstalling mysql#5.7 to no avail. I also tried restarting my machine.
I was able to validate what was happening when the service failed to start by reviewing the err (e.g., MyComputerName.local.err) log that it was spitting out into /opt/homebrew/var/mysql/ when I tried to start/restart the service via Homebrew. In the beginning, I was seeing things like this that pointed to issues with the config:
mysqld: Table 'mysql.plugin' doesn't exist
TIMESTAMPZ 0 [ERROR] Can't open the mysql.plugin table. Please run mysql_upgrade to create it.
TIMESTAMPZ 0 [ERROR] unknown variable 'mysqlx-bind-address=127.0.0.1'
TIMESTAMPZ 0 [ERROR] Aborting
I deleted my.cnf and my.cnf.default in /opt/homebrew/etc and attempted to start the service again. Then, the failure changed to this:
TIMESTAMPZ 0 [ERROR] Fatal error: Can't open and lock privilege tables: Table 'mysql.user' doesn't exist
TIMESTAMPZ 0 [ERROR] Fatal error: Failed to initialize ACL/grant/time zones structures or failed to remove temporary table files.
TIMESTAMPZ 0 [ERROR] Aborting
At this point I noticed several mysql lock files in /opt/homebrew/var/homebrew/locks/ (mysql.formula.lock, mysql.formula#5.7.lock, mysql.formula#8.0.lock); I deleted those files, as well as anything mysql in /opt/homebrew/var/homebrew/linked.
After that, I was able to start mysql without any issues.
just to complete this thread.
therefore MAMP (PRO) is used pretty often
the path here is
/Applications/MAMP/tmp/mysql/mysql.sock
I manually started mysql in the system preferences pane by initialising the database and then starting it. This solved my problem.
I had same problem. After trying all these methods without success I did the following:
tail -f the-mysql-or-maria-db-error-file.err
in another console:
brew services restart mariadb
I saw the following error:
"MAC HOMEBREW Crash recovery failed. Either correct the problem (if
it's, for example, out of memory error) and restart, or delete tc log
and start mysqld with"
So I changed the tc.log extesion to tc.log.txt and restart mariadb
brew services restart mariadb
and done!
[LINUX]
Though answer is expected to be for MacOS only but in Linux we may face the same Error.
I was facing the same issue in Linux. I ran this command:
sudo /etc/init.d/mysql.server start
and I was able to run the MySQL server
Ref. https://gist.github.com/vinodpandey/1a4b5b8228f9a000ca236820185fc3bc
I just ran into this problem, I did some tricks but for me it didn't work. If you used macOS, here is what I did. We start by uninstalling mysql and then reinstalling it.
brew uninstall mysql
brew install mysql
Note: you must have homebrew installed
This happened to me today 2023-02-23 after a homebrew update.
Homebrew had created a new my.cnf.default file, I backed up my old one and replaced it with this new default and restarted Mariadb and everything worked including the old root password.
For me, I had installed mariadb long time ago, then installed mysql#5.7.
When I executed mysql -uroot, I get the error:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
Reading the answers:
I uninstalled mariadb
Deleted the folder /usr/local/var/mysql
Ran the command mysqld --initialize
Then I was able to mysql -uroot -p

Can't find file: './ci/users.frm' (errno: 13)

I installed LAMP on Ubuntu 11.04 and copy project from Windows.
PHP directory (/ci/) to var/www/
and
MySQL project directory (/ci/) to var/lib/mysql/
Full text of error that i get:
A Database Error Occurred
Error Number: 1017
Can't find file: './ci/users.frm' (errno: 13)
SELECT COUNT(*) AS `numrows` FROM (`users`) WHERE `email` = 'admin#localsite.com'
I googled that its permission problem, but don't know what do next.
Log from /var/log/mysql/error.log:
110622 19:27:21 [ERROR] /usr/sbin/mysqld: Can't find file: './ci/users.frm' (errno: 13)
Permissions problem meaning the permissions on the file. MySQL probably can't read it. Just change the owner and group to mysql and it should work.
chown mysql:mysql /var/lib/mysql/ci/*
As well as the files being readable by the MySQL user, the directory containing the .MYI files needs to be read, write and executable by the MySQL user. On my system this was achieved by:
chown -R mysql:mysql /var/lib/mysql/dbname
chmod -R 660 /var/lib/mysql/dbname
chown mysql:mysql /var/lib/mysql/dbname
chmod 700 /var/lib/mysql/dbname
This is an old topic, but I didn't find anything that worked for me so for anyone running into the same problem, yet the above file permission suggestions still don't change the "Can't find file" errors, here's what worked for me and my particular issue.
I was doing a rescue from one CentOS server to another using a recovery image, which had a different OS than the original OS and the original filesystem was mounted on a temporary dir. While I had access to the original /var/lib/mysql files, I didn't have access to the mysql admin or dump utilities, which requires the server to be running anyway (it's not automatically included when doing a recovery from a read-only image). Backups were a week old and I wanted to see if I could get the most recent data possible.
Changing the standard file permissions on these still kept giving "Can't find file" for nearly all of the database tables, however I could see that the tables were there. Turns out it was related to SELinux context on the files I had moved over using rysnc. All of the rescued dirs and files looked like this:
$ ls -alZ
drwx------. mysql mysql unconfined_u:object_r:admin_home_t:s0 somedb_dev
drwx------. mysql mysql unconfined_u:object_r:admin_home_t:s0 somedb_local
drwx------. mysql mysql unconfined_u:object_r:admin_home_t:s0 somedb_production
drwx------. mysql mysql unconfined_u:object_r:admin_home_t:s0 somedb_staging
The -Z flag notes the security context of files and dirs. Notice the unconfined_u and admin_home_t context. These are different from what they should be:
drwx------. mysql mysql system_u:object_r:mysqld_db_t:s0 mysql
Changing these database files to the proper context solved the problem and gave proper access to mysqld using the chcon command:
$ chcon -R -u system_u -t mysqld_db_t somedb_*
This changed all my custom databases to the proper SELinux context and the files could now be recognized by mysqld. I recommend running the chcon commad while the database server is not active, just as a precaution.
Hope that helps someone running into the same problem I had! Of course, you can turn off SELinux temporarily to test if this is fact this issue, but I didn't want turning off SELinux as a permanent solution.
I followed this steps:
Stop the mysql service.
Modify the my.cnf line datadir to my custom location.
Deleted all the files ib_data* , ib_logfile* in our new custom location
Change the permissions of the entire folder with your sentence:
chown mysql:mysql -R /custom_location/mysql/*
Start again the mysql service.
It works!!
Thanks
This error also occurs if the table is not in the database; so if you changed permissions of the directory and are still running into issues check your database and make sure the table is there.
So let's say you got an error like the OP:
Can't find file: './ci/users.frm'
ci is the database name
users is the table name
So in this case if you changed permissions and still had this issue you would verify that the users table is in the ci database.
#Brent Baisley It does work in XAMPP for Linux, but the location is different.
I did upgrade the Kernel today to fix the new Linux “Dirty Cow” Vulnerability (CVE-2016-5195). After the reboot I got the 'frm' permission error too.
So, if you get the following error:
Can't find file: 'yourtablename.frm' (errno: 13 - Permission denied) SQL query :...
You can do:
chown mysql:mysql /opt/lampp/var/mysql/yourDBname/*.frm
This will resolve your issue.
If you'd like to check, if your permission to any of the files has been modified before you execute the permission change, do:
ls -l /opt/lampp/var/mysql/yourDBname/*.frm
Hope that helps someone.
If you have failed RENAME TABLE statement, it could leave MySQL metadata in bad state. The solution is to recreate schema or to recreate table.