1. Background
I exported the database table structure and data from the company server Mysql (version: 8.0.16) and restored it to my local computer Mysql (version: 8.0.29). Just after starting the project, I was prompted with a SqlSugar database connection error. The prompt error is as follows:
Execute Db.Ado.CheckConnection() reports an error
Connection open error . The given key ‘0’ was not present in the dictionary.note: the connection string: database=stocks;server=127.0.0.1;port=3306;uid=root;pwd=123;
The following is the error report problem of individual table query.
DB.Queryable().Where(p=>true).ToList() error. ToList() error, after analysis, the main problem is the current table field character set and sorting rules are not uniform. The error is reported as follows.
MySqlException: “Fatal error encountered attempting to read the resultset.”
Internal exception MySqlException: Expected end of data packet
2. Solution
1. Solution to connection error
Add charset=utf8mb4, and the connection will no longer report errors note: if you want utf8mb4, I use utf8 locally and still report an error
database=stocks;server=127.0.0.1;port=3306;uid=root;pwd=123;charset=utf8mb4;
2. Modify the inconsistency of database character set
Execute the following SQL script as required.
(1) Change the encoding (character set) of a table and the encoding (character set) of all fields in the table:
ALTER TABLE TABLE_NAME CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
·TABLE_NAME replace to your table name.
(2) Change the encoding (character set) of all tables and the encoding (character set) of all fields in the table:
SELECT
CONCAT(
'ALTER TABLE ',
TABLE_NAME,
' CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;'
)
FROM
information_schema.`TABLES`
WHERE
TABLE_SCHEMA = 'DATABASE_NAME';
·DATABASE_NAME replace to your databese name.
3. Other contents
Query all table names of a database:
SHOW FULL COLUMNS FROM TABLE_NAME;
·TABLE_NAME replace to your table name.
Read More:
- host ‘‘ is not allowed to connect to this mysql server Connect MYSQL Error
- [Solved] MYSQL Command Execute Error: Can ‘t connect to local MySQL server through socket ‘/tmp/mysql.sock ‘(2) “
- [Solved] ERROR 2002 (HY000): Can’t connect to local MySQL server through socket’/var/lib/mysql/mysql.sock’ (2)
- MySql Install Error: Can‘t connect to local MySQL server through socket ‘/tmp/mysql.sock‘
- MYSQL Enter password:ERROR 2003 (HY000): Can‘t connect to MySQL server on ‘localhost:3306‘
- How to Solve can‘t connect to local mysql server through socket ‘/var/lib/mysql/mysql.sock‘
- ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/tmp/mysql.sock’
- C# Connect MYSQL Error: MySql.Data.MySqlClient.MySqlException:“SSL Connection error.”
- Remote connection to MySQL database error: is not allowed to connect to this MYSQL server solution
- MySQL Install Error: MySQL error 1042: Unable to connect to any of the specified MySQL hosts
- How to Fix MySQL ERROR 1130 (HY000): Host ‘XXXX’ is not allowed to connect to this MySQL server
- Ubuntu ERROR 2003 (HY000): Can’t connect to MySQL server on ‘127.0.0.1’ (111)
- How to Solve c3p0 connect mysql8.0 Error
- [Solved] MYSQL Connect Error: Communications link failure
- [Solved] asp.NETCORE connect to MySQL error: encoding 1252
- [Solved] Navicat Connect MySQL error: Authentication plugin ‘caching_sha2_password‘ cannot be loaded
- Mysql Flashback Warning: C:\Program Files\MySQL\MySQL Server 8.0\bin\mysql.exe
- Idea connect mysql error Server returns invalid timezone. Go to’Advanced’ tab and set’serverTimezone’ property
- ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock’
- [Solved] Anaconda Error: pyqt can’t use QSqlDatabase to connect to MySQL