# SQL error when upgrading from 6.0.4 to 6.7.2

**URL:** <https://forum.mattermost.com/t/sql-error-when-upgrading-from-6-0-4-to-6-7-2/14908>\
**Category:** Troubleshooting\
**Created:** [January 26, 2023, 4:52am UTC](https://forum.mattermost.com/t/sql-error-when-upgrading-from-6-0-4-to-6-7-2/14908 "2023-01-26T04:52:12Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![will.graham](https://avatars.discourse-cdn.com/v4/letter/w/eb8c5e/32.png) [@will.graham](https://forum.mattermost.com/u/will.graham)\
**Post date:** [January 26, 2023, 4:52am UTC](https://forum.mattermost.com/t/sql-error-when-upgrading-from-6-0-4-to-6-7-2/14908/1 "2023-01-26T04:52:12Z")

</div>

For feature requests, please see: [https://portal.productboard.com/mattermost/33-what-matters-to-you](https://portal.productboard.com/mattermost/33-what-matters-to-you).

For troubleshooting questions, please post in the following format:

**Summary**  
Mattermost service fails to start after upgrade with a sql error

**Steps to reproduce**  
upgrading from 6.0.4 to 6.7.2 in order to keep upgrading to meet compliance

**Expected behavior**  
ran the upgrade steps and mattermost service won’t start

## – Unit mattermost.service has begun starting up. Jan 26 04:29:26 ip-172-31-39-85 mattermost[24349]: {“timestamp”:“2023-01-26 04:29:26.122 Z”,“level”:“info”,“msg”:“Server is initializing…”,“caller”:“app/server.go:262”,“go\_version”:“go1.18.1”} Jan 26 04:29:26 ip-172-31-39-85 mattermost[24349]: {“timestamp”:“2023-01-26 04:29:26.125 Z”,“level”:“info”,“msg”:“Pinging SQL”,“caller”:“sqlstore/store.go:226”,“database”:“master”} Jan 26 04:29:26 ip-172-31-39-85 mattermost[24349]: {“timestamp”:“2023-01-26 04:29:26.150 Z”,“level”:“info”,“msg”:“Pinging SQL”,“caller”:“sqlstore/store.go:226”,“database”:“master”} Jan 26 04:29:26 ip-172-31-39-85 mattermost[24349]: {“timestamp”:“2023-01-26 04:29:26.175 Z”,“level”:“fatal”,“msg”:“Failed to apply database migrations.”,“caller”:“sqlstore/store.go:163”,“error”:"driver: mysql, mess Jan 26 04:29:26 ip-172-31-39-85 systemd[1]: mattermost.service: Main process exited, code=exited, status=1/FAILURE Jan 26 04:29:26 ip-172-31-39-85 systemd[1]: mattermost.service: Failed with result ‘exit-code’. Jan 26 04:29:26 ip-172-31-39-85 systemd[1]: Failed to start Mattermost. – Subject: Unit mattermost.service has failed – Defined-By: systemd – Support: [http://www.ubuntu.com/support](http://www.ubuntu.com/support)

## – Unit mattermost.service has failed.

– The result is RESULT.

Trying not to restore as it’s on AWS

---

<div class="post-metadata">

**Author:** ![agriesser](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mattermost.com/agriesser/32/5644_2.png) [@agriesser](https://forum.mattermost.com/u/agriesser)\
**Post date:** [January 26, 2023, 5:21am UTC](https://forum.mattermost.com/t/sql-error-when-upgrading-from-6-0-4-to-6-7-2/14908/2 "2023-01-26T05:21:30Z")

</div>

Hi @will.graham and welcome to the Mattermost forums!

Can you please have a look at your Mattermost logfile? The default location is `/opt/mattermost/logs/mattermost.log`. It might contain some additional hints that would help us to idenfity the reason for the issue you’re seeing here.

---

<div class="post-metadata">

**Author:** ![will.graham](https://avatars.discourse-cdn.com/v4/letter/w/eb8c5e/32.png) [@will.graham](https://forum.mattermost.com/u/will.graham)\
**Post date:** [January 26, 2023, 6:10am UTC](https://forum.mattermost.com/t/sql-error-when-upgrading-from-6-0-4-to-6-7-2/14908/3 "2023-01-26T06:10:56Z")

</div>

{“timestamp”:“2023-01-26 04:41:34.802 Z”,“level”:“info”,“msg”:“Server is initializing…”,“caller”:“app/server.go:262”,“go\_version”:“go1.18.1”}  
{“timestamp”:“2023-01-26 04:41:34.803 Z”,“level”:“info”,“msg”:“Pinging SQL”,“caller”:“sqlstore/store.go:226”,“database”:“master”}  
{“timestamp”:“2023-01-26 04:41:34.829 Z”,“level”:“info”,“msg”:“Pinging SQL”,“caller”:“sqlstore/store.go:226”,“database”:“master”}  
{“timestamp”:“2023-01-26 04:41:34.866 Z”,“level”:“fatal”,“msg”:“Failed to apply database migrations.”,“caller”:“sqlstore/store.go:163”,“error”:“driver: mysql, message: failed when applying migration, command: apply\_migration, originalError: Error 1304: PROCEDURE Migrate\_LastRootPostAt already exists, query: \n\nCREATE PROCEDURE Migrate\_LastRootPostAt ()\nBEGIN\nDECLARE\n\tLastRootPostAt\_EXIST INT;\n\tSELECT\n\t\tCOUNT(\*)\n\tFROM\n\t\tINFORMATION\_SCHEMA.COLUMNS\n\tWHERE\n\t\tTABLE\_NAME = ‘Channels’\n\t\tAND table\_schema = DATABASE()\n\t\tAND COLUMN\_NAME = ‘LastRootPostAt’ INTO LastRootPostAt\_EXIST;\n\tIF(LastRootPostAt\_EXIST = 0) THEN\n ALTER TABLE Channels ADD COLUMN LastRootPostAt bigint DEFAULT 0;\n\t\tUPDATE\n\t\t\tChannels\n\t\t\tINNER JOIN (\n\t\t\t\tSELECT\n\t\t\t\t\tChannels.Id channelid,\n\t\t\t\t\tCOALESCE(MAX(Posts.CreateAt), 0) AS lastrootpost\n\t\t\t\tFROM\n\t\t\t\t\tChannels\n\t\t\t\t\tLEFT JOIN Posts FORCE INDEX (idx\_posts\_channel\_id\_update\_at) ON Channels.Id = Posts.ChannelId\n\t\t\t\tWHERE\n\t\t\t\t\tPosts.RootId = ‘’\n\t\t\t\tGROUP BY\n\t\t\t\t\tChannels.Id) AS q ON q.channelid = Channels.Id SET LastRootPostAt = lastrootpost;\n\tEND IF;\nEND;\n\tCALL Migrate\_LastRootPostAt ();\n\tDROP PROCEDURE IF EXISTS Migrate\_LastRootPostAt;\n\n”}

---

<div class="post-metadata">

**Author:** ![will.graham](https://avatars.discourse-cdn.com/v4/letter/w/eb8c5e/32.png) [@will.graham](https://forum.mattermost.com/u/will.graham)\
**Post date:** [January 26, 2023, 6:11am UTC](https://forum.mattermost.com/t/sql-error-when-upgrading-from-6-0-4-to-6-7-2/14908/4 "2023-01-26T06:11:38Z")

</div>

thanks for replying so quickly btw Alexander

---

<div class="post-metadata">

**Author:** ![agriesser](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mattermost.com/agriesser/32/5644_2.png) [@agriesser](https://forum.mattermost.com/u/agriesser)\
**Post date:** [January 26, 2023, 6:36am UTC](https://forum.mattermost.com/t/sql-error-when-upgrading-from-6-0-4-to-6-7-2/14908/5 "2023-01-26T06:36:32Z")

</div>

Alright, thanks - I’m not 100% sure I can read that out of the logs, but is that a MySQL or PostgreSQL deployment you’re having here?

Can you please connect to your Mattermost database and run the following command:

```sql
DROP PROCEDURE IF EXISTS Migrate_LastRootPostAt;

```

Then try to start the server again and see if it works now. If not, please look at your `mattermost.log` file again and share the last few lines again.

---

<div class="post-metadata">

**Author:** ![will.graham](https://avatars.discourse-cdn.com/v4/letter/w/eb8c5e/32.png) [@will.graham](https://forum.mattermost.com/u/will.graham)\
**Post date:** [January 26, 2023, 7:51am UTC](https://forum.mattermost.com/t/sql-error-when-upgrading-from-6-0-4-to-6-7-2/14908/6 "2023-01-26T07:51:12Z")

</div>

it’s mysql. FYI this command worked and you saved my sanity, if not my job. So thank you very much for your help!

---

<div class="post-metadata">

**Author:** ![agriesser](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mattermost.com/agriesser/32/5644_2.png) [@agriesser](https://forum.mattermost.com/u/agriesser)\
**Post date:** [January 26, 2023, 8:01am UTC](https://forum.mattermost.com/t/sql-error-when-upgrading-from-6-0-4-to-6-7-2/14908/7 "2023-01-26T08:01:54Z")

</div>

You’re welcome - glad to hear it’s up and running again 🙂
