Jun 25, 2021

Python3 write to file using Print

New version

with open(f'output.csv', 'w') as f:

      print(data, file=f)


Old version

with open(f'output.csv', 'w') as f:

      f.write(data)

Python3 fstrings

vars = 'abc'

print(f 'Variable  is : {vars}')

This is much better than 'Variable is : {}'.format(vars)

Jun 20, 2021

How to change the MySQL root account password on CentOS7?

 

1. Stop mysql:

systemctl stop mysqld


2. Set the mySQL environment option 

systemctl set-environment MYSQLD_OPTS="--skip-grant-tables"


3. Start mysql usig the options you just set

systemctl start mysqld


4. Login as root

mysql -u root


5. Update the root user password with these mysql commands

mysql> UPDATE mysql.user SET authentication_string = PASSWORD('MyNewPassword') WHERE User = 'root' AND Host = 'localhost';

mysql> FLUSH PRIVILEGES;


for 5.7.6 and later, you should use 

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass';

mysql> quit


6. Stop mysql

systemctl stop mysqld


7. Unset the mySQL envitroment option so it starts normally next time

systemctl unset-environment MYSQLD_OPTS


8. Start mysql normally:

systemctl start mysqld


Try to login using your new password:

7. mysql -u root -p



Jun 19, 2021

EC2 - Permission denied (publickey,gssapi-keyex,gssapi-with-mic).

  • EC2 login Issue
    • ssh -i /root/stage.pem root@ec2-54-<>.compute-1.amazonaws.com
  • ERROR
    • EC2 - Permission denied (publickey,gssapi-keyex,gssapi-with-mic).
  • Reason
    • There is a chance PEM file not added to ~/.ssh/authorized_keys 
Solution
  • Go to EC2 Dashbaord
  • Click on Connect (EC2 Instance Connect)



  • Open ~/.ssh/authorized_keys
  • Add the entry
    • ssh-rsa AAAAB3NzaC1yc2EAAAA<....>nJXw== Staging
  • Try to SSH again
  • Done

systemctl chkconfig - Enable/Disable service on boot


chkconfig --list httpd

chkconfig --list varnish

(or)

systemctl list-unit-files | grep httpd

systemctl list-unit-files | grep varnish



systemctl is-active httpd

systemctl is-active varnish


Enable on boot

  • chkconfig httpd on

Disable on boot

  • chkconfig httpd off

Jun 18, 2021

Find Unix Distribution Version

  • cat /proc/version
    • Linux version 4.14.219-164.354.amzn2.x86_64 (mockbuild@ip-10-0-1-103) (gcc version 7.3.1 20180712 (Red Hat 7.3.1-12) (GCC)) #1 SMP Mon Feb 22 21:18:39 UTC 2021 
  • rpm -E %{rhel}
    • 7

Jun 3, 2021

Python Array Vs List

 Python Array Vs List

ListArray
Heterigenous elements
E.g, [1, 2, [3, 4], 5, 6]
Homogenous elements
numbers = array.array('i', [1, 2, 3])
Explicitly define type of elements while defining (i - means integers
Use lot more spaceUse less spcace compared lists
List contains pointers to different objectsLike C language arrays, with a pointer pointing to first element & rest are allocated in continuous memory
More flexible keeping different structures of dataLess flexible
Less efficient in storing & manipulatingMore efficient in storing & manipulating
Used when your collection grow & shrink in time efficient manner & manage lot of data types in a listUsed when you perform lot of computationally intensive math operations
Numpy arrays are more suited for mathematical operations



Python Arrays

Arrays are sequence of homogeneous elements

import array

numbers = array.array('i', [1, 2, 3])

numbers.append(4)
print(numbers) # array('i', [1, 2, 3, 4])

# extend() appends iterable to the end of the array
numbers.extend([5, 6, 7])
print(numbers) # array('i', [1, 2, 3, 4, 5, 6, 7])