Linux updated folder and subfolder’s ownership

只要把這個folder的ownership 轉為 www-data 便可
我們可以使用 sudo chown user:usergroup folder/*

今日發現自從把wordpress 轉了用docker host 之後便不能 upload file
原因是我把之wordpress site 的檔案從 FTP upload 到新server 的 uploads folder
的ownership 是我FTP 的user名 而不是 run wordpress 的www-data

解決放法

sudo chown www-data:www-data uploads/*

Hope you find it useful

Bind Address Already In Use

Linux look for the process bound to a certain port

今日嘗試執行 Docker-Compose 去啟動Nginx Container 時出現以下的錯誤信息

Cannot start service nginx: driverfailed programming external connectivity on endpoint nginx 
 erland proxy: listen tcp 0.0.0.0:80: bind: address already in use
Cannot start service nginx: driverfailed programming external connectivity on endpoint nginx
erland proxy: listen tcp 0.0.0.0:80: bind: address already in use

原因是因為 port 80 已經在使用中..
怎樣知道那些process 正在使用 port 80呢?
我們可以使用以下command

sudo lsof -i -P -n | grep ":80 (LISTEN)"
linux command check which process use specific port
Linux command check which process use specific port

知道那些process 正在使用port :80之後 我們便可以 kill 了他們

Kill Linux Process
kill process

Hope you find it useful

C# Distinct List of Object

今日在Code Review 同事的Code 時候發現了一個有趣的寫法
他想做的是 Return 一個Distinct 左既 List Of Object

好直接既想法是使用 Distinct

List.Distinct();

之後發現Distinct 是不能 Distinct Object的
同事的寫法是用了 List 加 GroupBy 和Select First()

List.GrounBy(x=> x.key).Select(x => x.First())

之後發現.. 原來我們可以使用 GroupBy 加 First 來做到 Distinct Object的效果
以下是我的 Code Sample

void Main()
{
	var list = new List<Example>();
	list.Add(new Example() {Id=1, Name="Example 1", Comment="Comment 1" });
	list.Add(new Example() {Id=1, Name="Example 1", Comment="Comment 1" });
	list.Add(new Example() {Id=1, Name="ExamplE 1", Comment="Comment 2" });
	list.Add(new Example() {Id=2, Name="Example 2", Comment="Comment 3" });
	list.Add(new Example() { Id = 2, Name = "example 2", Comment = "Comment 3" });
	list.Add(new Example() { Id = 3, Name = "Example 3", Comment = "Comment 3" });
	list.Add(new Example() { Id = 4, Name = "Example 4", Comment = "Comment 3" });
	list.Add(new Example() { Id = 5, Name = "Example 5", Comment = "Comment 3" });

	Console.WriteLine("Use List.Distinct()");
	list.Distinct().Dump();

	Console.WriteLine("Use Group By Fields");
	list.GroupBy(x => new {x.Id, Name=x.Name}).Select(x =>x.First()).Dump();

	Console.WriteLine("Use Group By and make it ignore case");
	list.GroupBy(x => new {x.Id, Name=x.Name.ToLower()}).Select(x =>x.First()).Dump();
}

public class Example
{
	public int Id { get; set; }
	public string Name { get; set; }
	public string Comment {get;set;}
}

Output:

Hope you find it useful

EF Core with MySQL Specified key was too long; max key length is 3072 bytes



今日嘗試使用 EF Core with MySQL 時出現了以下的錯誤信息:

MySql.Data.MySqlClient.MySqlException
HResult=0x80004005
Message=Specified key was too long; max key length is 3072 bytes
Source=MySql.Data
StackTrace:
at MySql.Data.MySqlClient.MySqlStream.ReadPacket()
at MySql.Data.MySqlClient.NativeDriver.GetResult(Int32& affectedRow, Int64& insertedId)
at MySql.Data.MySqlClient.Driver.NextResult(Int32 statementId, Boolean force)
at MySql.Data.MySqlClient.MySqlDataReader.NextResult()
at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)
at MySql.Data.MySqlClient.MySqlCommand.ExecuteNonQuery()
at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.Execute(IRelationalConnection connection, DbCommandMethod executeMethod, IReadOnlyDictionary
2 parameterValues) at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.ExecuteNonQuery(IRelationalConnection connection, IReadOnlyDictionary2 parameterValues)
at Microsoft.EntityFrameworkCore.Migrations.MigrationCommand.ExecuteNonQuery(IRelationalConnection connection, IReadOnlyDictionary
2 parameterValues) at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationCommandExecutor.ExecuteNonQuery(IEnumerable1 migrationCommands, IRelationalConnection connection)
at Microsoft.EntityFrameworkCore.Storage.RelationalDatabaseCreator.CreateTables()
at Microsoft.EntityFrameworkCore.Storage.RelationalDatabaseCreator.EnsureCreated()
at Microsoft.EntityFrameworkCore.Infrastructure.DatabaseFacade.EnsureCreated()
at epm.Api.Startup.Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, ApplicationDbContext dbContext) in C:\Users\yauch\Documents\git\docker-easy-property\api\src\server\epm.Api\Startup.cs:line 65

我是使了了Official 的 NuGet PackageMySql.Data.EntityFrameworkCore  “
做了一會research 之後發現… 是這一個package 的問題
解決方法十分簡單.. 我們只需要用另一個 nuget package 便可”Pomelo.EntityFrameworkCore.MySql
https://www.nuget.org/packages/Pomelo.EntityFrameworkCore.MySql

Hope you find it useful



Git Check which branch contain specific commit

今日公司個Production project 有一個 Bug 但係在 Staging 和 UAT environment 都 replicate 唔到呢個問題..
之後便用Git Blame 去檢查一下有什麼 code change
再看看是那一個 commit 做了code change
找到commit 之後
便要嘗試去看看這個commit merge 了去那些 branch
解決方法

我們可以使用以下 git command

git branch --contains $COMMIT_ID

e.g.
git branch --contains 7df12c5

Hope you find it useful