1, Check mariadb version
rpm -qa | grep -i mariadb
We don't have to uninstall mariadb before we install mysql;
2, Check glibc version
ldd --version
3, Uncompress and add a soft link
mkdir -p /usr/local/opt/mysql
xz -d mysql-8.0.23-linux-glibc2.12-x86_64.tar.xz
tar -xvf /usr/local/src/mysql/mysql-8.0.23-linux-glibc2.12-x86_64.tar -C /usr/local/opt/mysql/
ln -s /usr/local/opt/mysql/mysql-8.0.23-linux-glibc2.12-x86_64/ /usr/local/mysql
Add a new line to /etc/profile:
export PATH=/usr/local/mysql/bin:$PATH
Make the new environment variables effective:
source /etc/profile
4, Add a user "mysql" and a group "mysql"
[root@localhost mysql]# groupadd mysql
[root@localhost mysql]# useradd -r -g mysql mysql
[root@localhost mysql]# chown -R mysql:mysql /usr/local/mysql/
5, Initialize and generate passwords (in this case, root@localhost: /E.ZvW,-77YZ):
/usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
Output:
2021-01-26T08:22:11.589727Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release.
2021-01-26T08:22:11.589847Z 0 [System] [MY-013169] [Server] /usr/local/mysql/bin/mysqld (mysqld 8.0.23) initializing of server in progress as process 30395
2021-01-26T08:22:11.599860Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2021-01-26T08:22:13.867668Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2021-01-26T08:22:15.548075Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: hFcAZ4vp!cNS
A temporary password is generated for root@localhost: hFcAZ4vp!cNS
6, Create a temp file for user 'mysql':
[root@localhost mysql]# mkdir -p /var/log/mariadb
[root@localhost mysql]# touch /var/log/mariadb/mariadb.log
[root@localhost mysql]# chown -R mysql:mysql /var/log/mariadb
mkdir -p /var/run/mariadb
touch /var/run/mariadb/mariadb.pid
chown -R mysql:mysql /var/run/mariadb/
[root@localhost mysql]# mkdir /var/lib/mysql
[root@localhost mysql]# chmod 777 /var/lib/mysql/
7, Check /etc/my.conf
[root@localhost run]# cat /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
Comment the "datadir=/var/lib/mysql" line. (Or you can change it to /usr/local/mysql/data. The "datadir" directory must be empty when the mysql instance initializes. It also must be empty when mysqld_safe starts.)
The result is the following:
[mysqld]
#datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
8, Start mysql instance:
Add a soft link (mysql uses both /var/lib/mysql/mysql.sock and /tmp/mysql.sock for communication between the server and the clients):
ln -s /var/lib/mysql/mysql.sock /tmp/mysql.sock
Start the mysql instance using "mysqld_safe" command:
/usr/local/mysql/mysql/bin/mysqld_safe --user=mysql
[root@localhost ~]# ps -ef | grep mysql
root 31862 23439 0 03:36 pts/0 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --user=mysql
mysql 31982 31862 1 03:36 pts/0 00:00:01 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/var/log/mariadb/mariadb.log --pid-file=/var/run/mariadb/mariadb.pid --socket=/var/lib/mysql/mysql.sock
root 32161 32105 0 03:37 pts/1 00:00:00 grep --color=auto mysql
[root@localhost ~]# vi /etc/my.cnf
[root@localhost ~]# netstat -an | grep 3306
tcp6 0 0 :::33060 :::* LISTEN
tcp6 0 0 :::3306 :::* LISTEN
unix 2 [ ] DGRAM 33062
[root@localhost ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.23
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
9, Change the user root's password:
mysql> alter user 'root'@'localhost' identified by 'root';
Query OK, 0 rows affected (0.01 sec)
10, Allow remote computers to connect (the default is only to allow the localhost to connect):
mysql> update mysql.user set host='%' where user='root';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
11, Exit the mysql terminal:
mysql> exit
Bye
12, Check whether we can change shut down the mysql instance:
Use the following command to shut down the mysql:
[root@localhost ~]# /usr/local/mysql/bin/mysqladmin -uroot -p shutdown
Enter password:
12, Configure to make mysql auo-start when the operating system starts:
[root@localhost ~]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@localhost ~]# chmod +x /etc/rc.d/init.d/mysqld
[root@localhost ~]# chkconfig -add mysqld
-add: 未知的选项
[root@localhost ~]# chkconfig --add mysqld
[root@localhost ~]# chkconfig --list mysqld
注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。
要列出 systemd 服务,请执行 'systemctl list-unit-files'。
查看在具体 target 启用的服务请执行
'systemctl list-dependencies [target]'。
mysqld 0:关 1:关 2:开 3:开 4:开 5:开 6:关
if 3 and 4 and 5 are on, it's configured successfully.
Now you can use "service mysql start" and "service mysql stop" to start and stop the mysql service.
[root@localhost ~]# service mysql stop
Redirecting to /bin/systemctl stop mysql.service
[root@localhost ~]# ps -ef | grep mysql
root 33835 32105 0 04:04 pts/1 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --user=mysql
mysql 33955 33835 0 04:04 pts/1 00:00:02 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/var/log/mariadb/mariadb.log --pid-file=/var/run/mariadb/mariadb.pid --socket=/var/lib/mysql/mysql.sock
root 34674 32367 0 04:13 pts/2 00:00:00 grep --color=auto mysql
[root@localhost ~]# service mysql start