diff --git a/mysql-test/r/partition.result b/mysql-test/r/partition.result
index af90ac6c7149394c33a8ca94fdc45ef0faa7725a..090ee4b7734f59c15ea92c1eafb8f84400188c6f 100644
--- a/mysql-test/r/partition.result
+++ b/mysql-test/r/partition.result
@@ -1245,4 +1245,12 @@ INSERT INTO t1 SELECT a + 8, b FROM t1;
 ALTER TABLE t1 ADD PARTITION (PARTITION p1 VALUES LESS THAN (64));
 ALTER TABLE t1 DROP PARTITION p1;
 DROP TABLE t1;
+USE mysql;
+SET GLOBAL general_log = 0;
+ALTER TABLE general_log ENGINE = MyISAM;
+ALTER TABLE general_log PARTITION BY RANGE (TO_DAYS(event_time))
+(PARTITION p0 VALUES LESS THAN (733144), PARTITION p1 VALUES LESS THAN (3000000));
+ERROR HY000: Incorrect usage of PARTITION and log table
+ALTER TABLE general_log ENGINE = CSV;
+SET GLOBAL general_log = default;
 End of 5.1 tests
diff --git a/mysql-test/t/partition.test b/mysql-test/t/partition.test
index 186cf1d3e8aaf5bb7460c0b61c09bdd3008ad2be..358b0501e32839fbdf2a64fe807a4f7565c6e16a 100644
--- a/mysql-test/t/partition.test
+++ b/mysql-test/t/partition.test
@@ -1465,4 +1465,18 @@ ALTER TABLE t1 DROP PARTITION p1;
 
 DROP TABLE t1;
 
+#
+# Bug #27816: Log tables ran with partitions crashes the server when logging
+# is enabled.
+#
+
+USE mysql;
+SET GLOBAL general_log = 0;
+ALTER TABLE general_log ENGINE = MyISAM;
+--error ER_WRONG_USAGE
+ALTER TABLE general_log PARTITION BY RANGE (TO_DAYS(event_time))
+  (PARTITION p0 VALUES LESS THAN (733144), PARTITION p1 VALUES LESS THAN (3000000));
+ALTER TABLE general_log ENGINE = CSV;
+SET GLOBAL general_log = default;
+
 --echo End of 5.1 tests
diff --git a/sql/log.cc b/sql/log.cc
index 6ef1c1ea9120d1d3cd1e1b1aa63823a26b3be0ce..ac6ea92c61a43bd7e63b242200044ef1dfca51b6 100644
--- a/sql/log.cc
+++ b/sql/log.cc
@@ -296,6 +296,8 @@ bool Log_to_csv_event_handler::open_log_table(uint log_table_type)
   table->db= log_thd->db;
   table->db_length= log_thd->db_length;
 
+  lex_start(log_thd);
+  log_thd->clear_error();
   if (simple_open_n_lock_tables(log_thd, table) ||
       table->table->file->extra(HA_EXTRA_MARK_AS_LOG_TABLE) ||
       table->table->file->ha_rnd_init(0))
diff --git a/sql/sql_table.cc b/sql/sql_table.cc
index ca657a77e81aba08ce495358358d808e22046db7..bcc131e45bc6c71e9b96476cabba6c1617835e94 100644
--- a/sql/sql_table.cc
+++ b/sql/sql_table.cc
@@ -5747,21 +5747,31 @@ bool mysql_alter_table(THD *thd,char *new_db, char *new_name,
                                    table_list->table_name_length,
                                    table_list->table_name, 0);
 
-    /* Disable alter of enabled log tables */
-    if (table_kind && logger.is_log_table_enabled(table_kind))
+    if (table_kind)
     {
-      my_error(ER_BAD_LOG_STATEMENT, MYF(0), "ALTER");
-      DBUG_RETURN(TRUE);
-    }
+      /* Disable alter of enabled log tables */
+      if (logger.is_log_table_enabled(table_kind))
+      {
+        my_error(ER_BAD_LOG_STATEMENT, MYF(0), "ALTER");
+        DBUG_RETURN(TRUE);
+      }
 
-    /* Disable alter of log tables to unsupported engine */
-    if (table_kind &&
-        (create_info->used_fields & HA_CREATE_USED_ENGINE) &&
-        (!create_info->db_type || /* unknown engine */
-        !(create_info->db_type->flags & HTON_SUPPORT_LOG_TABLES)))
-    {
-      my_error(ER_UNSUPORTED_LOG_ENGINE, MYF(0));
-      DBUG_RETURN(TRUE);
+      /* Disable alter of log tables to unsupported engine */
+      if ((create_info->used_fields & HA_CREATE_USED_ENGINE) &&
+          (!create_info->db_type || /* unknown engine */
+           !(create_info->db_type->flags & HTON_SUPPORT_LOG_TABLES)))
+      {
+        my_error(ER_UNSUPORTED_LOG_ENGINE, MYF(0));
+        DBUG_RETURN(TRUE);
+      }
+
+#ifdef WITH_PARTITION_STORAGE_ENGINE
+      if (alter_info->flags & ALTER_PARTITION)
+      {
+	my_error(ER_WRONG_USAGE, MYF(0), "PARTITION", "log table");
+        DBUG_RETURN(TRUE);
+      }
+#endif
     }
   }