一、查看用户ID

首先要确认要修改密码用户的ID,有两种方法:

  • api接口查询
  • gitlab数据库查询

1.1、方法一:通过api接口查询用户id

通过使用浏览器或者curl等命令直接访问gitlab接口地址:http://gitlab的url/api/v4/users?username=用户名

这里以Linux终端命令为例:

[root@GitLabServer ~]# curl http://192.168.0.2/api/v4/users?username=root
[{"id":1,"name":"Admin","username":"root","state":"active","avatar_url":"http://192.168.0.2/uploads/-/system/user/avatar/1/avatar.png","web_url":"http://192.168.0.2/root"}]
[root@GitLabServer ~]#

这里可以看到root的用户ID为1。

1.2、方法二:进入到gitlab数据库查询

执行以下命令:

# 在gitlab服务器上执行以下命令
# 连接gitlab数据库
[root@GitLabServer ~]# gitlab-rails dbconsole
psql (11.7)
Type "help" for help.

# 查看数据库列表
gitlabhq_production=> \l
                                             List of databases
        Name         |    Owner    | Encoding |   Collate   |    Ctype    |        Access privileges        
---------------------+-------------+----------+-------------+-------------+---------------------------------
 gitlabhq_production | gitlab      | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 postgres            | gitlab-psql | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 template0           | gitlab-psql | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/"gitlab-psql"               +
                     |             |          |             |             | "gitlab-psql"=CTc/"gitlab-psql"
 template1           | gitlab-psql | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/"gitlab-psql"               +
                     |             |          |             |             | "gitlab-psql"=CTc/"gitlab-psql"
(4 rows)

# 以gitlab用户连接数据库gitlabhq_production
gitlabhq_production=> \c gitlabhq_production
You are now connected to database "gitlabhq_production" as user "gitlab".
# 查询所有用户列表,可以看到root的id为1
gitlabhq_production=> select id,name,username from users;
 id |       name       |   username   
----+------------------+--------------
  1 | Admin            | root
(1 rows)
# 直接查询root用户的id,可以看到是1
gitlabhq_production=> select id from users where username = 'root';
 id 
----
  1
(1 row)
# 退出
gitlabhq_production-> \q

二、实例:修改root账号密码

如果gitlab忘记了root账号的密码,可以直接在gitlab服务器上直接修改数据库数据:

执行以下命令:

# 在gitlab服务器上执行以下命令
# 连接gitlab数据库production
[root@GitLabServer ~]# gitlab-rails console -e production
--------------------------------------------------------------------------------
 GitLab:       13.0.12 (12f657f4f85) FOSS
 GitLab Shell: 13.2.0
 PostgreSQL:   11.7
--------------------------------------------------------------------------------
Loading production environment (Rails 6.0.3)
# 这里的id就是上面查到的root的id值为1
irb(main):001:0> user = User.where(id: 1).first
=> #<User id:1 @root>
# 配置root的新密码
irb(main):002:0> user.password = 'aaaaaa@888!'
=> "aaaaaa@888!"
# 再次确认root的新密码
irb(main):003:0> user.password_confirmation = 'aaaaaa@888!'
=> "aaaaaa@888!"
# 保存
irb(main):004:0> user.save!
Enqueued ActionMailer::DeliveryJob (Job ID: 69bc35b3-6dc7-44ee-ba11-4035aad98406) to Sidekiq(mailers) with arguments: "DeviseMailer", "password_change", "deliver_now", #<GlobalID:0x00007f90cdc294e8 @uri=#<URI::GID gid://gitlab/User/1>>
=> true
# 退出数据库连接
irb(main):005:0> quit

至此root账号的密码修改完成,可以用新的密码登录gitlab后台了。

三、报错处理

本次修改gitlab的root账号密码遇到一个报错。

在连接gitlab数据库production时报错,报错如下:

记一次gitlab修改root密码过程_gitlab

原来用的连接数据库的命令是gitlab-rails console production,报以上错误。应该是gitlab版本的问题,该命令需要加-e参数,正确命令是gitlab-rails console -e production