1"use strict";(globalThis.webpackChunk_typeorm_docs||=[]).push([[7951],{2162(e,n,s){s.r(n),s.d(n,{assets:()=>d,contentTitle:()=>t,default:()=>h,frontMatter:()=>o,metadata:()=>i,toc:()=>l});const i=JSON.parse('{"id":"drivers/mysql","title":"MySQL / MariaDB","description":"MySQL, MariaDB and Amazon Aurora MySQL are supported as TypeORM drivers.","source":"@site/docs/drivers/mysql.md","sourceDirName":"drivers","slug":"/drivers/mysql","permalink":"/docs/drivers/mysql","draft":false,"unlisted":false,"editUrl":"https://github.com/typeorm/typeorm/tree/master/docs/docs/drivers/mysql.md","tags":[],"version":"current","frontMatter":{},"sidebar":"tutorialSidebar","previous":{"title":"MongoDB","permalink":"/docs/drivers/mongodb"},"next":{"title":"Oracle","permalink":"/docs/drivers/oracle"}}');var r=s(1987),c=s(7008);const o={},t="MySQL / MariaDB",d={},l=[{value:"Installation",id:"installation",level:2},{value:"Data Source Options",id:"data-source-options",level:2},{value:"Column Types",id:"column-types",level:2},{value:"<code>enum</code> column type",id:"enum-column-type",level:3},{value:"<code>set</code> column type",id:"set-column-type",level:3},{value:"Vector Types",id:"vector-types",level:3}];function a(e){const n={a:"a",blockquote:"blockquote",code:"code",h1:"h1",h2:"h2",h3:"h3",header:"header",li:"li",p:"p",pre:"pre",ul:"ul",...(0,c.R)(),...e.components};return(0,r.jsxs)(r.Fragment,{children:[(0,r.jsx)(n.header,{children:(0,r.jsx)(n.h1,{id:"mysql--mariadb",children:"MySQL / MariaDB"})}),"\n",(0,r.jsx)(n.p,{children:"MySQL, MariaDB and Amazon Aurora MySQL are supported as TypeORM drivers."}),"\n",(0,r.jsx)(n.h2,{id:"installation",children:"Installation"}),"\n",(0,r.jsx)(n.pre,{children:(0,r.jsx)(n.code,{className:"language-shell",children:"npm install mysql2\n"})}),"\n",(0,r.jsx)(n.h2,{id:"data-source-options",children:"Data Source Options"}),"\n",(0,r.jsxs)(n.p,{children:["See ",(0,r.jsx)(n.a,{href:"/docs/data-source/data-source-options",children:"Data Source Options"})," for the common data source options. You can use the data source types ",(0,r.jsx)(n.code,{children:"mysql"}),", ",(0,r.jsx)(n.code,{children:"mariadb"})," and ",(0,r.jsx)(n.code,{children:"aurora-mysql"})," to connect to the respective databases."]}),"\n",(0,r.jsxs)(n.ul,{children:["\n",(0,r.jsxs)(n.li,{children:["\n",(0,r.jsxs)(n.p,{children:[(0,r.jsx)(n.code,{children:"url"})," - Connection url where the connection is performed. Please note that other data source options will override parameters set from url."]}),"\n"]}),"\n",(0,r.jsxs)(n.li,{children:["\n",(0,r.jsxs)(n.p,{children:[(0,r.jsx)(n.code,{children:"host"})," - Database host."]}),"\n"]}),"\n",(0,r.jsxs)(n.li,{children:["\n",(0,r.jsxs)(n.p,{children:[(0,r.jsx)(n.code,{children:"port"})," - Database host port. Default mysql port is ",(0,r.jsx)(n.code,{children:"3306"}),"."]}),"\n"]}),"\n",(0,r.jsxs)(n.li,{children:["\n",(0,r.jsxs)(n.p,{children:[(0,r.jsx)(n.code,{children:"username"})," - Database username."]}),"\n"]}),"\n",(0,r.jsxs)(n.li,{children:["\n",(0,r.jsxs)(n.p,{children:[(0,r.jsx)(n.code,{children:"password"})," - Database password."]}),"\n"]}),"\n",(0,r.jsxs)(n.li,{children:["\n",(0,r.jsxs)(n.p,{children:[(0,r.jsx)(n.code,{children:"database"})," - Database name."]}),"\n"]}),"\n",(0,r.jsxs)(n.li,{children:["\n",(0,r.jsxs)(n.p,{children:[(0,r.jsx)(n.code,{children:"socketPath"})," - Database socket path."]}),"\n"]}),"\n",(0,r.jsxs)(n.li,{children:["\n",(0,r.jsxs)(n.p,{children:[(0,r.jsx)(n.code,{children:"poolSize"})," - Maximum number of clients the pool should contain for each connection."]}),"\n"]}),"\n",(0,r.jsxs)(n.li,{children:["\n",(0,r.jsxs)(n.p,{children:[(0,r.jsx)(n.code,{children:"charset"})," and ",(0,r.jsx)(n.code,{children:"collation"})," - The charset/collation for the connection. If an SQL-level charset is specified (like utf8mb4) then the default collation for that charset is used."]}),"\n"]}),"\n",(0,r.jsxs)(n.li,{children:["\n",(0,r.jsxs)(n.p,{children:[(0,r.jsx)(n.code,{children:"timezone"})," - the timezone configured on the MySQL server. This is used to typecast server date/time\nvalues to JavaScript Date object and vice versa. This can be ",(0,r.jsx)(n.code,{children:"local"}),", ",(0,r.jsx)(n.code,{children:"Z"}),", or an offset in the form\n",(0,r.jsx)(n.code,{children:"+HH:MM"})," or ",(0,r.jsx)(n.code,{children:"-HH:MM"}),". (Default: ",(0,r.jsx)(n.code,{children:"local"}),")"]}),"\n"]}),"\n",(0,r.jsxs)(n.li,{children:["\n",(0,r.jsxs)(n.p,{children:[(0,r.jsx)(n.code,{children:"connectTimeout"})," - The milliseconds before a timeout occurs during the initial connection to the MySQL server.\n(Default: ",(0,r.jsx)(n.code,{children:"10000"}),")"]}),"\n"]}),"\n",(0,r.jsxs)(n.li,{children:["\n",(0,r.jsxs)(n.p,{children:[(0,r.jsx)(n.code,{children:"acquireTimeout"})," - The milliseconds before a timeout occurs during the initial connection to the MySQL server. It differs from ",(0,r.jsx)(n.code,{children:"connectTimeout"})," as it governs the TCP connection timeout whereas connectTimeout does not. (default: ",(0,r.jsx)(n.code,{children:"10000"}),")"]}),"\n"]}),"\n",(0,r.jsxs)(n.li,{children:["\n",(0,r.jsxs)(n.p,{children:[(0,r.jsx)(n.code,{children:"insecureAuth"})," - Allow connecting to MySQL instances that ask for the old (insecure) authentication method.\n(Default: ",(0,r.jsx)(n.code,{children:"false"}),")"]}),"\n"]}),"\n",(0,r.jsxs)(n.li,{children:["\n",(0,r.jsxs)(n.p,{children:[(0,r.jsx)(n.code,{children:"supportBigNumbers"})," - When dealing with big numbers (",(0,r.jsx)(n.code,{children:"BIGINT"})," and ",(0,r.jsx)(n.code,{children:"DECIMAL"})," columns) in the database,\nyou should enable this option (Default: ",(0,r.jsx)(n.code,{children:"true"}),")"]}),"\n"]}),"\n",(0,r.jsxs)(n.li,{children:["\n",(0,r.jsxs)(n.p,{children:[(0,r.jsx)(n.code,{children:"bigNumberStrings"})," - Enabling both ",(0,r.jsx)(n.code,{children:"supportBigNumbers"})," and ",(0,r.jsx)(n.code,{children:"bigNumberStrings"})," forces big numbers\n(",(0,r.jsx)(n.code,{children:"BIGINT"})," and ",(0,r.jsx)(n.code,{children:"DECIMAL"}
1)," columns) to be always returned as JavaScript String objects (Default: ",(0,r.jsx)(n.code,{children:"true"}),").\nEnabling ",(0,r.jsx)(n.code,{children:"supportBigNumbers"})," but leaving ",(0,r.jsx)(n.code,{children:"bigNumberStrings"})," disabled will return big numbers as String\nobjects only when they cannot be accurately represented with\n",(0,r.jsx)(n.a,{href:"http://ecma262-5.com/ELS5_HTML.htm#Section_8.5",children:"JavaScript Number objects"}),"\n(which happens when they exceed the ",(0,r.jsx)(n.code,{children:"[-2^53, +2^53]"})," range), otherwise they will be returned as\nNumber objects. This option is ignored if ",(0,r.jsx)(n.code,{children:"supportBigNumbers"})," is disabled."]}),"\n"]}),"\n",(0,r.jsxs)(n.li,{children:["\n",(0,r.jsxs)(n.p,{children:[(0,r.jsx)(n.code,{children:"dateStrings"})," - Force date types (",(0,r.jsx)(n.code,{children:"TIMESTAMP"}),", ",(0,r.jsx)(n.code,{children:"DATETIME"}),", ",(0,r.jsx)(n.code,{children:"DATE"}),") to be returned as strings rather than\ninflated into JavaScript Date objects. Can be true/false or an array of type names to keep as strings.\n(Default: ",(0,r.jsx)(n.code,{children:"false"}),")"]}),"\n"]}),"\n",(0,r.jsxs)(n.li,{children:["\n",(0,r.jsxs)(n.p,{children:[(0,r.jsx)(n.code,{children:"debug"})," - Prints protocol details to stdout. Can be true/false or an array of packet type names that\nshould be printed. (Default: ",(0,r.jsx)(n.code,{children:"false"}),")"]}),"\n"]}),"\n",(0,r.jsxs)(n.li,{children:["\n",(0,r.jsxs)(n.p,{children:[(0,r.jsx)(n.code,{children:"trace"}),' - Generates stack traces on Error to include call site of library entrance ("long stack traces").\nSlight performance penalty for most calls. (Default: ',(0,r.jsx)(n.code,{children:"true"}),")"]}),"\n"]}),"\n",(0,r.jsxs)(n.li,{children:["\n",(0,r.jsxs)(n.p,{children:[(0,r.jsx)(n.code,{children:"multipleStatements"})," - Allow multiple mysql statements per query. Be careful with this, it could increase the scope\nof SQL injection attacks. (Default: ",(0,r.jsx)(n.code,{children:"false"}),")"]}),"\n"]}),"\n",(0,r.jsxs)(n.li,{children:["\n",(0,r.jsxs)(n.p,{children:[(0,r.jsx)(n.code,{children:"legacySpatialSupport"})," - Use legacy spatial functions like ",(0,r.jsx)(n.code,{children:"GeomFromText"})," and ",(0,r.jsx)(n.code,{children:"AsText"})," which have been replaced by the standard-compliant ",(0,r.jsx)(n.code,{children:"ST_GeomFromText"})," or ",(0,r.jsx)(n.code,{children:"ST_AsText"})," in MySQL 8.0. (Default: ",(0,r.jsx)(n.code,{children:"false"}),")"]}),"\n"]}),"\n",(0,r.jsxs)(n.li,{children:["\n",(0,r.jsxs)(n.p,{children:[(0,r.jsx)(n.code,{children:"flags"})," - List of connection flags to use other than the default ones. It is also possible to blacklist default ones.\nFor more information, check ",(0,r.jsx)(n.a,{href:"https://github.com/mysqljs/mysql#connection-flags",children:"Connection Flags"}),"."]}),"\n"]}),"\n",(0,r.jsxs)(n.li,{children:["\n",(0,r.jsxs)(n.p,{children:[(0,r.jsx)(n.code,{children:"ssl"})," - object with SSL parameters or a string containing the name of the SSL profile.\nSee ",(0,r.jsx)(n.a,{href:"https://github.com/mysqljs/mysql#ssl-options",children:"SSL options"}),"."]}),"\n"]}),"\n",(0,r.jsxs)(n.li,{children:["\n",(0,r.jsxs)(n.p,{children:[(0,r.jsx)(n.code,{children:"enableQueryTimeout"})," - If a value is specified for maxQueryExecutionTime, in addition to generating a warning log when a query exceeds this time limit, the specified maxQueryExecutionTime value is also used as the timeout for the query. For more information, check ",(0,r.jsx)(n.a,{href:"https://github.com/mysqljs/mysql#timeouts",children:"mysql timeouts"}),"."]}),"\n"]}),"\n"]}),"\n",(0,r.jsxs)(n.p,{children:["Additional options can be added to the ",(0,r.jsx)(n.code,{children:"extra"})," object and will be passed directly to the client library. See more in the ",(0,r.jsx)(n.a,{href:"https://sidorares.github.io/node-mysql2/docs",children:"mysql2 documentation"}),"."]}),"\n",(0,r.jsx)(n.h2,{id:"column-types",children:"Column Types"}),"\n",(0,r.jsxs)(n.p,{children:[(0,r.jsx)(n.code,{children:"bit"}),", ",(0,r.jsx)(n.code,{children:"int"}),", ",(0,r.jsx)(n.code,{children:"integer"}),", ",(0,r.jsx)(n.code,{children:"tinyint"}),", ",(0,r.jsx)(n.code,{children:"smallint"}),", ",(0,r.jsx)(n.code,{children:"mediumint"}),", ",(0,r.jsx)(n.code,{children:"bigint"}),", ",(0,r.jsx)(n.code,{children:"float"}),", ",(0,r.jsx)(n.code,{children:"double"}),", ",(0,r.jsx)(n.code,{children:"double precision"}),", ",(0,r.jsx)(n.code,{children:"dec"}),", ",(0,r.jsx)(n.code,{children:"decimal"}),", ",(0,r.jsx)(n.code,{children:"numeric"}),", ",(0,r.jsx)(n.code,{children:"fixed"}),", ",(0,r.jsx)(n.code,{children:"bool"}),", ",(0,r.jsx)(n.code,{children:"boolean"}),", ",(0,r.jsx)(n.code,{children:"date"}),", ",(0,r.jsx)(n.code,{children:"datetime"}),", ",(0,r.jsx)(n.code,{children:"timestamp"}),", ",(0,r.jsx)(n.code,{children:"time"}),", ",(0,r.jsx)(n.code,{children:"year"}),", ",(0,r.jsx)(n.code,{children:"char"}),", ",(0,r.jsx)(n.code,{children:"nchar"}),", ",(0,r.jsx)(n.code,{children:"national char"}),", ",(0,r.jsx)(n.code,{children:"varchar"}),", ",(0,r.jsx)(n.code,{children:"nvarchar"}),", ",(0,r.jsx)(n.code,{children:"national varchar"}),", ",(0,r.jsx)(n.code,{children:"text"}),", ",(0,r.jsx)(n.code,{children:"tinytext"}),", ",(0,r.jsx)(n.code,{children:"mediumtext"}),", ",(0,r.jsx)(n.code,{children:"blob"}),", ",(0,r.jsx)(n.code,{children:"longtext"}),", ",(0,r.jsx)(n.code,{children:"tinyblob"}),", ",(0,r.jsx)(n.code,{children:"mediumblob"}),", ",(0,r.jsx)(n.code,{children:"longblob"}),", ",(0,r.jsx)(n.code,{children:"enum"}),", ",(0,r.jsx)(n.code,{children:"set"}),", ",(0,r.jsx)(n.code,{children:"json"}),", ",(0,r.jsx)(n.code,{children:"binary"}),", ",(0,r.jsx)(n.code,{children:"varbinary"}),", ",(0,r.jsx)(n.code,{children:"geometry"}),", ",(0,r.jsx)(n.code,{children:"point"}),", ",(0,r.jsx)(n.code,{children:"linestring"}),", ",(0,r.jsx)(n.code,{children:"polygon"}),", ",(0,r.jsx)(n.code,{children:"multipoint"}),", ",(0,r.jsx)(n.code,{children:"multilinestring"}),", ",(0,r.jsx)(n.code,{children:"multipolygon"}),", ",(0,r.jsx)(n.code,{children:"geometrycollection"}),", ",(0,r.jsx)(n.code,{children:"uuid"}),", ",(0,r.jsx)(n.code,{children:"inet4"}),", ",(0,r.jsx)(n.code,{children:"inet6"})]}),"\n",(0,r.jsxs)(n.blockquote,{children:["\n",(0,r.jsxs)(n.p,{children:["Note: ",(0,r.jsx)(n.code,{children:"uuid"}),", ",(0,r.jsx)(n.code,{children:"inet4"}),", and ",(0,r.jsx)(n.code,{children:"inet6"})," are only available for MariaDB and for the respective versions that made them available."]}),"\n"]}),"\n",(0,r.jsxs)(n.h3,{id:"enum-column-type",children:[(0,r.jsx)(n.code,{children:"enum"})," column type"]}),"\n",(0,r.jsxs)(n.p,{children:["See ",(0,r.jsx)(n.a,{href:"/docs/entity/entities#enum-column-type",children:"enum column type"}),"."]}),"\n",(0,r.jsxs)(n.h3,{id:"set-column-type",children:[(0,r.jsx)(n.code,{children:"set"})," column type"]}),"\n",(0,r.jsxs)(n.p,{children:[(0,r.jsx)(n.code,{children:"set"})," column type is supported by ",(0,r.jsx)(n.code,{children:"mariadb"})," and ",(0,r.jsx)(n.code,{children:"mysql"}),". There are various possible column definitions:"]}),"\n",(0,r.jsx)(n.p,{children:"Using TypeScript enums:"}),"\n",(0,r.jsx)(n.pre,{children:(0,r.jsx)(n.code,{className:"language-typescript",children:'export enum UserRole {\n ADMIN = "admin",\n EDITOR = "editor",\n GHOST = "ghost",\n}\n\n@Entity()\nexport class User {\n @PrimaryGeneratedColumn()\n id: number\n\n @Column({\n type: "set",\n enum: UserRole,\n default: [UserRole.GHOST, UserRole.EDITOR],\n })\n roles: UserRole[]\n}\n'})}),"\n",(0,r.jsxs)(n.p,{children:["Using an array with ",(0,r.jsx)(n.code,{children:"set"})," values:"]}),"\n",(0,r.jsx)(n.pre,{children:(0,r.jsx)(n.code,{className:"language-typescript",children:'export type UserRoleType = "admin" | "editor" | "ghost"\n\n@Entity()\nexport class User {\n @PrimaryGeneratedColumn()\n id: number\n\n @Column({\n type: "set",\n enum: ["admin", "editor", "ghost"],\n default: ["ghost", "editor"],\n })\n roles: UserRoleType[]\n}\n'})}),"\n",(0,r.jsx)(n.h3,{id:"vector-types",children:"Vector Types"}),"\n",(0,r.jsxs)(n.p,{children:["MySQL supports the ",(0,r.jsx)(n.a,{href:"https://dev.mysql.com/doc/refman/en/vector.html",children:"VECTOR type"})," since version 9.0, while in MariaDB, ",(0,r.jsx)(n.a,{href:"https://mariadb.com/docs/server/reference/sql-structure/vectors/vector-overview",children:"vectors"})," are available since 11.7."]})]})}function h(e={}){const{wrapper:n}={...(0,c.R)(),...e.components};return n?(0,r.jsx)(n,{...e,children:(0,r.jsx)(a,{...e})}):a(e)}},7008(e,n,s){s.d(n,{R:()=>o,x:()=>t});var i=s(1763);const r={},c=i.createContext(r);function o(e){const n=i.useContext(c);return i.useMemo(function(){return"function"==typeof e?e(n):{...n,...e}},[n,e])}function t(e){let n;return n=e.disableParentContext?"function"==typeof e.components?e.components(r):e.components||r:o(e.components),i.createElement(c.Provider,{value:n},e.children)}}}]);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.