Tuesday, February 25, 2020

Important MySQL Commands


To login (from unix shell) use -h only if needed.
# [mysql dir]/bin/mysql -h hostname -u username -ppassword
To login (from windows)
mysql dir/bin/mysql.exe -h hostname -u username -ppassword

Create a database.
mysql> create database [databasename];

List all databases on the server.
mysql> show databases;

Switch to a database.
mysql> use [db name];

To see all the tables in the db.
mysql> show tables;

To see table's field formats.
mysql> describe [table name];

To delete a db.
mysql> drop database [database name];

To delete a table.
mysql> drop table [table name];

Show all data from a table.
mysql> SELECT * FROM [table name];

To return columns and column information.
mysql> show columns from [table name];

Show particular rows with the given value.
mysql> SELECT * FROM [table name] WHERE [field name] = "value";

Show all records containing the name "Something" AND the phone number '0123456789'.
mysql> SELECT * FROM [table name] WHERE name = "Something" AND phone_number = '0123456789';

Show all records not containing the name "Something" AND the phone number '0123456789' order by the phone_number field.
mysql> SELECT * FROM [table name] WHERE name != "Something" AND phone_number = '0123456789' order by phone_number;

Show all records starting with the letters 'Something' AND the phone number '0123456789'.
mysql> SELECT * FROM [table name] WHERE name like "Something%" AND phone_number = '0123456789';

Show all records starting with letters 'Something' AND the phone number '0123456789' limit to records 1 through 5.
mysql> SELECT * FROM [table name] WHERE name like "Something%" AND phone_number = '0123456789' limit 1,5;

Use a regular expression to find records. Use "REGEXP BINARY" to force case-sensitivity. This finds any record beginning with a.
mysql> SELECT * FROM [table name] WHERE rec RLIKE "^a";

Show unique records.
mysql> SELECT DISTINCT [column name] FROM [table name];

Show selected records sorted in an ascending (asc) or descending (desc).
mysql> SELECT [col1],[col2] FROM [table name] ORDER BY [col2] DESC;

Return number of rows.
mysql> SELECT COUNT(*) FROM [table name];

Sum column.
mysql> SELECT SUM(*) FROM [table name];

Creating a new user. Login as root. Switch to the MySQL db. Make the user. Update privs.
# mysql -u root -p
mysql> use mysql;
mysql> INSERT INTO user (Host,User,Password) VALUES('%','username',PASSWORD('password'));
mysql> flush privileges;

Change a users password from unix shell.
# [mysql dir]/bin/mysqladmin -u username -h hostname -ppassword 'new-password'

Change a users password from MySQL prompt. Login as root. Set the password. Update privileges.
# mysql -u root -p
mysql> SET PASSWORD FOR 'user'@'hostname' = PASSWORD('password');
mysql> flush privileges;

Recover a MySQL root password. Stop the MySQL server process. Start again with no grant tables. Login to MySQL as root. Set new password. Exit MySQL and restart MySQL server.
# /etc/init.d/mysql stop
# mysqld_safe --skip-grant-tables
# mysql -u root
mysql> use mysql;
mysql> update user set password=PASSWORD("newpassword") where User='root';
mysql> flush privileges;
mysql> quit
# /etc/init.d/mysql stop
# /etc/init.d/mysql start

Set a root password if there is no root password.
# mysqladmin -u root password newpassword

Update a root password.
# mysqladmin -u root -p oldpassword newpassword

Allow the user "Someone" to connect to the server from localhost using the password "passwd". Login as root. Switch to the MySQL db. Give privs. Update privs.
# mysql -u root -p
mysql> use mysql;
mysql> grant usage on *.* to Someone@localhost identified by 'passwd';
mysql> flush privileges;

Give user privilages for a db. Login as root. Switch to the MySQL db. Grant privs. Update privs.
# mysql -u root -p
mysql> use mysql;
mysql>INSERT INTO user(Host,Db,User,Select_priv,Insert_priv,Update_priv,Delete_priv,Create_priv,Drop_priv)
 VALUES ('%','databasename','username','Y','Y','Y','Y','Y','N');
mysql> flush privileges;

or

mysql> grant all privileges on databasename.* to username@localhost;
mysql> flush privileges;

To update info already in a table.
mysql> UPDATE [table name] SET Select_priv = 'Y',Insert_priv = 'Y',Update_priv = 'Y' where [field name] = 'user';

Delete a row(s) from a table.
mysql> DELETE from [table name] where [field name] = 'fieldvalue';

Update database permissions/privilages.
mysql> flush privileges;

Delete a column.
mysql> alter table [table name] drop column [column name];

Add a new column to db.
mysql> alter table [table name] add column [new column name] varchar (20);

Change column name.
mysql> alter table [table name] change [old column name] [new column name] varchar (50);

Make a unique column so you get no dupes.
mysql> alter table [table name] add unique ([column name]);

Make a column bigger.
mysql> alter table [table name] modify [column name] VARCHAR(3);

Delete unique from table.
mysql> alter table [table name] drop index [colmn name];

