Solution of error 1918 when installing MySQL ODBC Driver

I once found the following error when installing MySQL ODBC Driver 5.3.7:

Error 1918. Error installing ODBC driver MySQL ODBC 5.3 ANSI Driver, ODBC error 13: The setup routines for the MySQL ODBC 5.3 ANSI Driver ODBC driver could not be loaded due to system error code 126: The specified module could not be found. …

The cause of the error is not installed Microsoft Visual C++ 2013 Redistributable Package. You can download it HERE

Depending on the version of MySQL ODBC Driver installed, x86 or x64, you need to download and install the Microsoft Visual C ++ 2013 Redistributable Package.

After this, the error should not be.

I note that for older versions of MySQL ODBC Driver 5.1, Microsoft Visual C ++ 2010 Redistributable Package was needed, and in new versions it will probably be required newer.
The current installation instructions for Connector/ODBC can be found at the link on the download page or HERE.

How to fix error “Table ‘name’ is marked as crashed and last (automatic?) repair failed”

Once in the FreeRADIUS logs I noticed a MySQL error:

Table ‘./radius/radacct’ is marked as crashed and last (automatic?) repair failed

As it turned out, the radacct table was damaged, since the data there were not particularly important, then the entire table was cleaned.
You can clean up via phpMyAdmin or SQL query:

truncate table TableName

A bit later for the experiment I decided to break the whole database, took another large table in general from another application, about 8 gigabytes in size and 80 million lines.
I applied to it SQL query to clean up old rows before the date specified in the query and rebooted at that moment MySQL, the request was interrupted, the database was left intact, executed the request to optimize the database and again rebooted MySQL, eventually got a corrupted database and a similar error:

#144 – Table ‘name’ is marked as crashed and last (automatic?) repair failed

To restore the database, you must stop the MySQL server (if the table is not used, then you can not stop it):

sudo service mysql stop

Let’s move to the directory with the database:

cd /var/lib/mysql/$DATABASE_NAME

Execute the command to restore the specified table:

myisamchk -r -o -f -v $TABLE_NAME

Upon completion, if you stopped the MySQL server, then run it:

sudo service mysql start

Similarly, on the test, also to speed up the process, the table was restored by copying it to another more powerful server, namely three files /var/lib/mysql/$DATABASE_NAME/ ($TABLE_NAME.MYD, $TABLE_NAME.MYI, $TABLE_NAME.frm).

How to change MySQL database encoding and its tables

Here is an example of changing the MySQL encoding of a database and tables.
Before any actions on important data it is necessary to make a backup copy, for example:

mysqldump -u USER -h localhost -p BASE | gzip -c > backup_base_`date +%Y-%m-%d`.sql.gz

For the test, we will connect to MySQL and create a couple of new databases without specifying the encoding and specifying:

mysql -u root -p
CREATE DATABASE test_db1;
CREATE DATABASE test_db2 CHARACTER SET utf8 COLLATE utf8_general_ci;

Create a test table in the first database and see its encoding:

USE test_db1;

CREATE TABLE users (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50),
reg_date TIMESTAMP
);

show table status like 'users';

Create a test table in the second database and see its encoding:

USE test_db2;

CREATE TABLE users (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50),
reg_date TIMESTAMP
);

show table status;

Let’s also look at the encoding of both databases:

SELECT default_character_set_name FROM information_schema.SCHEMATA WHERE schema_name = "test_db1";
SELECT default_character_set_name FROM information_schema.SCHEMATA WHERE schema_name = "test_db2";

To see the encoding of a column in a specific table, you can do this:

SELECT character_set_name FROM information_schema.`COLUMNS`
WHERE table_schema = "test_db1"
AND table_name = "users"
AND column_name = "firstname";

In my case, the table in the first database was encoded with latin1_swedish_ci, since it is standard, and in the second one utf8_general_ci, since I specified it beforehand.

You can see the table of possible encodings by such requests:

show collation;
show collation like 'utf8%';
show collation like 'latin1%';

View existing databases as follows:

show databases;

View existing tables in the database:

USE test_db1;
show tables;

Now we change the encoding of the first database and its tables to utf8 and immediately check:

ALTER DATABASE `test_db1` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
USE test_db1;
ALTER TABLE `test_db1`.`users` CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;
show table status;

If you need to change the encoding in a sql file, open it in the Notepad ++ editor, for example, convert it to UTF-8/without BOM, and if the encoding in SET NAMES is specified at the beginning of the file, change it there, then you can import the file into the database.

How to create a MySQL user and configure access rights

To create a user, we first connect to the MySQL server console:

mysql

Let’s see what users are:

select * from mysql.user;
select user,host from mysql.user;

Create a user (where localhost is specified from where the user can connect, you can specify the IP address, localhost – from the local machine where the MySQL server itself, or % from any addresses):

CREATE USER 'user'@'localhost' IDENTIFIED BY 'password';

If you intend to connect not only locally, you need to comment out the line in my.cnf:

#bind-address = 127.0.0.1

And restart the MySQL server:

sudo service mysql restart

After that, I recommend restricting access to MySQL using IPTables.
See also – Configuring IPTables

To assign the newly created user unlimited permissions to a specific database, execute the following command:

GRANT ALL PRIVILEGES ON database_name.* TO 'user'@'localhost';

If necessary on all bases:

GRANT ALL PRIVILEGES ON *.* TO 'user'@'localhost';

You can specify specific access rights:

GRANT SELECT ON database_name.* TO 'user'@'localhost';
GRANT SELECT, INSERT ON database_name.table_name TO user@192.168.1.5;

If you want to create a new database:

CREATE DATABASE database_name;

For the changes to take effect, execute:

