Last updated 12 July 2016. Created on 17 January 2011.
Edited by morbidick, azovsky, kostajh, greggles. Log in to edit this page.
Having the Anonymous user deleted in your site breaks Drupal in unexpected ways, including a message like:
"Could not login with user ID #0." error from Drush
"warning: Invalid argument supplied for foreach() in /includes/form.inc on line 1181." and various other strange errors from Drupal core
Fixing cases where you accidentally deleted the user
There are many ways to accidentally delete the uid 0 (Anonymous) user: a mistaken query that forgot to use "and uid != 0" or a Views Bulk Operations view that was a little too aggressive.
The technique to fix it depends on a few factors. Below are different techniques that work on Drupal 7 or Drupal 6.
Drupal 8
For Drupal 8, you can run the following query:
INSERT INTO users (uid, langcode, uuid) VALUES (0, "en", "");
e.g. via Drush:
drush -u 1 sql-query "INSERT INTO users (uid, langcode, uuid) VALUES (0, "en", "");"
This will insert a new user with uid 0.
Drupal 7
For Drupal 7, you can run two queries:
INSERT INTO users (name, pass, mail, theme, signature, language, init, timezone) VALUES ('', '', '', '', '', '', '', '');
UPDATE users SET uid = 0 WHERE name = '';
e.g. via Drush:
drush -u 1 sql-query "INSERT INTO users (name, pass, mail, theme, signature, language, init, timezone) VALUES ('', '', '', '', '', '', '', '')"
drush -u 1 sql-query "UPDATE users SET uid = 0 WHERE name = ''"
This will insert a new row getting a random UID and then the update query fixes to get the right uid for the user.
For Drupal 6 using a slightly different technique
You can also insert directly with a specific value for the auto-incrementing UID if you tell your database to use a different SQL_MODE.
This pair of statements will bring the user back on Drupal 6:
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
INSERT INTO `users` (`uid`, `name`, `pass`, `mail`, `mode`, `sort`, `threshold`, `theme`, `signature`, `created`, `access`, `login`, `status`, `timezone`, `language`, `picture`, `init`, `data`)
VALUES ('0', '', '', '', 0, 0, 0, '', '', 0, 0, 0, 0, NULL, '', '', '', NULL);
Check again: select name, uid from users where name = '';
Output should be like this:
-+------+-----+
-| name | uid |
-+------+-----+
-| | 0 |
-+------+-----+
-1 row in set (0.00 sec)
Fixing cases where the import changed the uid values
Certain database dump programs, such as older versions of phpMyAdmin, do not respect MySQL's SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO"; directive. The result of this is when importing a faulty database dump, your anonymous user record gets imported as (highest user ID) +1 instead of user ID 0.
Should this happen to you, you can fix your database by issuing the following query:
UPDATE users SET uid = 0 WHERE name = '';
To check if this fix applies to you run:
select name, uid
from users
where name = '';
Output should be something like this:
-+------+-----+
-| name | uid |
-+------+-----+
-| | 0 |
-+------+-----+
-1 row in set (0.00 sec)
Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.
Comments
Rebuilding search-index not working without user 0
Thanks for this comment!
It just solved a problem I was trying to fix for the last two days.
I had this issue with a Drupal 6 installation that the search index wasn't properly rebuilt by the usual cron job. After researching a lot and not finding anything helpful, I installed Drush, and Drush told me that it couldn't login as user #0. So I followed the directions above, so that I could use Drush with user #0. After I did that, Drush AND the search indexing worked perfectly fine.
So for everyone having similar problems: the Drupal search index rebuilding engine seems to rely on the Anonymous user as well.
Drush issue
If you came to this issue because Drush sent you here:
Drush spits out:
Could not login with user ID #0. This is typically caused by importing a MySQL database dump [error]
from a faulty tool which re-numbered the anonymous user ID in the users table. See
http://drupal.org/node/1029506 for help recovering from this situation.
This was related to a site we built where we do not allow anonymous access. So the homepage displays access denied and the login screen.
Simple solution is to run drush as admin:
Eg clear cache:
drush -u 1 cc all
re: Drush issue
Running drush as user 1 worked wonderfully for me.
drush -u 1 cc all
Does anyone know if there are any downsides/dangers to running drush as admin user? If not, is there a way to tell drush to ALWAYS run as admin?
XDebug + PHPStorm
This error suddenly started appearing while I was trying to debug migrations from the command line using XDebug + PHPStorm. The problem was that I had set a bad conditional breakpoint on a line in /includes/entity.inc -- I had used the assignment operator where I should have used the equality operator (ie. $ids=NULL instead of $ids==NULL). Big facepalm. Be careful when using debuggers!
Well, as now Drupal 7 has
Well, as now Drupal 7 has INNER JOIN to user_revision table, we have to keep connection between users and user_revision tables on vid, otherwise user_load won't return any user.
Only if you are using the user revision module
Only if you are using the user revision module.
This was exactly my issue.
This was exactly my issue. The VID for user 0 did not mach the one in the user table.
This might reflect a bug in the user revision module.
Thanks for this.
Where should I insert snippet?
I know very little PHP - just enough to be dangerous. I'm specifically trying to get Ubercart working, but after each completed transaction (membership registration), User 0 is re-numbered. Yes, I can go run the query in PHPMyAdmin manually, but in what file should I insert the snippet listed above:
insert into users (name, pass, mail, theme, signature, language, init, timezone) values ('', '', '', '', '', '', '', '');
update users set uid = 0 where name = '';
to restore User 0 after each new transaction? I'm using Drupal 7.
Many thanks for all the help I've received from these forums!
-wiz
Update D 6.32
I just updated to Drupal 6.32 and this seems to have broke Drush now. I'm getting this error now.
You have uid0 and have this
You have uid0 and have this err?
(MYSQL related)
1. export your db_name.
2. drop your db_name.
3. create db_name with utf8 charset.CREATE DATABASE `mydb` CHARACTER SET utf8 COLLATE utf8_general_ci;
4. import your db.
You have uid0 and have this
Thank you, svetlio. I just saw this response from you and will try it. But before I do (since I just crashed another site of mine because of a different problem relating to dropping the database) let me just confirm that you are basically just telling me to delete and then recreate the database (after backing it up) using the utf8 charset. Correct? Sorry, I just want to do it right the first time.
Thanks,
-wiz
That's right
That's right
One note from my experience:
One of my installation this was good effect, for another not. So this is not the 100% for all situations solution. But sometime work ;)
Login as user 1
https://www.drupal.org/node/1029506#comment-7314538
Thnx, this comment saved my day!
Drupal backend developer since 2011.
Open Source Webdevelopment Coding Blog
Missing uid gave me serious error
I didnt realize about it but while i was using Quiz module, i was not able to proceed to next question after i click next. I did mysql import on my local and while i went to do drush cc, it gave me message of missing uid 0 and that's how i realised the issue with it.
remove some fields
In my Drupal 7 the SQL command did not worked, so I removed fields (mode`, `sort`, `threshold`) and their values. and the query worked perfectly.
Why its happens?
Most value question: Why its happens?
Why UID 0 is removed from the database once a month?
Drupal Core files may be hacked
This is what happened with my drupal 8. Making some developing I've accidentally commented out a line of drupal core code and after refreshing a page everything was gone away. Nothing on the page, even bartik theme had disappeared, just black footer background and this message: You are not authorized to access this page..
I've tryed to go to <DrupalRoot>/user but mod_rewrite changed it for <DrupalRoot>/user/1 which is expected because I was logged in at the moment as the user 1. Then I've tryed this <DrupalRoot>/user/login, that gave me a bare html form to insert name and password but again, no avail.
Sorry, unrecognized username or password. Have you forgotten your password?
Go further, I've tryed drush cc all. This gave "Could not login with user ID #0."which lead me on this thread. Checked database user tables, everything is OK. Tryed reinstall site, deleting settings.php at first. Nothing helped — "Drupal site is already installed, go to main page".
What is saved me is git reset HEAD. After resetting working directory my site again worked perfectly. The internal reason for this kind of crashes is that Drupal always has at any its run time at least two users. One is you, as the Drupal site user, may be authenticated or anonymous, and the other user with uid == 1, which is the Drupal itself! It works for you on the backgroung, executing scripts and serving pages. You may ask but how about physical user with uid == 1, who is s/he? To understand this you need to recall about Linux superuser, which works on the background even if it is not created(!), and you as administrator, installing for example software on the system on behalf of a superuser using sudo <command>. That is, when we logged in the User One, we are actually an ordinary user having the same permissions as the great and mighty Drupal :)
Thank you so much ! BUT...
Thank you so much, I have an anonymous user but this anonymous user does not have a login record, so it is still not recognized by drupal, do you know what I can do ?
I compared with other users, even on other drupal sites, and the only difference is the LOGIN column in the users table that is set to zero only on 1 websites having issues with anonymous user.
Any idea about how I could fill this login column ?
Thanks again.
Anonymous users aren't logged in
By definition, an anonymous user is a not logged in user. The anonymous user will never be logged in so it is normal that the login timestamp be zero for the anonymous user (uid = 0).
Also, any user having a login value of zero will not break anything, so if you are having problems that isn't the cause.
Sorry but this is wrong
Sorry but this is wrong, all my websites anonymous users have a login value that is not 0
Only 1 website that has problem and anonymous user with 0 in login column
I just checked a test site
I just checked a test site (Drupal 7) and for user 0, most columns/fields are 0 or null, including login being 0.
zero is ok
As you can see from the Drupal 6 example above for adding the anonymous user to the database, it is ok to have zero for the login column, even in D6.
I suspect that whatever your problem is is not related to the anonymous user login value being zero.
Also, extended conversations relating to bug troubleshooting are not really on topic for comments on documentation.
I would recommend posting a question asking for help fixing whatever your problem is.
Include as much detail as possible about what the symptoms of the problem are. So far you have said you have a problem but not said what it is.
I recommend posting it either to http://drupal.stackexchange.com/ or https://www.drupal.org/forum/22
Drupal 8
drush -u 1 sql-query "INSERT INTO users (uid, langcode, uuid) VALUES (0, "en", "");" this is not working the below is working
drush -u 1 sql-query "INSERT INTO `users_field_data` (`uid`, `langcode`, `preferred_langcode`, `preferred_admin_langcode`, `name`, `pass`, `mail`, `timezone`, `status`, `created`, `changed`, `access`, `login`, `init`, `default_langcode`) VALUES ('0', 'en', 'en', NULL, '', NULL, NULL, 'UTC', '0', '', '', '0', '0', NULL, '1');"
来自 https://www.drupal.org/node/1029506
“无法使用用户ID#0登录”
在D7上,我刚刚从D6升级,我尝试了每次drush命令时“无法使用用户ID#0登录”。
只有我做过的古怪的事情是:
- 将免费更新设置为true
- 将数据库移至新下载的7并编辑settings.php而不是移动文件
评论
怀疑它是VBO - 这是一个全新的D6我安装测试图像模块升级,当然不会去用户桌附近!
来自 https://www.drupal.org/project/drush/issues/1028700
Could not login with user ID
Trying out Drush, getting the error above on certain copies of sites. Googled but could not find reference to this error. Hoping someone might be able to give me some insights here. Probably something simple that I don't yet understand about the utility. Thanks!
Learn more
Comments
Got the same error message as well! I've blocked anonymous user access to my site. Using the command drush -u 1 status works for me. The -u option specifies a user id to log in with.
This tip also worked for me on a site where I was receiving the same message. I am cool if it is by design, but am curious as to what permissions trigger Drush to not be able to login as the anonymous user. Most of my sites are multisites so I am also using --uri=http://www.example.com in my Drush command strings.
I'm also getting this error message when I run drush to install modules using drush.php dl admin_menu. The download seems to work, despite the error message. When I add the -u 1 option, the "Could not login" error message disappears, and instead I get "Notice: Trying to get property of non-object in /mysitepath/includes/common.inc on line 1797."
I guess this isn't stopping me from getting work done, but it's confusing, and when I get an error message like this, I'd like to know what it means.
I'm trying to work with a profile from themesnap (www.themesnap.com/premium-drupal-themes/magazine-plus.html - it's a darn shame the CSS is not GPL).
Now I'm trying to use Drush on a site I've set up with this theme, but I'm getting:
$ drush --verbose=2 info
Initialized Drupal 6.14 root directory at /var/www/example.org/htdocs/ [notice]
Initialized Drupal site default at sites/default [notice]
Could not login with user ID #<em>0</em>. [error]
Command info needs a higher bootstrap level to run - you will need invoke drush from a more functional Drupal environment to run this command. [error]
The command 'drush.php info' could not be executed. [error]
$ drush -u 1 --verbose=2 info
Initialized Drupal 6.14 root directory at /var/www/example.org/htdocs [notice]
Initialized Drupal site default at sites/default [notice]
Including version specific file : /opt/drush/commands/pm/update_info/drupal_6.inc [notice]
No information available. [error]
An error occurred at function : drush_pm_info [error]
Command dispatch complete
And I'm curious about ways that would allow me to use Drush on this site.
drush -u 1 status is working now, just like most other drush stuff, drush -u 1 infois not.
It could be nice to have the user ID in the configuration somewhere.
drush -u 1 status does get me the status, but I also get the following error at the end:An error occurred at function : drush_core_status
Just a note for anyone else who comes across this based on the anon user error, i.e. Could not login with user ID #0.
It means exactly what it says so check the user table and ensure there is a user with uid 0.
In my case there was no uid 0 due to a restored faulty backup (where the first entry in the user table, i.e. the anon user, was allocated a uid which was not 0).
Thanks to #8 I did export import of the missing row and voala:
(The SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO"; ensures that the uid will be = 0 and not the last_uid + 1).
Correct the PREFIX before executing.
This stopped the drush errors.
-- phpMyAdmin SQL Dump
-- version 3.2.5
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Jan 30, 2010 at 06:55 PM
-- Server version: 5.1.41
-- PHP Version: 5.2.11-2
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
--
-- Database: `dr_akh_cons_at`
--
-- --------------------------------------------------------
--
-- Dumping data for table `dr6_umed_users`
--
INSERT INTO `PREFIX_users` (`uid`, `name`, `pass`, `mail`, `mode`, `sort`, `threshold`, `theme`, `signature`, `created`, `access`, `login`, `status`, `timezone`, `language`, `picture`, `init`, `data`, `signature_format`) VALUES
(0, '', '', '', 0, 0, 0, '', '', 0, 0, 0, 0, NULL, '', '', '', NULL, 1);
Thanks for this. Oddly enough, I had this problem on a fresh install in which I'd never monkeyed with the database at all. For no obvious reason, there was no uid 0 -- the anonymous user was uid 3 (?). I just changed it in the database to 0 and the drush error is now gone.
I get this error because the site I work on uses a settings.php in /sites/example.comand has no settings.php in /sites/default. Using -l http://localhost/example.comdoes not help either. Only the -u 1 gets me through without errors.
This will happen if you reload a site from a dumpfile created by drush sql-dump; the uid of UserZero (the anonymous user) can get set to 1+number of users.
Assuming you're using drush you can do the following to fix it (YMMV):
echo "update users set uid=0 where name='' and pass='' and data is NULL" | `drush sql-connect`
NOTE: your mileage may vary, update the stuff in between the back-ticks if you need to specify something else (eg --uri=example.com) to get to the correct mysql connection string.
My 1+number of users occurred without the sql-dump on migrating the database. But the same concept in #12 worked for me. I just edited the UID of that entry directly to 0 and everything was normal again. You have to give drush props for the clue to the problem via the error message.
| Version: | All-Versions-2.0 | » All-versions-3.3 |
Adding
$options['u'] = '1';
to "drushrc.php" residing in drush's install dir solves all these weird problems.
In recent version drushrc.php is no longer available instead you need to change includes/environment.inc line 902
$drush_user = drush_set_context('DRUSH_USER', drush_get_option(array('u', 'user'), 0));
to
$drush_user = drush_set_context('DRUSH_USER', drush_get_option(array('u', 'user'), 1));
The shipped file is called "example.drushrc.php" and it *is* there in both versions 3.3 (v 1.8 2010/06/05) and 4.0-rc1 (v 1.13 2010/12/05). Why should it have been removed?
Thanks #8 for a clear explanation. I had this problem upon installing with fantastico. Used phpmyadmin to look at users table, which showed an entry right before user 1 with blank fields, but id of 3. Manually changed this to 0 and now it's working.
| Status: | Closed (works as designed) | » Needs review |
| File | Size |
|---|---|
| 1.04 KB |
This patch might help reduce duplicate support requests about this. I was stranded and had no idea what was causing this until I googled.
Feel free to mark it back to "by design," though. Not sure how much special casing you really want to put into Drush for a 6.x-era bug caused by faulty database dumping tools.
| Status: | Fixed | » Closed (fixed) |
Automatically closed -- issue fixed for 2 weeks with no activity.
| Category: | support | » task |
| Status: | Closed (fixed) | » Active |
This is a weak solution. Ask the user "Do you want to fix this?". Here's how: name and mail both are unique and so if there is a user whose name and mail are empty that's the anon user, run UPDATE uid = uid - uid WHERE name = '' AND mail = '' and hope for the best
| Version: | » All-versions-4.x-dev | |
| Issue summary: | View changes | |
Here is the insert SQL that you want for 7.x:
INSERT INTO `users` (`uid`, `name`, `pass`, `mail`, `theme`, `signature`, `signature_format`, `created`, `access`, `login`, `status`, `timezone`, `language`, `picture`, `init`, `data`) VALUES
(0, '', '', '', '', '', NULL, 0, 0, 0, 1, NULL, '', 0, '', NULL)