Load a CSV file into a table.
mysql> LOAD DATA INFILE '/tmp/filename.csv' replace INTO TABLE [table name] FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' (field1,field2,field3);

Dump all databases for backup. Backup file is sql commands to recreate all db's.
# mysqldump -u username -ppassword --opt > /tmp/alldatabases.sql

Dump one database for backup.
# mysqldump -u username -ppassword --databases databasename > /tmp/databasename.sql

Dump a table from a database.
# mysqldump -u username -ppassword databasename tablename > /tmp/databasename.tablename.sql

Restore database (or database table) from backup.
# mysql -u username -ppassword databasename < /tmp/databasename.sql

Create Table Example 1.
mysql> CREATE TABLE [table name] (name VARCHAR(20));

Create Table Example 2.
mysql> create table [table name] (personid int(50) not null auto_increment primary key,firstname varchar(35),middlename varchar(50),lastnamevarchar(50) default 'somethiing');

Saturday, February 22, 2020

C# .NET REST client


https://docs.microsoft.com/en-gb/dotnet/core/install/linux-package-manager-ubuntu-1910
https://docs.microsoft.com/en-gb/dotnet/core/install/linux-package-manager-centos7
https://docs.microsoft.com/en-gb/dotnet/core/install/linux-package-manager-rhel81

https://dotnet.microsoft.com/download
https://dotnet.microsoft.com/download/dotnet-core
https://dotnet.microsoft.com/platform/tools
https://code.visualstudio.com/Download


https://dotnet.microsoft.com/learn/dotnet/hello-world-tutorial/intro

https://dotnet.microsoft.com/learn/dotnet/hello-world-tutorial/create

wget -q https://packages.microsoft.com/config/ubuntu/19.10/packages-microsoft-prod.deb -O packages-microsoft-prod.deb sudo dpkg -i packages-microsoft-prod.deb


sudo apt-get update sudo apt-get install apt-transport-https sudo apt-get update sudo apt-get install dotnet-sdk-3.1
sudo apt-get install aspnetcore-runtime-3.1
sudo apt-get install dotnet-runtime-3.1

https://docs.microsoft.com/en-gb/dotnet/csharp/tutorials/intro-to-csharp/branches-and-loops-local


REST client

https://docs.microsoft.com/en-gb/dotnet/csharp/tutorials/console-webapiclient

This tutorial teaches you a number of features in .NET Core and the C# language. You’ll learn:
  • The basics of the .NET Core CLI.
  • An overview of C# Language features.
  • Managing dependencies with NuGet
  • HTTP Communications
  • Processing JSON information
  • Managing configuration with Attributes.


    cat /etc/lsb-release 
    wget -q https://packages.microsoft.com/config/ubuntu/19.10/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
   sudo dpkg -i packages-microsoft-prod.deb
   sudo apt-get update
   sudo apt-get install apt-transport-https
   sudo apt-get update
   sudo apt-get install dotnet-sdk-3.1
   sudo apt-get update
   sudo apt-get install apt-transport-https
   sudo apt-get update
   sudo apt-get install aspnetcore-runtime-3.1
   sudo apt-get install dotnet-runtime-3.1
   dotnet
   dotnet --info
   powershell --version
   dotnet --list-sdks
   dotnet --list-runtimes
   cd /home/learn/dotnet/HelloWorld
   dotnet new console -o myApp
   cd myApp
  
/home/learn/dotnet/HelloWorld/myApp# dotnet run
Hello World!
The current time is 2/22/2020 11:46:12 PM





Saturday, February 15, 2020

Don’t install Postgres, just PuLL iT



https://hackernoon.com/dont-install-postgres-docker-pull-postgres-bee20e200198
https://hackernoon.com/docker-commands-the-ultimate-cheat-sheet-994ac78e2888
https://www.enterprisedb.com/postgres-tutorials/how-install-postgres-docker
https://github.com/docker-library/postgres
https://www.jrebel.com/blog/docker-commands-cheat-sheethttps://www.jrebel.com/system/files/docker-commands-cheat-sheet.pdf




apt install docker.io

systemctl status docker
docker pull postgres:9.6.17-alpine

docker images
docker ps
docker run
docker exec
docker start
docker stop


root@breaker:/home/learn/docker# docker run --rm   --name pg-docker -e POSTGRES_PASSWORD=docker -d -p 5432:5432 -v $HOME/docker/volumes/postgres:/var/lib/postgresql/data  postgres

Unable to find image 'postgres:latest' locally
latest: Pulling from library/postgres
bc51dd8edc1b: Pull complete 
d2b355dbb6c6: Pull complete 
d237363a1a91: Pull complete 
ff4b9d2fde66: Pull complete 
646492d166e7: Pull complete 
50eeac6fd5fb: Pull complete 
502963de6da8: Pull complete 
d7263f7627b9: Pull complete 
d234d8f1a205: Pull complete 
1b2c24e5275c: Pull complete 
3f7c6dd9a7ae: Pull complete 
d6d6977a74b3: Pull complete 
acf1093f8b78: Pull complete 
77e1ac8c247a: Pull complete 
Digest: sha256:f8ffd650ecc802c8e45d9e72b7ba90829c3d89df3075a23b3f8e966728cfd97c
Status: Downloaded newer image for postgres:latest
aff1e95e8a9ad2ff6b0183785d3515114d28dbc83f17768123293d654cb69be1
root@breaker:/home/learn/docker# 


