Laravel入门遇MySQL错误:指定DEFINER用户'mysql.infoschema'@'localhost'不存在
Hey there, I’ve run into this exact definer error before with Homebrew-installed MySQL on older macOS versions, so let’s walk through how to fix it step by step:
Step 1: Stop the MySQL service first
First, we need to shut down MySQL to make changes safely:
brew services stop mysql
If that doesn’t work (e.g., older Homebrew setup), try:
mysql.server stop
Step 2: Start MySQL in safe mode (skip permission checks)
This lets us log in without needing valid credentials to fix the missing user:
mysqld_safe --skip-grant-tables &
The & runs it in the background so you can keep using the same terminal window.
Step 3: Log into MySQL without a password
Now connect to the MySQL server as root, no password required:
mysql -u root
Step 4: Create the missing mysql.infoschema user and set permissions
Run these SQL commands one by one inside the MySQL prompt:
-- Create the infoschema user (empty password is default for this system user) CREATE USER 'mysql.infoschema'@'localhost' IDENTIFIED BY ''; -- Grant necessary SELECT permissions (this user only needs to read schema info) GRANT SELECT ON *.* TO 'mysql.infoschema'@'localhost'; -- Refresh permissions to apply changes immediately FLUSH PRIVILEGES;
Step 5: Exit MySQL and stop the safe-mode process
Type exit; to leave the MySQL prompt, then kill the safe-mode MySQL process:
pkill mysqld
Step 6: Restart MySQL normally
Start the service back up using Homebrew:
brew services start mysql
Step 7: Test if the error is gone
Log into MySQL again (use your root password if you have one set):
mysql -u root -p
Then run the command that was failing before:
SHOW DATABASES;
This should now work without the definer error.
If the above steps don’t work (nuclear option: reinitialize MySQL)
Warning: This will delete all your existing databases. Back up your data first!
- Stop the service:
brew services stop mysql - Back up your MySQL data directory:
cp -r /usr/local/var/mysql /usr/local/var/mysql_backup - Find your installed MySQL version with:
Look for a line likebrew info mysqlusr/local/Cellar/mysql/8.0.36– that’s your--basedirpath. - Initialize a fresh MySQL instance:
mysqld --initialize --user=mysql --basedir=/usr/local/Cellar/mysql/[YOUR_VERSION] --datadir=/usr/local/var/mysql - Start the service:
brew services start mysql - Find the temporary root password in the error log:
cat /usr/local/var/mysql/$(hostname).err | grep temporary password - Log in with that temporary password, then set a new root password:
After this, you can restore your backed-up databases if needed.ALTER USER 'root'@'localhost' IDENTIFIED BY 'your-new-password';
Why this happens
This error usually pops up when MySQL is updated via Homebrew, and the automatic upgrade process fails to create or update the system-level mysql.infoschema user (which is critical for querying database schemas). Manually creating the user fixes the permission mismatch that’s causing the error.
内容的提问来源于stack exchange,提问作者jdubtd10

