Replace `mysql` with `mysql2`
Describe the Bug
Changing DB_CLIENT from mysql to mysql2 and installing the mysql2 package leads to an error about unsupported database client.
Though I understand this is not a bug, I did find in the code that both Knex and the Directus schema code support mysql2 so wonder if this was just missed?
To Reproduce
Ad in in description switch to mysql2
Directus Version
10.5.2
Hosting Strategy
Self-Hosted (Docker Image)
I'd rather not try to support both if the end result is the same, so I'm hijacking this issue as a reminder to replace mysql with mysql2 completely 🙂
Please don't forget about this, the mysql error logs are getting ridiculous. Thanks for all your hard work.
+1 for this feature
I can reveal that I have been using Directus with mysql2 for the past year now in three production environments and have had zero issues so far, if anyone was wondering about possible issues.
I apply it via a hotfix overwrite of some directus files
How are you applying the hotfix? Are you manually editing files in node_modules?
Yeh, from the start I have been using Directus via npm install, since there are so many hot-fixes I need just for different reasons. Here is my full set of hot fixes, most are done for performance optimisations that I have made PRs for, but they are not in Directus yet. So if you only use mysql, then you should be able to use this: Unzip into your root Directus folder (same as where extensions folder is) then run node hot-fix.js
Nice one thanks will take a look!
A better way to do this is to just run this bash command after install to find/replace (can even execute inside Docker):
sed -i -e 's/Client_MySQL/Client_MySQL2/' node_modules/@directus/api/dist/database/index.js
Also set DB_CLIENT="mysql2" in your .env file.
this issue gains importance. you cannot start directus with actual versions of mysql8.... even if you use: --mysql-native-password=ON on the mysql-container, the default generated user has no "mysql_native_password" auth... so directus is not starting alongside with mysql bigger than 8.3.
this issue gains importance. you cannot start directus with actual versions of mysql8.... even if you use: --mysql-native-password=ON on the mysql-container, the default generated user has no "mysql_native_password" auth... so directus is not starting alongside with mysql bigger than 8.3.
Try this solution. It helped me run the container. https://stackoverflow.com/a/78447221/4022411
But this issue is really becoming important.
+1 for getting this fixed. Not only is using mysql_native_password less secure, but this is making it increasingly difficult to use MySQL databases with Directus. And since Directus allows you to use it with a preexisting database, the answer isn't always as straightforward as saying 'use another database'...
Based on the suggestion by @fire015, I build this minimal Dockerfile for those using docker.
FROM directus/directus
WORKDIR /directus
#Directus uses pnpm as it's package manager, but the official docker image doesn't come with it installed.
#The current user (node) doesn't have permission to install global packages, so we switch to root and install pnpm.
USER root
RUN npm install -g [email protected]
#We can now switch back to the node user and run our install command to add mysql2 lib.
USER node
RUN pnpm i mysql2
#Then we run the command provided by our friend [fire015](https://github.com/fire015)
RUN sed -i -e 's/Client_MySQL/Client_MySQL2/' node_modules/@directus/api/dist/database/index.js
Wow wow wow, today I upgrade all my containers (include MySQL 8.4 to 9) and suddenly my Directus broken, with above solution by @marchiolidev I can start again with mysql2 and everything run smoothly :) Just add Dockerfile and need rebuild the image, if there is error during build, usually because pnpm version mismatch, just follow the error message, you will be saved hahaah.
Please note that the mysql2 PR has been merged in Directus v11, which is currently available as a release candidate.
I ran into this issue and was able to use https://stackoverflow.com/a/50130973, and there's lots of variations out there. I'm doing this only for local development so I'm not concerned about the issues surrounding this approach. I just ran the mysql command: ALTER USER 'MY_USER' IDENTIFIED WITH mysql_native_password BY 'MY_PASS';