1. APT安装
$ wget -c https://repo.mysql.com//mysql-apt-config_0.8.20-1_all.deb
$ sudo dpkg -i mysql-apt-config_0.8.20-1_all.deb
$ sudo apt update
$ sudo apt install -y mysql-server
2. 无法登录
但是安装完 MySQL8 后,会有 2 个问题:
- 即使安装过程中设置了 root 密码,也不能登录
- 使用
service mysql status
提示无法为别mysql
命令
解决方法如下:
$ wget https://raw.githubusercontent.com/mysql/mysql-server/8.0/support-files/mysql.server.sh
修改文件中 data_dir
参数,以防后续默认存储拉爆,这里貌似改了也不会起作用:
datadir=/home/yumingmin/data/mysql
终端下执行以下代码,就可以正常登录了。
$ sudo mv mysql.server.sh /etc/init.d/mysql
$ sudo chmod +x /etc/init.d/mysql
$ sudo service mysql start # stop
3. 修改存储地址
登录之后,查看一下存储地址,发现还是数据放在 /var/lib/mysql 文件夹下:
mysql > SHOW VARIABLES LIKE '%dir%';
+-----------------------------------------+--------------------------------+
| Variable_name | Value |
+-----------------------------------------+--------------------------------+
| basedir | /usr/ |
| binlog_direct_non_transactional_updates | OFF |
| character_sets_dir | /usr/share/mysql-8.0/charsets/ |
| datadir | /var/lib/mysql/ |
| innodb_data_home_dir | |
| innodb_directories | |
| innodb_doublewrite_dir | |
| innodb_log_group_home_dir | ./ |
| innodb_max_dirty_pages_pct | 90.000000 |
| innodb_max_dirty_pages_pct_lwm | 10.000000 |
| innodb_redo_log_archive_dirs | |
| innodb_temp_tablespaces_dir | ./#innodb_temp/ |
| innodb_tmpdir | |
| innodb_undo_directory | ./ |
| lc_messages_dir | /usr/share/mysql-8.0/ |
| plugin_dir | /usr/lib/mysql/plugin/ |
| replica_load_tmpdir | /tmp |
| slave_load_tmpdir | /tmp |
| tmpdir | /tmp |
+-----------------------------------------+--------------------------------+
修改 /etc/mysql/mysql.conf.d/mysqld.cnf 文件,修改 datadir
属性的值:
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
# datadir = /var/lib/mysql
datadir = /home/yumingmin/data/mysql
log-error = /var/log/mysql/error.log
但是修改之后提示找不到 mysql.pid,又再次无法启动了,需要将整个 /var/lib/mysql 目录移动到 /home/yumingmin/data 下。注意:一定要使用 mv
命令,不然还是有错误。
$ sudo mv /var/lib/mysql /home/yumingmin/data
4. 删除 MySQL
$ dpkg --list|grep mysql
$ sudo apt autoremove mysql-server
$ dpkg -l | grep ^rc|awk '{print $2}' | sudo xargs dpkg -P
$ sudo apt autoremove --purge mysql-apt-config
参考文档:https://www.cnblogs.com/panlq/p/13704965.html
报错:mysqld_safe Directory ‘/var/run/mysqld’ for UNIX socket file don’t exists
$ sudo mkdir -p /var/run/mysqld
$ sudo chown mysql:mysql /var/run/mysqld
$ sudo service mysql start