日度归档:2018-06-11

在Ubuntu 18.04上安装MySQL

使用 apt 或 tasksel 安装完 mysql-server 之后,默认无法登录,需要进行配置。

  1. 使用命令 mysql_secure_installation 并按提示操作,之后重启服务。
  2. 使用 sudo mysql -u root -p 登录,密码为空,或上一步设置的密码。
  3. 运行以下SQL: update mysql.user set plugin='mysql_native_password', authentication_string=password('root') where User='root'; ,退出,重启服务。

新密码是 root

mysql 8.0 无法登录的解决办法:

  1. 更改加密方式:
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'password' PASSWORD EXPIRE NEVER;
  1. 更改密码:
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
  1. 刷新:
mysql> FLUSH PRIVILEGES;

Laravel 5.4: Specified key was too long error

Laravel 5.4 made a change to the default database character set, and it’s now utf8mb4 which includes support for storing emojis. This only affects new applications and as long as you are running MySQL v5.7.7 and higher you do not need to do anything.

For those running MariaDB or older versions of MySQL you may hit this error when trying to run migrations:

[Illuminate\Database\QueryException]
SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes (SQL: alter table users add unique users_email_unique(email))

[PDOException]
SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes

As outlined in the Migrations guide to fix this all you have to do is edit your AppServiceProvider.php file and inside the boot method set a default string length:

use Illuminate\Support\Facades\Schema;

public function boot()
{
    Schema::defaultStringLength(191);
}

After that everything should work as normal. If you enjoyed this post be sure and join the weekly Laravel newsletter and never miss out on new releases, framework tips, and new tutorials.