]# vi /etc/hosts 192.168.101.21 itsky21
# ping 192.168.101.21
[root@localhost ~]# rpm -qa |grep mysql
[root@localhost ~]# rpm -qa |grep mariadb
mariadb-libs-5.5.60-1.el7_5.x86_64
[root@localhost ~]# rpm -e --nodeps mariadb-libs-5.5.60-1.el7_5.x86_64
mkdir -p /mysql/app
mkdir -p /mysql/data/3306
mkdir -p /mysql/log/3306
[root@localhost mysql]# groupadd mysql
[root@localhost mysql]# useradd -r -g mysql -s /bin/false mysql
[root@localhost mysql]# chown -R mysql:mysql /mysql
[root@localhost mysql]# vi /etc/profile
[root@localhost mysql]# ls
bin COPYING docs include lib man README share support-files
[root@localhost mysql]# cd bin/
[root@localhost bin]# pwd
/mysql/app/mysql/bin
[root@localhost bin]# vi ~/.bash_profile
PATH=$PATH:/mysql/app/mysql/bin:$HOME/bin
exit 重新连接 which mysql
[root@localhost ~]# which mysql
/mysql/app/mysql/bin/mysql
[root@localhost ~]#
配置文件参数
vi /mysql/data/3306/my.cnf
[mysql]
default-character-set=utf8
socket=/mysql/data/3306/mysql.sock
[mysqld]
#skip-name-resolve
port= 3306
socket=/mysql/data/3306/mysql.sock
basedir=/mysql/app/mysql
datadir=/mysql/data/3306/data
character-set-server=utf8
default-storage-engine=INNODB
innodb_buffer_pool_size = 200M
max_allowed_packet=16M
explicit_defaults_for_timestamp=1
log-output=FILE
general_log=0
general_log_file=/mysql/log/3306/itsky21-general.err
slow_query_log=ON
slow_query_log_file=/mysql/log/3306/itsky21-query.err
long_query_time=10
log-error=/mysql/log/3306/itsky21-error.err
MySQL数据库初始化
ln -sf /mysql/data/3306/my.cnf /etc/my.cnf
[root@localhost ~]# ln -sf /mysql/data/3306/my.cnf /etc/my.cnf
[root@localhost ~]# more /etc/my.cnf
[mysql]
# yum install libaio
/mysql/app/mysql/bin/mysqld --initialize --user=mysql --basedir=/mysql/app/mysql/ --datadir=/mysql/data/3306/data/
初始化错误后
[root@localhost 3306]# rm -rf data
[root@localhost 3306]# pwd
/mysql/data/3306
初始化成功去查看初始化之后的文件和密码
[root@itsky21 data]# pwd 初始化成功生成文件
/mysql/data/3306/data
[root@itsky21 data]# ll
[root@itsky21 3306]# pwd 密码
/mysql/log/3306
[root@itsky21 3306]# ls
itsky21-error.err itsky21-query.err
[root@itsky21 3306]# cat itsky21-error.err )S.x3N1ty#gu
创建启动脚本
[root@itsky21 ~]# vi /usr/lib/systemd/system/mysqld.service
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
ExecStart=/mysql/app/mysql/bin/mysqld --defaults-file=/mysql/data/3306/my.cnf
LimitNOFILE= 65536
LimitNPROC=65536
脚本授权
# chmod u+x /usr/lib/systemd/system/mysqld.service
启动MySQL
# systemctl daemon-reload
systemctl status mysqld
systemctl start mysqld
systemctl status mysqld
创建开机启动和后台运行
nohup /mysql/app/mysql/bin/mysqld_safe --defaults-file=/mysql/data/3306/my.cnf & 后台运行
# mysqladmin -u root -p shutdown -S /mysql/data/3306/mysql.sock 手动停止数据库
登录后修改原始密码
mysql> alter user ‘root‘@‘localhost‘ identified by ‘rootroot‘;
Query OK, 0 rows affected (0.00 sec)
设置远程登录密码
mysql> use mysql;
mysql> create user ‘root‘@‘%‘ identified by ‘root‘;
mysql> select host,user from user where user=‘root‘;
+-----------+------+
| host | user |
+-----------+------+
| % | root |
| localhost | root |
+-----------+------+
2 rows in set (0.00 sec)
mysql> grant all privileges on *.* to ‘root‘@‘%‘ with grant option;
使用远程账号登录
[root@itsky21 ~]# mysql -u root -p -h192.168.101.21
mysql> create database itsky21 default charset utf8mb4;
mysql> grant all privileges on itsky21.* to ‘itsky21‘@‘%‘ identified by ‘itsky21‘;
mysql> grant all privileges on itsky21.* to ‘itsky21‘@‘localhost‘ identified by ‘itsky21‘;
mysql> select host,user from mysql.user;
create table itsky01(
-> id int auto_increment primary key,
-> name varchar(15)
-> )engine = InnoDB;
mysql> insert into itsky01 values(1,‘itsky01‘);
mysql> insert into itsky01 values(2,‘itsky02‘);
mysql> insert into itsky01 values(3,‘itsky03‘);
mysql> select * from itsky01;
卸载
systemctl stop mysqld rm -rf mysql