root@breaker:/home/learn/docker# docker ps

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
aff1e95e8a9a        postgres            "docker-entrypoint.s…"   15 minutes ago      Up 15 minutes       0.0.0.0:5432->5432/tcp   pg-docker

root@breaker:/home/learn/docker# 



root@breaker:/home/learn/docker# docker exec -it pg-docker bash

root@aff1e95e8a9a:/# 
root@aff1e95e8a9a:/# cat /etc/*rele*
PRETTY_NAME="Debian GNU/Linux 10 (buster)"
NAME="Debian GNU/Linux"
VERSION_ID="10"
VERSION="10 (buster)"
VERSION_CODENAME=buster
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"
root@aff1e95e8a9a:/# 
root@aff1e95e8a9a:/# 
root@aff1e95e8a9a:/# exit
exit
root@breaker:/home/learn/docker# 
root@breaker:/home/learn/docker# cat /etc/*rele*
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=16.04
DISTRIB_CODENAME=xenial
DISTRIB_DESCRIPTION="Ubuntu 16.04.5 LTS"
NAME="Ubuntu"
VERSION="16.04.5 LTS (Xenial Xerus)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 16.04.5 LTS"
VERSION_ID="16.04"
HOME_URL="http://www.ubuntu.com/"
SUPPORT_URL="http://help.ubuntu.com/"
BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"
VERSION_CODENAME=xenial
UBUNTU_CODENAME=xenial
root@breaker:/home/learn/docker# 


root@breaker:/home/learn/docker# psql -h localhost -U postgres -d postgres

Password for user postgres: 
psql (11.1 (Ubuntu 11.1-1.pgdg16.04+1), server 12.2 (Debian 12.2-1.pgdg100+1))
WARNING: psql major version 11, server major version 12.
         Some psql features might not work.
Type "help" for help.

postgres=# 




root@breaker:/home/learn/docker# docker run  --name pg-docker -e POSTGRES_PASSWORD=docker -d -p 5496:5432 -v $HOME/docker/postgres:/var/lib/postgresql/data  postgres:9.6.17-alpine

ea6aa193cdd19bf151601ee27464a08d6934de1704f84bfc3428fe0f7cf566ba
root@breaker:/home/learn/docker# 
root@breaker:/home/learn/docker# psql -h localhost -U postgres -p 5496 -d postgres
Password for user postgres: 
psql (11.1 (Ubuntu 11.1-1.pgdg16.04+1), server 9.6.17)
Type "help" for help.


postgres=# 

postgres=# select version();
                                         version                                         
-----------------------------------------------------------------------------------------
 PostgreSQL 9.6.17 on x86_64-pc-linux-musl, compiled by gcc (Alpine 9.2.0) 9.2.0, 64-bit
(1 row)

postgres=# 


root@breaker:/home/learn/docker# docker ps

CONTAINER ID        IMAGE                    COMMAND                  CREATED             STATUS              PORTS                    NAMES
ea6aa193cdd1        postgres:9.6.17-alpine   "docker-entrypoint.s…"   4 minutes ago       Up 4 minutes        0.0.0.0:5496->5432/tcp   pg-docker
root@breaker:/home/learn/docker# 


root@breaker:/home/learn/docker# docker ps
CONTAINER ID        IMAGE                    COMMAND                  CREATED             STATUS              PORTS                    NAMES
ea6aa193cdd1        postgres:9.6.17-alpine   "docker-entrypoint.s…"   11 minutes ago      Up 17 seconds       0.0.0.0:5496->5432/tcp   pg-docker
root@breaker:/home/learn/docker# 
root@breaker:/home/learn/docker# docker stop pg-docker
pg-docker
root@breaker:/home/learn/docker# 
root@breaker:/home/learn/docker# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
root@breaker:/home/learn/docker# 
root@breaker:/home/learn/docker# docker start pg-docker
pg-docker
root@breaker:/home/learn/docker# 
root@breaker:/home/learn/docker# docker ps
CONTAINER ID        IMAGE                    COMMAND                  CREATED             STATUS              PORTS                    NAMES
ea6aa193cdd1        postgres:9.6.17-alpine   "docker-entrypoint.s…"   11 minutes ago      Up 2 seconds        0.0.0.0:5496->5432/tcp   pg-docker

root@breaker:/home/learn/docker# 


root@breaker:/home/learn/docker# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
postgres            9.6.17-alpine       b1adbeb9895f        19 hours ago        38.2MB
postgres            latest              0d2531ee3abd        20 hours ago        397MB

root@breaker:/home/learn/docker#