ming's blog
MySQL安装记录

首先是卸载问题:如果安装 mysql 错误,请按如下标准卸载:

# 关闭mysql服务
sudo service stop mysql;

# 清除所有mysql包
sudo apt purge mysql-server mysql-client mysql-common mysql-server-core-* mysql-client-core-*

# 删除所有mysql文件
sudo rm -rf /etc/mysql /var/lib/mysql /var/log/mysql

# 最后清理所有不需要的包
sudo apt autoremove
sudo apt autoclean

找不到 /var/run/mysqld/mysqld.sock 报错

如果启动 mysql 时遇到 Error: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2),请仔细确认安装时是否有报错信息,根据报错原因去解决问题。

这类问题主要是my.cnf的配置指向错误,网上解决办法有很多种,请自行选择。

我自己遇到的是安装时提示 mysql-server depends on mysql-server-8.0,因为我是直接安装的 mysq-server,卸载重装时将安装包改为 mysql-server-8.0 就好了。

apt install -y mysql-server-8.0 mysql-common

初始化mysql

在使用 mysql 前,先使用 mysql_secure_installation 初始化数据库。

初始化数据库遇到 # Failed! Error: SET PASSWORD has no significance for user ‘root’@’localhost’ as the authentication method used doesn’t store authentication data in the MySQL server, 先退出进程,直接mysql进入数据库,运行 ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password by 'mynewpassword';’ 更改身份验证参数,然后重新初始化。(密码为 mynewpassword)

附上初始化过程:

root@6f72ddf70108:/# mysql_secure_installation

Securing the MySQL server deployment.

Enter password for user root: 
The 'validate_password' component is installed on the server.
The subsequent steps will run with the existing configuration
of the component.
Using existing password for root.

Estimated strength of the password: 50 
Change the password for root ? ((Press y|Y for Yes, any other key for No) : y

New password: 

Re-enter new password: 

Estimated strength of the password: 0 
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
 ... Failed! Error: Your password does not satisfy the current policy requirements

New password: 

Re-enter new password: 
Sorry, passwords do not match.

New password: 

Re-enter new password: 

Estimated strength of the password: 50 
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.


Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : n

 ... skipping.
By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.


Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
 - Dropping test database...
Success.

 - Removing privileges on test database...
Success.

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.

All done! 

mysql 启动失败:su: warning: cannot change directory to /nonexistent: No such file or directory

sudo service mysql stop
sudo usermod -d /var/lib/mysql/ mysql
sudo service mysql start