Category Archives: MySQL

MYSQL: How to Use ifnull()

Generally, when we use the ifnull() method, it is similar to the following statement:

IFNULL(expr1,expr2)
If expr1 is not NULL, IFNULL() returns expr1, otherwise it returns expr2. IFNULL() returns a number or string value

select ifnull(name,’no name’) from person;

Convert the null in the query result to the specified string, but in fact, you can also use ifnull() in the where query part:

select * from person where ifnull(name,’no name’)=’no name’;

This kind of use seems a bit stupid, but in some applications it can reduce the amount of code very well.

How to Solved Error: 18456, Severity: 14, State: 38.

If you have installed Windows SharePoint 3.0, “Error: 18456, Severity: 14, State: 38”, caused SQLSERVERAGENT does not start.

Set start mode for SQL Server Agent to automatic, fix this problem

I also encountered the Error 18456 Sev 14 State 16 on our MS Sql Server. At first i try to reset the password for my user but i have no success. But when I try again to look at the users properties having the error i found out that there is no default database assign for that user. I just assign a database and presto I can log on to my database.

My Slotuion:

1. Log on using sa account.

2. Expand secuirity and logins.

3. Right click on the user account with login problem problem and choose properties.

4. On properties genral tab, you will see an drop down option for default database used for the account, choose a database for the account to used and click ok or apply then ok.

5. Log off on sa account and try to log in again.

That solution works for me.

Solve ERROR 1231 (42000): Variable’time_zone’ can’t

According to the configuration file, MySQL limits the size of the data packets accepted by the Server. Sometimes large inserts and updates are limited by the max_allowed_packet parameter, causing write or update failures. (For example, import database, data table)

The following errors appear in the process of migrating or restoring the mysql database:

ERROR 1231 ( 42000 ): Variable ' time_zone ' can ' t be set to the value of ' NULL ' 
ERROR 1231 (42000): Variable ' sql_mode ' can ' t be set  to the value of  ' NULL ' 
ERROR 1231 ( 42000 ): Variable ' foreign_key_checks ' can ' t be set to the value of ' NULL ' 
ERROR 1231 (42000): Variable 'unique_checks ' can ' t be set  to the value of  ' NULL ' 
ERROR 1231 ( 42000 ): Variable ' character_set_client ' can ' t be set to the value of ' NULL '
Query OK, 0 rows affected (0.00 sec)
ERROR 1231 (42000): Variable ' collation_connection ' can ' t be set  to the value of  ' NULL ' 
ERROR 1231 ( 42000 ): Variable ' sql_notes ' can ' t be set to the value of ' NULL '

Solution:

Modify the mysql configuration file: max_allowed_packet=1024M

View the current configuration:

code show as below:
Copy code
show VARIABLES like  ' %max_allowed_packet% ' ;

The results displayed are:

code show as below:
+——————–+———+
| Variable_name | Value |
+———— ——–+———+
| max_allowed_packet | 1048576 |
+——————–+—- —–+

The above description shows that the current configuration is: 1M

 

Modification method

1. Modify the configuration file

You can edit my.cnf to modify (my.ini under windows), and modify it in the [mysqld] section or the mysql server configuration section.

code show as below:
max_allowed_packet = 20M

If you can’t find my.cnf, you can pass

code show as below:
mysql –help | grep my.cnf

Look for the my.cnf file.
The file is under /etc/ under linux.

Restart mysql:

1. Start with service: service mysqld restart
2. Start with mysqld script: /etc/inint.d/mysqld restart

Check whether max_allowed_packet is successfully edited

Note: Setting this value too small will cause a single record to fail to write to the database after exceeding the limit, and subsequent record writing will also fail.

[Solved] SQLSTATE[HY000] [2002] Connection refused to report an error when PHP connects to mysql in the docker container

Laradock is a complete PHP local development environment provided by Docker

Error when connecting to MySQL in the framework

SQLSTATE[HY000] [2002] Connection refused

The main reason is that there is not enough understanding of the isolation mechanism of Docker containers. Each container is isolated. If there are interdependent services, it is necessary to perform display associations, such as using options --link.
In the same way, docker-composewhen using , the association between containers is similar to the following method:

# docker-compose.xml basic
version: '2'
services:
	
	...
	
    php:
        build: ./php
        
        ...
        
        links:
            - "mysql"
           
    mysql:
        build: ./mysql
        ports:
            - "3306:3306"
        environment:
            MYSQL_PASSWORD: root

Note that the key point is here: the code to test the connection to MySQL is actually running in the container corresponding to PHP, and the MySQL service is in its own container. When our host fills in 127.0.0.1, it actually corresponds to the PHP container. Inside, so it is impossible to find the corresponding service, which causes the above connection refused error.

So, how do you connect?

In fact, after the containers are associated, they can be connected by the container name.

In the above docker-compose.xmldocument, the container corresponds to the service name MySQL mysql, PHP container name is associated with it mysql, so the 127.0.0.1change mysqland then connect to.

# thinkphp project modify database.php file
'hostname'        => 'mysql',

# Laravel project modify .env file
DB_HOST=mysql

How to Solve mysql [Err] 1067-Invalid default value for

Cause of error

The default value incompatibility problem caused by mysql5.7 version, the same problem may also occur in mysql8.0.

The problematic values ​​are:
NO_ZERO_IN_DATE
In strict mode, the date and month are not allowed to be zero.

NO_ZERO_DATE
Set this value, mysql database does not allow the insertion of a zero date, and inserting a zero date will throw an error instead of a warning.