FLUSH PRIVILEGES;

You can delete the user as follows:

DROP USER 'user'@'localhost';

Example of viewing privileges:

SHOW GRANTS FOR 'user'@'localhost';
SHOW GRANTS;
SELECT * FROM information_schema.user_privileges;

Import and export MySQL databases

Below are examples of importing and exporting MySQL databases from a Linux terminal.

Export the database to a file like this:

mysqldump -u user -h localhost -p DATABASE_NAME > DATABASE_NAME.sql

You can export directly to the archive:

mysqldump -u user -h localhost -p DATABASE_NAME | gzip -c > /dir/DATABASE_NAME_`date +%Y-%m-%d`.sql.gz

Export several databases into one file and immediately compress it as follows:

mysqldump -u user -h localhost -p -B DATABASE_NAME1 DATABASE_NAME2 | gzip -c > /dir/DATABASE_NAME_`date +%Y-%m-%d`.sql.gz

You can import the compressed file back into the database like this:

mysql -u user -p DATABASE_NAME < /dir/DATABASE_NAME_`date +%Y-%m-%d`.sql.gz

A non-compressed file is similar:

mysql -u user -p DATABASE_NAME < /dir/DATABASE_NAME_`date +%Y-%m-%d`.sql

The -p option indicates that you must enter the password during the connection to the MySQL server, so that you can not enter it in the command (for security reasons it is not desirable), for example:

mysql -u user -pPASSWORD DATABASE_NAME < DATABASE_NAME.sql

The solution of error “ERROR 1067 (42000) at line 211: Invalid default value for ‘blablabla'”

I noticed once when importing a sql file the following error:

ERROR 1067 (42000) at line 211: Invalid default value for ‘blablabla’

It arises because new versions of MySQL server use strict mode and parameters such as NO_ZERO_DATE do not allow entering date values like ‘0000-00-00’ into the database.

Connect to mysql server:

mysql -u root -p

Execute a query that displays the values of sql_mode:

show variables like 'sql_mode';

Copy the string with these values and exit mysql:

exit

Open the configuration file for example in the text editor nano (Ctrl+X for exit, y/n for saving or canceling the changes):

sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf

I did not have sql_mode = in the file, so at the end of the file I inserted the line with the previously copied values, removing NO_ZERO_IN_DATE, NO_ZERO_DATE from it, in my case, the following happened:

sql_mode=ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

Restart mysql to apply the changes:

sudo service mysql restart

Done, now when importing this error should not be.

Monitoring the size of a MySQL database or table in Zabbix

To see a table with a list of databases and their size in megabytes, you can execute a SQL query:

SELECT table_schema "databases name", sum(data_length + index_length)/1024/1024  "DВ size in MB" FROM information_schema.TABLES GROUP BY table_schema;

To see the size of the tables of a particular database, for example zabbix, you can execute SQL:

SELECT table_name AS `Table`, round(((data_length + index_length) / 1024 / 1024), 2) `Size in MB` FROM information_schema.TABLES WHERE table_schema = "zabbix";

Or, in bytes, see the size of a single database with the name zabbix:

SELECT SUM( data_length + index_length ) AS 'size'
FROM information_schema.TABLES
WHERE table_schema = 'zabbix'
LIMIT 1;

To see the size of a particular database table, for example, zabbix.history:

SELECT SUM( data_length + index_length ) AS 'size'
FROM information_schema.TABLES
WHERE table_schema = 'zabbix' AND table_name = 'history'
LIMIT 1;

We use the examples of previous requests for monitoring. Create a script in the script directory Zabbix, for example, with the name zabbix_database_size.sh and content:

mysql -u root -pPASSWORD -h 127.0.0.1 -e "SELECT SUM( data_length + index_length ) AS 'size' FROM information_schema.TABLES WHERE table_schema = 'zabbix' LIMIT 1;" -s -N

In Zabbix network node or in a new template, we create a data element in which we indicate:

Name: Zabbix Database Size
Type: External check
Key: zabbix_database_size.sh
Type of information: Numeric (positive integer)
Unit of measurement: b

Accordingly, we will create a graph for it.

Done.

Change wait_timeout and interactive_timeout in MySQL

wait_timeout – The number of seconds that the server waits for activity in a non-interactive connection before closing it.
At the time of connection, wait_timeout is taken from the global value wait_timeout or interactive_timeout depending on the client type (as defined by the CLIENT_INTERACTIVE connect option for mysql_real_connect ())

Connect to MySQL and see the current value:

mysql -u USER -p
show variables like "wait_timeout";
show variables like "interactive_timeout";
quit;

By default, the values wait_timeout and interactive_timeout are 28800 seconds = 8 hours.
You can set minimum 1, maximum – 31536000, maximum (for Windows) – 2147483.

You can change the value of wait_timeout by executing the SQL query, for example:

set global wait_timeout = 28800;
set global interactive_timeout = 28800;

That the set value has not been reset, it needs to be specified in the file /etc/mysql/my.cnf, in the mysqld block:

[mysqld]
wait_timeout = 28800
interactive_timeout = 28800

Change connect_timeout in MySQL

connect_timeout – the number of seconds that the mysql server waits for the connection package before terminating the connection.

Connect to MySQL and see the current value:

mysql -u USER -p
show variables like "connect_timeout";
quit;

The value of connect_timeout can be specified in the file /etc/mysql/my.cnf, for example:

[mysqld]
connect_timeout=10

In real time, you can change by executing the SQL query (after restarting MySQL it will be reset to the standard or specified in the configuration file):

SET GLOBAL connect_timeout=10;

The standard value is 10, the minimum value is 2, the maximum is 31536000.