MySql & MariaDB 安装
1. MySql 安装(二进制)
1.1 下载MySQL
https://cdn.mysql.com//Downloads/MySQL-5.6/mysql-5.6.39-linux-glibc2.12-x86_64.tar.gz
1.2 解压、移动目录位置
tar -zxf mysql-5.6.39-linux-glibc2.12-x86_64.tar.gz
mv mysql-5.6.39-linux-glibc2.12-x86_64 /mnt/installer/mysql
1.3 创建用户
useradd -s /sbin/nologin mysql
1.4 修改数据目录权限
cd /mnt/installer/mysql
chown -R mysql.data
1.5 初始化配置
./scripts/mysql_install_db --user=mysql --datadir=/mnt/installer/mysql/data
报错:
FATAL ERROR: please install the following Perl modules before executing ./scripts/mysql_install_db: Data::Dumper
解决:
yum -y install autoconf
1.6 拷贝配置文件和服务
cp support-files/my-default.cnf /etc/my.cnf
cp support-files/mysql.server /etc/init.d/mysqld
1.7 编辑服务文件
vim /etc/init.d/mysqld
#### 修改配置文件以下参数
basedir=/mnt/installer/mysql
datadir=/mnt/installer/mysql/data
1.8 添加开机启动/启动mysql
chkconfig --add mysqld
chkconfig mysqld on
service mysqld start
1.9 更改环境变量
#### 编辑环境变量文件
vim /etc/profile.d/path.sh
#### 添加MySQL路径
#!/bin/bash
export PATH=$PATH:/mnt/installer/mysql/bin
#### 生效环境变量
source /etc/profile.d/path.sh
2. MariaDB 安装 (YUM)
在CentOS 7.0安装MariaDB的数据库,在这里记录下安装过程,以便以后查看。
2.1 安装MariaDB
- 安装命令
yum -y install mariadb mariadb-server
- 启动MariaDB
systemctl start mariadb
- 设置开机启动
systemctl enable mariadb
2.2 初始化MariaDB
mysql_secure_installation
Enter current password for root (enter for none):<– //初次运行直接回车
……
Set root password? [Y/n] <– //是否设置root用户密码
New password: <– //设置root用户的密码
Re-enter new password: <– //再输入一次你设置的密码
……
Remove anonymous users? [Y/n] <– //是否删除匿名用户
Disallow root login remotely? [Y/n] <– //是否禁止root远程登录
Remove test database and access to it? [Y/n] <– //是否删除test数据库
Reload privilege tables now? [Y/n] <– //是否重新加载权限表
初始化MariaDB完成,接下来测试登录 进入数据库mysql -uroot -p "password"
2.3 配置MariaDB的字符集
- 修改配置文件
vi /etc/my.cnf
在[mysqld]标签下添加
init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET NAMES utf8'
collation-server=utf8_unicode_ci
skip-character-set-client-handshake
vi /etc/my.cnf.d/client.cnf
在[client]中添加
…………
default-character-set=utf8`
…………
vi /etc/my.cnf.d/mysql-clients.cnf
在[mysql]中添加
…………
default-character-set=utf8`
…………
重启mariadb :systemctl restart mariadb
2.4 查看MariaDB字符集
mysql> show variables like "%character%";show variables like "%collation%";
显示为
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)
+----------------------+-----------------+
| Variable_name | Value |
+----------------------+-----------------+
| collation_connection | utf8_unicode_ci |
| collation_database | utf8_unicode_ci |
| collation_server | utf8_unicode_ci |
+----------------------+-----------------+
3 rows in set (0.00 sec)
字符集配置完成。
2.5 添加用户,设置权限
- 创建用户命令
mysql>create user username@localhost identified by 'password';
- 直接创建用户并授权的命令
mysql>grant all on *.* to username@localhost indentified by 'password';
- 授予外网登陆权限
mysql>grant all privileges on *.* to username@'%' identified by 'password';
- 授予权限并且可以授权
mysql>grant all privileges on *.* to username@'hostname' identified by 'password' with grant option;
简单的用户和权限配置基本就这样了,其中只授予部分权限把 其中 all privileges或者all改为select,insert,update,delete,create,drop,index,alter,grant,references,reload,shutdown,process,file其中一部分。