ONLY_FULL_GROUP_BY
For the GROUP BY aggregation operation, if the column in the SELECT does not appear in the GROUP BY, then this SQL is illegal because the column is not in the GROUP BY clause.

Solution

method one

Execute select @@sql_mode, copy the value of the query and delete NO_ZERO_DATE, and then execute set sql_mode = ‘modify number’.

This method only works in the current session

Way two

Execute select @@global.sql_mode, copy the value of the query and delete NO_ZERO_DATE, and then execute set global sql_mode = ‘modify number’.

This method takes effect in the current service, and it becomes invalid after restarting the MySQL service

Method Three

In the mysql installation directory, open the my.ini or my.cnf file. Under wamp, SQL_MODE is not set in MySQL 5.7.

1. Found in my.ini file [mysqld]

2. If there is no SQL_MODE, add it, and modify it if it has

sql_mode="STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION"

or

sql_mode=ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

Finally restart MySQL

service mysqld restart

[Solved] docker mysql SQLSTATE[HY000] [2002] Connection refused

Use docker to build the lnmp environment, and use a sub-container construction scheme. The
framework uses thinkphp and wants to connect to the data MySQL. It always shows “SQLSTATE[HY000] [2002] Connection refused”. The
database configuration host is filled with localhost, and then changed to 127.0 .0.1, I can’t connect.
I thought it was a database user permission problem. I created a new user and refreshed the permissions. The result was still not working for
a long time. It turned out to be a problem of sub-containers. If you connect to 127.0.0.1 or localhost in the container, you must not be able to connect to the database MySQL. You
should fill in the database configuration host with the name of the MySQL container. My local MySQL container name is MySQL, so you can connect to it.

How to Solve MYSQL Error: Failed to start MySQL 8.0 database server

the reason

It appears in the mysql error log: The innodb_system data file’ibdata1′ must be writable, literally: ibdata1 must be writable

Check the log and report an error, the folder has no write permission

cat /var/log/mysqld.log

solve

Find file path

find / -name ibdata1

Grant folder permissions

chmod -R 777 /var/lib/mysql
chown mysql:mysql -R /var/lib/mysql

Start MySQL

systemctl start mysqld.service
// CHECK MySQL STATUS
systemctl status mysqld.service

PLSQL environment variable configuration tnsnames.ora file path

1、 Directory structure

D:\install\PLSQL
    |-- instantclient_11_2
        |-- tnsnames.ora
    |-- PLSQL Developer
    |-- readme.txt

2、 Environment variables
you must configure environment variables, otherwise the database fields will be added with random comments

NLS_LANG = SIMPLIFIED CHINESE_CHINA.ZHS16GBK

TNS_ADMIN = D:\install\PLSQL\instantclient_11_2

3、 PL/SQL developer environment settings

1. Location

    Tools -> Preferences -> Connections

2. Settings

    Oracle home directory name = D:\install\PLSQL\instantclient_11_2

    OCI library = D:\install\PLSQL\instantclient_11_2\oci.dll 

4. Supplement

The tnsnames.ora file needs to be created manually and set up accordingly, refer to the online tutorial.

Problem: you need to modify the tnsnames.ora file to configure the database connection, but you can’t find the path of the file. Many online searches say that it’s in the app directory of disk D, but you still can’t find it.

Solution: open PL/SQL and find help – & gt; Support information
after opening, you will see the PL/SQL version information and other configuration information. If you pull down, there will be a “TNS file”, which is the tnsnames.ora file path of the PL/SQL you installed. After modification, you need to restart PL/SQL

How to release Oracle PLSQL data lock table

(1)See which table is locked
select b.owner,b.object_name,a.session_id,a.locked_mode from vKaTeX parse error: Expected ‘EOF’, got ‘#’ at position 115: …,b.sid,b.serial#̲,logon_time fro…locked_object a,v$session b where a.session_id = b.sid order by b.logon_time;
(3)Kill the corresponding process
Execute the command: alter system kill session ‘1025,41’; (pay attention to the problem of spaces in the middle)
Here is the main point

---Lock table query SQL

SELECT object_name, machine, s.sid, s.serial# ,o.owner

FROM gv$locked_object l, dba_objects o, gv$session s 

WHERE l.object_id = o.object_id 

AND l.session_id = s.sid; 

Use this query to look up the locked object, the locking table device, and the sid and serial

2
--Force process shutdown

alter system kill session '1434, 2425';

Here 1434 and 2425 are the sid and serial of the previous query, respectively

3
--When 'ORA-00030: User session ID does not exist' appears

--query process number

select distinct s.sid,s.serial#,p.spid as system process number

from v$locked_object l , dba_objects o , v$session s , v$process p

where l.object_id=o.object_id and l.session_id=s.sid and s.paddr=p.addr;

4
Log in to the database server using the Xshell tool and execute the following command.

kill -9 system process number

The system process number is the spid queried in the previous step

 

At this point, the table locking problem can be solved perfectly!

MYSQL: CURRENT_TIMESTAMP & ON UPDATE CURRENT_TIMESTAMP

1. Examples

CREATE TABLE `job` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `status` varchar(15) DEFAULT NULL COMMENT 'Status 1: In progress 2 Completed 3 Failed',
  `create_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Creation time',
  `update_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 'modify time',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

2. Explanation

During the insert operation, if there is a timestamp field, the property is set to current_ Time stamp, the current system time is inserted regardless of whether the field has a set value

During the update operation, if a timestamp field property is set to   ON UPDATE CURRENT_ When the data is updated, this field will automatically update the time

Note: these two properties can be used in combination