when pid column of sys.session will be null in mysql? - mysql

Am using mysql 5.7.19-log server, i am working on application monitor tool where i need to monitor session details of a mysql server. In the process came to know that pid column can be null in sys.session table. i want to know what are the cases in which pid can be null?
when i exec desc session in mysql shell result as follows:
mysql>desc session;
+------------------------+------------------------------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------------------+------------------------------------------+------+-----+---------+-------+
| thd_id | bigint(20) unsigned | NO | | NULL | |
| conn_id | bigint(20) unsigned | YES | | NULL | |
| user | varchar(128) | YES | | NULL | |
| db | varchar(64) | YES | | NULL | |
| command | varchar(16) | YES | | NULL | |
| state | varchar(64) | YES | | NULL | |
| time | bigint(20) | YES | | NULL | |
| current_statement | longtext | YES | | NULL | |
| statement_latency | text | YES | | NULL | |
| progress | decimal(26,2) | YES | | NULL | |
| lock_latency | text | YES | | NULL | |
| rows_examined | bigint(20) unsigned | YES | | NULL | |
| rows_sent | bigint(20) unsigned | YES | | NULL | |
| rows_affected | bigint(20) unsigned | YES | | NULL | |
| tmp_tables | bigint(20) unsigned | YES | | NULL | |
| tmp_disk_tables | bigint(20) unsigned | YES | | NULL | |
| full_scan | varchar(3) | NO | | | |
| last_statement | longtext | YES | | NULL | |
| last_statement_latency | text | YES | | NULL | |
| current_memory | text | YES | | NULL | |
| last_wait | varchar(128) | YES | | NULL | |
| last_wait_latency | text | YES | | NULL | |
| source | varchar(64) | YES | | NULL | |
| trx_latency | text | YES | | NULL | |
| trx_state | enum('ACTIVE','COMMITTED','ROLLED BACK') | YES | | NULL | |
| trx_autocommit | enum('YES','NO') | YES | | NULL | |
| pid | varchar(1024) | YES | | NULL | |
| program_name | varchar(1024) | YES | | NULL | |
+------------------------+------------------------------------------+------+-----+---------+-------+
28 rows in set (0.00 sec)```

In your screenshot, You need to check the column with the name Null. It says that whether the value of a field can be null or not. In your case, pid can be null because as per the description of a table, pid allows Null.
Possibilities of pid can be the null are followed.
Inserting pid as null value
INSERT INTO session (pid, program_name) values (null, 'test1'); // pid: null
Don't inserting pid
INSERT INTO session (program_name) values ('test1'); // pid: null
Updating pid as null value
UPDATE session SET pid = null WHERE `thd_id` = 1; // pid: null

Related

Trying to update table in mysql 8 gives error

I am trying to update a table that I am working with.
Here is the "describe table;" output:
MySQL [mydb]> describe ost_staff;
+------------------------+-------------------------------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------------------+-------------------------------------------+------+-----+---------+----------------+
| staff_id | int unsigned | NO | PRI | NULL | auto_increment |
| dept_id | int unsigned | NO | MUL | 0 | |
| role_id | int unsigned | NO | | 0 | |
| username | varchar(32) | NO | UNI | | |
| firstname | varchar(32) | YES | | NULL | |
| lastname | varchar(32) | YES | | NULL | |
| passwd | varchar(128) | YES | | NULL | |
| backend | varchar(32) | YES | | NULL | |
| email | varchar(255) | YES | | NULL | |
| phone | varchar(24) | NO | | | |
| phone_ext | varchar(6) | YES | | NULL | |
| mobile | varchar(24) | NO | | | |
| signature | text | NO | | NULL | |
| lang | varchar(16) | YES | | NULL | |
| timezone | varchar(64) | YES | | NULL | |
| locale | varchar(16) | YES | | NULL | |
| notes | text | YES | | NULL | |
***| isactive | tinyint(1) | NO | MUL | 1 | |***
I am trying to update the field: isactive so I try the following command from one of the solutions that I found online:
UPDATE `ost_staff` SET `value`=0 WHERE `key`='isactive';
when I ran this command, I get the following error:
ERROR 1054 (42S22): Unknown column 'key' in 'where clause'
I am trying to change the value from 1 to zero in "Default" field.
The error message is self explanary...
There is no column key in your table.
The request should be something like
UPDATE `ost_staff` SET `isactive`=0 WHERE `staff_id`=XX;
or
UPDATE `ost_staff` SET `isactive`=0
to set 0 to all staff.

Store Apache access logs to MySQL

I referred this link to log access information into mysql database.
Below is my database structure for
apachelogs.web_access_log
+------------------+----------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------------+----------------------+------+-----+---------+-------+
| id | char(19) | YES | | NULL | |
| agent | varchar(255) | YES | | NULL | |
| bytes_sent | int(10) unsigned | YES | | NULL | |
| child_pid | smallint(5) unsigned | YES | | NULL | |
| cookie | varchar(255) | YES | | NULL | |
| machine_id | varchar(25) | YES | | NULL | |
| request_file | varchar(255) | YES | | NULL | |
| referer | varchar(255) | YES | | NULL | |
| remote_host | varchar(50) | YES | | NULL | |
| remote_logname | varchar(50) | YES | | NULL | |
| remote_user | varchar(50) | YES | | NULL | |
| request_duration | smallint(5) unsigned | YES | | NULL | |
| request_line | varchar(255) | YES | | NULL | |
| request_method | varchar(10) | YES | | NULL | |
| request_protocol | varchar(10) | YES | | NULL | |
| request_time | char(28) | YES | | NULL | |
| request_uri | varchar(255) | YES | | NULL | |
| request_args | varchar(255) | YES | | NULL | |
| server_port | smallint(5) unsigned | YES | | NULL | |
| ssl_cipher | varchar(25) | YES | | NULL | |
| ssl_keysize | smallint(5) unsigned | YES | | NULL | |
| ssl_maxkeysize | smallint(5) unsigned | YES | | NULL | |
| status | smallint(5) unsigned | YES | | NULL | |
| time_stamp | int(10) unsigned | YES | | NULL | |
| virtual_host | varchar(255) | YES | | NULL | |
+------------------+----------------------+------+-----+---------+-------+
I see most of the column values are NULL as below when the request is received from browser:
+------+-------+------------+-----------+--------+------------+--------------+---------+-------------+----------------+-------------+------------------+--------------+----------------+------------------+--------------+-------------+--------------+-------------+------------+-------------+----------------+--------+------------+--------------+
| id | agent | bytes_sent | child_pid | cookie | machine_id | request_file | referer | remote_host | remote_logname | remote_user | request_duration | request_line | request_method | request_protocol | request_time | request_uri | request_args | server_port | ssl_cipher | ssl_keysize | ssl_maxkeysize | status | time_stamp | virtual_host |
+------+-------+------------+-----------+--------+------------+--------------+---------+-------------+----------------+-------------+------------------+--------------+----------------+------------------+--------------+-------------+--------------+-------------+------------+-------------+----------------+--------+------------+--------------+
| - | - | 0 | NULL | NULL | NULL | NULL | - | ::1 | NULL | - | 0 | NULL | NULL | HTTP/1.0 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | 408 | 1531980584 | 192.168.4.18 |
+------+-------+------------+-----------+--------+------------+--------------+---------+-------------+----------------+-------------+------------------+--------------+----------------+------------------+--------------+-------------+--------------+-------------+------------+-------------+----------------+--------+------------+--------------+
1 row in set (0.02 sec)
Most of the request parameters are missing. Let me know what I have to modify in order to log all the attributes with their corresponding values.
Thanks
Apache2 is unable to write the logs to MySQL database properly due to the mismatch of 'id' column width. The default width created in the web_access_log table is 19 which should be increased to 25. If 25 did not help try again with bigger width.
After doing this, the MySQL log automatically started updating without any further issues.

FULLTEXT mysql return empty

I have a table with the following structure:
+-----------------+--------------+------+-----+-------------------+-----------------------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+--------------+------+-----+-------------------+-----------------------------+
| imovel_id | int(11) | NO | PRI | NULL | auto_increment |
| user_id | int(11) | NO | | NULL | |
| img_dest | varchar(111) | YES | | NULL | |
| data_imob | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
| finalidade_imob | varchar(51) | NO | MUL | NULL | |
| status_imob | varchar(51) | NO | | NULL | |
| tipo_imob | varchar(255) | YES | | NULL | |
| uf_imob | varchar(51) | NO | MUL | NULL | |
| cidade_imob | varchar(255) | NO | | NULL | |
| bairro_imob | varchar(255) | YES | | NULL | |
| rua_imob | varchar(255) | YES | | NULL | |
| vaga_imob | varchar(255) | YES | | NULL | |
| dorms_imob | char(2) | YES | | NULL | |
| tamanho_imob | varchar(222) | YES | | NULL | |
| valor_imob | varchar(255) | YES | | 0 | |
| titulo_imob | varchar(255) | YES | | NULL | |
| descricao_imob | longtext | YES | | NULL | |
| carac_imob | varchar(255) | YES | | NULL | |
+-----------------+--------------+------+-----+-------------------+-----------------------------+
If I run this query:
SELECT * FROM form_imovel LEFT JOIN (form_user) ON (form_user.user_id = form_imovel.user_id) WHERE MATCH(finalidade_imob,status_imob,tipo_imob,uf_imob,cidade_imob,bairro_imob,dorms_imob,valor_imob) AGAINST ('Comprar' IN BOOLEAN MODE);
Where Comprar is the value from finalidade_imob column, I have the right results, that work with all columns, except uf_imobcolumn.
The uf_imobcolumns have values like: SP,MG,RJ.
If I try run the following query, it returns empty:
SELECT * FROM form_imovel LEFT JOIN (form_user) ON (form_user.user_id = form_imovel.user_id) WHERE MATCH(finalidade_imob,status_imob,tipo_imob,uf_imob,cidade_imob,bairro_imob,dorms_imob,valor_imob) AGAINST ('SP' IN BOOLEAN MODE);
Even though that column has so many SP registers.
Fiddle example

MySQL command run on terminal is stuck, but phpmyadmin success

I want to get location from members IP. I can use the command on phpmyadmin, and it costs about 2 seconds. But it will be stuck when the command is executed on MySQL through terminal. How can I fix the problem.
SQL Command:
SELECT
ID,country
FROM
(SELECT ID,substring_index(members.server_ids,',',1) AS IP FROM members) AS A
JOIN
(SELECT country,start,end FROM ip) AS B
ON
INET_ATON(A.IP) BETWEEN INET_ATON(B.start) AND INET_ATON(B.end);
desc members:
+---------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------------+--------------+------+-----+---------+----------------+
| ID | int(11) | NO | PRI | NULL | auto_increment |
| USER_NAME | varchar(100) | NO | UNI | NULL | |
| PASSWORD | varchar(32) | YES | | NULL | |
| NAME | varchar(100) | YES | | NULL | |
| EMAIL | varchar(50) | YES | MUL | NULL | |
| SAFE_EMAIL | varchar(50) | YES | | NULL | |
| NICK_NAME | varchar(100) | YES | MUL | NULL | |
| GENDER | tinyint(1) | YES | | 0 | |
| BIRTHDAY | date | YES | | NULL | |
| CREATEDAY | datetime | YES | | NULL | |
| PHONE | varchar(20) | YES | | NULL | |
| MOBILE | varchar(15) | YES | | NULL | |
| ID_CARD | varchar(32) | YES | | NULL | |
| COUNTY | int(11) | YES | | NULL | |
| ADDRESS | varchar(255) | YES | | NULL | |
| LOGIN_TIME | datetime | YES | | NULL | |
| BUY_SN | varchar(50) | YES | | NULL | |
| BUY_KIND | varchar(50) | YES | | NULL | |
| PARTNER_SN | varchar(50) | YES | | NULL | |
| cookie_value | varchar(100) | YES | | NULL | |
| CONSIGNEE_INFO | int(11) | YES | MUL | NULL | |
| LOGIN_COUNT | int(11) | YES | | NULL | |
| PAY_COUNT | int(11) | YES | | NULL | |
| status | tinyint(4) | YES | | 1 | |
| experience | int(11) | YES | | 0 | |
| amount | int(11) | YES | | 0 | |
| score | int(11) | YES | | 0 | |
| is_facebook_account | tinyint(4) | YES | | 0 | |
| avatar | varchar(200) | YES | | NULL | |
| avatar_status | tinyint(1) | YES | | 0 | |
| member_authhash | varchar(50) | YES | | NULL | |
| job | varchar(50) | YES | | NULL | |
| server_ids | varchar(255) | YES | | NULL | |
+---------------------+--------------+------+-----+---------+----------------+
SELECT ID,server_ids FROM members LIMIT 10000,1;
+-------+--------------------------+
| ID | server_ids |
+-------+--------------------------+
| 20005 | 61.20.167.219, 127.0.0.1 |
+-------+--------------------------+
desc ip:
+---------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| country | char(2) | NO | | NULL | |
| type | char(4) | NO | | NULL | |
| start | varchar(15) | NO | MUL | NULL | |
| end | varchar(15) | NO | MUL | NULL | |
+---------+------------------+------+-----+---------+----------------+
SELECT * FROM ip LIMIT 100,1;
+-----+---------+------+-----------+---------------+
| id | country | type | start | end |
+-----+---------+------+-----------+---------------+
| 101 | IN | ipv4 | 1.187.0.0 | 1.187.255.255 |
+-----+---------+------+-----------+---------------+
phpMyAdmin automatically adds a LIMIT clause to the query. Because you do not have an ODER BY clause, MySQL sends the results as soon as it has found the number of records that you requested in the LIMIT clause.
Add a LIMIT clause to the query that you execute on the command line.

how to add a foreign key using an existing column in mysql

I have following table called transactions and I want to add foreign key using column account_id. But when I execute the query (see below) 0 rows affected. I am not getting what is wrong with the query.
I have two tables called transactions and accounts. Accounts table has an id as primary key and an account has_many transactions.
transactions table
+---------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| account_number | int(11) | YES | | NULL | |
| m_number | varchar(255) | YES | | NULL | |
| registration_number | varchar(255) | YES | | NULL | |
| page_number | varchar(255) | YES | | NULL | |
| entry_date | datetime | YES | | NULL | |
| narration | varchar(255) | YES | | NULL | |
| voucher_number | varchar(255) | YES | | NULL | |
| debit_credit | float | YES | | NULL | |
| profit | float | YES | | NULL | |
| account_code | int(11) | YES | | NULL | |
| balance | float | YES | | NULL | |
| branch | varchar(255) | YES | | NULL | |
| account_id | int(11) | YES | MUL | NULL | |
| created_at | datetime | YES | | NULL | |
| updated_at | datetime | YES | | NULL | |
+---------------------+--------------+------+-----+---------+----------------+
16 rows in set (0,01 sec)
mysql> ALTER TABLE transactions ADD FOREIGN KEY (account_id) REFERENCES accounts(id);
Query OK, 0 rows affected (0,03 sec)
Records: 0 Duplicates: 0 Warnings: 0
accounts table
+-----------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| account_number | int(11) | YES | | NULL | |
| account_code | int(11) | YES | | NULL | |
| branch | varchar(255) | YES | | NULL | |
| name | varchar(255) | YES | | NULL | |
| father_name | varchar(255) | YES | | NULL | |
| nic_number | varchar(255) | YES | | NULL | |
| mohalla | varchar(255) | YES | | NULL | |
| village | varchar(255) | YES | | NULL | |
| nominee | varchar(255) | YES | | NULL | |
| relationship | varchar(255) | YES | | NULL | |
| opening_balance | float | YES | | NULL | |
| opening_date | datetime | YES | | NULL | |
| created_at | datetime | YES | | NULL | |
| updated_at | datetime | YES | | NULL | |
+-----------------+--------------+------+-----+---------+----------------+
After running the query shouldn't the column say F_K or something like PRI ?
Any help would be great. Thanks!