Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.2k views
in Technique[技术] by (71.8m points)

mysql - Ubuntu Linux 18.04 WSL in Windows: MariaDB service start fails

After installing MariaDB repository configuration tool for the first time in my Linux WSL for Windows (as described in MariaDB Download Page), I executed mysql but there was a socket error. netstat -apn | grep mysql shows nothing, indicating the mysql service is stopped; sudo apt list | grep *mysql-server* shows I had successfully installed mysql-server.

However, as I tried sudo service mysql start, the command line gives:

 * Starting MariaDB database server mysqld                 [fail]

I tried the following methods, but all failed and yielded the same answer:

  • Using /etc/init.d/mysql start
  • Removing /var/lib/mysql/ib_logfile0 and /var/lib/mysql/ib_logfile1
  • Upgrading access of /var/lib/mysql using chmod -R 777 /var/lib/mysql
  • Removing everything from /var/lib/mysql/
  • Changing port setting using port=1112 in /etc/my.cnf (since I have another mysql on the Windows side)
  • Filling in additional information in /etc/my.cnf (my configuration file was initially empty after installation, and I filled in the basedir, datadir, socket, log_error, and pid-file properties)
  • Trying systemctl instead of service (this failed because Linux WSL uses sysvinit instead of systemd)

How could I start my MariaDB service? Thanks


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I'm able to reproduce your problem (or one that looks an awfully lot like it) on WSL1. Can you confirm that you are using WSL1?

I spun up two cloned instances (wsl --import of a clean backup) of Ubuntu 20.04 -- One on WSL1 and the other on WSL2. Unfortunately, I don't have a handy 18.04 to work with, but I'm hoping the problem is the same.

On WSL2, everything worked properly. After the installation steps (pretty much the ones you put in your comment, but for 20.04), I was able to:

sudo service mariadb start

and then sudo mysql -u root successfully.

On WSL1, however, the MariaDB installation seems to fail in a strange way. It does not create /etc/mysql/mariadb.cnf, which leads to what you saw with an empty /etc/mysql/my.cnf, since it's a symlink to mariadb.cnf.

So I created mariadb.cnf manually:

sudo vi /etc/mysql/mariadb.cnf

with the contents:

# The MariaDB configuration file
#
# The MariaDB/MySQL tools read configuration files in the following order:
# 0. "/etc/mysql/my.cnf" symlinks to this file, reason why all the rest is read.
# 1. "/etc/mysql/mariadb.cnf" (this file) to set global defaults,
# 2. "/etc/mysql/conf.d/*.cnf" to set global options.
# 3. "/etc/mysql/mariadb.conf.d/*.cnf" to set MariaDB-only options.
# 4. "~/.my.cnf" to set user-specific options.
#
# If the same option is defined multiple times, the last one will apply.
#
# One can use all long options that the program supports.
# Run program with --help to get a list of available options and with
# --print-defaults to see which it would actually understand and use.
#
# If you are new to MariaDB, check out https://mariadb.com/kb/en/basic-mariadb-articles/

#
# This group is read both by the client and the server
# use it for options that affect everything
#
[client-server]
# Port or socket location where to connect
# port = 3306
socket = /run/mysqld/mysqld.sock

# Import all .cnf files from configuration directory
!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mariadb.conf.d/

This is simply the default mariadb.cnf that was created correctly by the installation on WSL2.

Attempting to start the service then gave an error about a missing /etc/mysql/debian-start, so I repeated the same steps of copying it over:

sudo vi /etc/mysql/debian-start

With the contents:

#!/bin/bash
#
# This script is executed by "/etc/init.d/mariadb" on every (re)start.
#
# Changes to this file will be preserved when updating the Debian package.
#
# NOTE: This file is read only by the traditional SysV init script, not systemd.
#

source /usr/share/mysql/debian-start.inc.sh

# Read default/mysql first and then default/mariadb just like the init.d file does
if [ -f /etc/default/mysql ]; then
  . /etc/default/mysql
fi

if [ -f /etc/default/mariadb ]; then
  . /etc/default/mariadb
fi

MYSQL="/usr/bin/mysql --defaults-file=/etc/mysql/debian.cnf"
MYADMIN="/usr/bin/mysqladmin --defaults-file=/etc/mysql/debian.cnf"
# Don't run full mysql_upgrade on every server restart, use --version-check to do it only once
MYUPGRADE="/usr/bin/mysql_upgrade --defaults-extra-file=/etc/mysql/debian.cnf --version-check"
MYCHECK="/usr/bin/mysqlcheck --defaults-file=/etc/mysql/debian.cnf"
MYCHECK_SUBJECT="WARNING: mysqlcheck has found corrupt tables"
MYCHECK_PARAMS="--all-databases --fast --silent"
MYCHECK_RCPT="${MYCHECK_RCPT:-root}"

## Checking for corrupt, not cleanly closed (only for MyISAM and Aria engines) and upgrade needing tables.

# The following commands should be run when the server is up but in background
# where they do not block the server start and in one shell instance so that
# they run sequentially. They are supposed not to echo anything to stdout.
# If you want to disable the check for crashed tables comment
# "check_for_crashed_tables" out.
# (There may be no output to stdout inside the background process!)

# Need to ignore SIGHUP, as otherwise a SIGHUP can sometimes abort the upgrade
# process in the middle.
trap "" SIGHUP
(
  upgrade_system_tables_if_necessary;
  check_root_accounts;
  check_for_crashed_tables;
) >&2 &

exit 0

And then chmod 755 /etc/mysql/debian-start

After that, voila:

sudo service mariadb restart

sudo mysql -u root

Welcome to the MariaDB monitor.  Commands end with ; or g.
Your MariaDB connection id is 32
Server version: 10.5.8-MariaDB-1:10.5.8+maria~focal mariadb.org binary distribution

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.

MariaDB [(none)]>

Given the steps you've tried so far, I'd recommend blowing away pretty much all of it to try to start over "clean":

sudo apt remove mariadb-server
sudo apt autoremove
sudo rm -rf /etc/mysql
sudo rm -rf /var/lib/mysql
sudo rm -rf /usr/lib/mysql

Then reinstall mariadb-server and follow the steps above to create the correct files.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...