MySQL startup error problem InnoDB: Unable to lock and ibdata1 error

  • 2021-12-09 10:21:28
  • OfStack

Error when MySQL starts in OS X environment:


016-03-03T00:02:30.483037Z 0 [ERROR] InnoDB: Unable to lock ./ibdata1 error: 35
2016-03-03T00:02:30.483100Z 0 [Note] InnoDB: Check that you do not already have another mysqld process using the same InnoDB data or log files.

The terminal repeatedly prints the above error log, and it seems from the error log that another mysqld process is occupying the./ibdata1 file, so use the ps command to see if there is an mysqld process running:


ps -ef |grep mysqld
74 7711 1 0 8:04 Morning  ?? 0:00.34 /usr/local/mysql/bin/mysqld --user=_mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --log-error=/usr/local/mysql/data/mysqld.local.err --pid-file=/usr/local/mysql/data/mysqld.local.pid

Found that a 7711 process was running, so forced kill to drop:


sudo kill -9 7711

ps query again:


ps -ef |grep mysqld
74 7759 1 0 8:10 Morning  ?? 0:00.29 /usr/local/mysql/bin/mysqld --user=_mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --log-error=/usr/local/mysql/data/mysqld.local.err --pid-file=/usr/local/mysql/data/mysqld.local.pid

The discovery is still there, but pid has changed from the original 7711 to the current 7759, so look at what files the mysqld process opened:


lsof -c mysqld

The process didn't open any files, which is damn it.

Mac OS X, lsof only shows your own processes unless running as root with sudo

So run again:


sudo lsof -c mysqld
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
mysqld 8655 _mysql cwd DIR 1,4 544 3090250 /usr/local/mysql/data
mysqld 8655 _mysql txt REG 1,4 31130736 3089789 /usr/local/mysql/bin/mysqld

It is found that there is a real mysqld process running, which also occupies these mysql files. After one Google solution, it is found that starting MySQL in OS X is completely incompatible with starting mode in Linux. The correct posture for starting/restarting MySQL in OS X is:


sudo launchctl unload -w /Library/LaunchDaemons/com.oracle.oss.mysql.mysqld.plist

At this point, let's see if there is still an mysqld process:


ps -ef |grep mysqld

Well, if you find that it is really gone, start MySQL again:


sudo launchctl load -w /Library/LaunchDaemons/com.oracle.oss.mysql.mysqld.plist

The problem has been solved at last, but it's not over yet. We must make clear the principle.

What is LAUNCHD?

launchd is a key process introduced by Mac OS X from 10.4, which is used to initialize the system environment. It is the first process started in OS environment after the kernel is successfully loaded. Traditional Linux uses /etc/rc.* or /etc/init to manage the services to be started, while in OS X, launchd is used to manage them. Configuring startup items in this way is very simple, and only one plist file is needed. The plist files in the/Library/LaunchDaemons directories are all processes that start immediately after the system starts. Use the launchctl command to load/unload the plist file. After loading the configuration file, the program starts, and after unloading the configuration file, the program closes.

After uninstalling the configuration file, try to start the mysql process directly with the mysqld command:


/usr/local/mysql/bin/mysqld
2016-03-03T01:35:50.359258Z 0 [ERROR] InnoDB: ./ib_logfile0 can't be opened in read-write mode.
2016-03-03T01:35:50.359283Z 0 [ERROR] InnoDB: Plugin initialization aborted with error Generic error
2016-03-03T01:35:50.670517Z 0 [ERROR] Plugin 'InnoDB' init function returned error.
2016-03-03T01:35:50.670555Z 0 [ERROR] Plugin 'InnoDB' registration as a STORAGE ENGINE failed.
2016-03-03T01:35:50.670568Z 0 [ERROR] Failed to initialize plugins.
2016-03-03T01:35:50.670574Z 0 [ERROR] Aborting

ib_logfile0 cannot be opened, guess it is a user rights file, and mysql cannot be started with the current system user. Then add sudo and start it with root:


ps -ef |grep mysqld
74 7711 1 0 8:04 Morning  ?? 0:00.34 /usr/local/mysql/bin/mysqld --user=_mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --log-error=/usr/local/mysql/data/mysqld.local.err --pid-file=/usr/local/mysql/data/mysqld.local.pid
0

Ask me to read the safety manual of MySQL, or start it in the way of launchd.


Related articles: