MySQL 百度百科介绍:https://baike.baidu.com/item/mySQL/471251?fr=aladdin
从官方下载二进制包:
[root@centos ~]#wget -O /usr/local/src/mariadb-10.2.14-linux-glibc_214-x86_64.tar.gz http://mariadb.melbourneitmirror.net//mariadb-10.2.14/bintar-linux-glibc_214-x86_64/mariadb-10.2.14-linux-glibc_214-x86_64.tar.gz
解压:
[root@centos ~]# tar -zxf /usr/local/src/mariadb-10.2.14-linux-glibc_214-x86_64.tar.gz -C /usr/local/src/
移动并 重命名:
[root@centos ~]# mv /usr/local/src/mariadb-10.2.14-linux-glibc_214-x86_64 /usr/local/mysql
创建数据存储目录和日志存放目录:
[root@centos ~]# mkdir -p /data/mysql /var/log/mysql
添加运行MySQL 的系统用户mysql:
[root@centos ~]# useradd -s /bin/bash mysql
将mysql 目录和数据存放目录所属主和组都修改为 mysql:
[root@centos ~]# chown -R mysql.mysql /usr/local/mysql /data/mysql /var/log/mysql
编辑 /etc/my.cnf,简单的配置如下:
[mysqld]
datadir=/data/mysql
socket=/tmp/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/mysql/mariadb.log
pid-file=/data/mysql/mariadb.pid
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
创建mysql 启动脚本,如下:
cat << eof > /usr/lib/systemd/system/mysql.service
[Unit]
Description=Mariadb Server
After=network.target
After=syslog.target
[Service]
User=mysql
PIDFile=/data/mysql/mariadb.pid
ExecStart=/usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --user=mysql
ExecReload=/bin/kill -s HUP \$MAINPID
ExecStop=/bin/kill -s QUIT \$MAINPID
TimeoutSec=600
Restart=always
PrivateTmp=false
[Install]
WantedBy=multi-user.target
eof
切换到mysql 用户下:
[root@centos ~]# sudo su - mysql
进入mysql 目录:
[mysql@centos ~]$ cd /usr/local/mysql/
执行下面的语句安装mysql;
[mysql@centos mysql]$ ./scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/data/mysql --skip-name-resolve --user=mysql
Installing MariaDB/MySQL system tables in '/data/mysql' ...
OK
To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system
PLEASE REMEMBER TO SET A PASSWORD FOR THE MariaDB root USER !
To do so, start the server, then issue the following commands:
'/usr/local/mysql/bin/mysqladmin' -u root password 'new-password'
'/usr/local/mysql/bin/mysqladmin' -u root -h 217.160.0.230 password 'new-password'
Alternatively you can run:
'/usr/local/mysql/bin/mysql_secure_installation'
which will also give you the option of removing the test
databases and anonymous user created by default. This is
strongly recommended for production servers.
See the MariaDB Knowledgebase at http://mariadb.com/kb or the
MySQL manual for more instructions.
You can start the MariaDB daemon with:
cd '/usr/local/mysql' ; /usr/local/mysql/bin/mysqld_safe --datadir='/data/mysql'
You can test the MariaDB daemon with mysql-test-run.pl
cd '/usr/local/mysql/mysql-test' ; perl mysql-test-run.pl
Please report any problems at http://mariadb.org/jira
The latest information about MariaDB is available at http://mariadb.org/.
You can find additional information about the MySQL part at:
http://dev.mysql.com
Consider joining MariaDB's strong and vibrant community:
https://mariadb.org/get-involved/
切换到root 用户下,启动mysql:
[root@centos ~]# systemctl start mysql.service
没报错的 情况,查看监听的进程:
[root@centos ~]# ps aux|egrep mysql
mysql 5319 0.1 0.1 113264 1604 ? Ss 23:35 0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --user=mysql
mysql 5401 2.0 7.8 1257876 73672 ? Sl 23:35 0:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --log-error=/var/log/mysql/mariadb.log --pid-file=/data/mysql/mariadb.pid --socket=/data/mysql/mysql.sock
root 5434 0.0 0.1 112660 968 pts/3 R+ 23:35 0:00 grep -E --color=auto mysql
监听的 端口:
[root@centos ~]# ss -lntp|egrep mysql
LISTEN 0 80 :::3306 :::* users:(("mysqld",pid=5401,fd=20))
配置mysql 系统环境变量,方便后面便捷的使用MySQL 的各个命令。编辑 /etc/profile 文件,在文件的最后一行下面添加:
PATH=$PATH:/usr/local/mysql/bin
执行下面的语句刷新系统环境变量:
source /etc/profile
测试直接 使用mysql 客户端命令连接服务端:
[root@centos ~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 10.2.14-MariaDB MariaDB Server
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)]>
执行 mysql__secure__installation 进行安全配置:
[root@centos ~]# mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none): # 输入初始化密码,默认为空,直接回车
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.
Set root password? [Y/n] y # 询问是否要设置 root 密码,y 设置
New password: # 输入密码
Re-enter new password: # 再次输入
Password updated successfully!
Reloading privilege tables..
... Success!
By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB 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? [Y/n] y # 是否删除匿名用户,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? [Y/n] y # 是否关闭root 用户远程登录, y 是
... Success!
By default, MariaDB 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? [Y/n] y # 是否关闭测试数据库,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? [Y/n] y # 是否重载数据库, y 是
... Success!
Cleaning up...
All done! If you've completed all of the above steps, your MariaDB
installation should now be secure.
Thanks for using MariaDB!