
CentOS 7中,已经使用MariaDB替代了MySQL数据库
一键安装 MariaDB(版本比较旧,不建议使用此方法安装)
# yum install -y mariadb-server #询问是否要安装,输入Y即可自动安装,直到安装完成 # systemctl start mariadb.service#启动MariaDB # systemctl stop mariadb.service#停止MariaDB # systemctl restart mariadb.service#重启MariaDB # systemctl enable mariadb.service#设置开机启动 # vi /etc/my.cnf #配置文件
添加最新版MariaDB源
到官网查看最新的包:http://downloads.mariadb.org/mariadb/repositories/
选择合适自己平台源,添加yum库: MariaDB10.4的版本库
# vi /etc/yum.repos.d/MariaDB.repo //文件内容如下: # MariaDB 10.4 CentOS repository list - created 2019-08-10 06:11 UTC # http://downloads.mariadb.org/mariadb/repositories/ [mariadb] name = MariaDB baseurl = http://yum.mariadb.org/10.4/centos7-amd64 gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB gpgcheck=1 :x 保存退出
安装或更新
# yum install MariaDB-server MariaDB-client # yum update MariaDB-server MariaDB-client
服务器相关的命令
# systemctl start mariadb.service //启动MariaDB # systemctl stop mariadb.service //停止MariaDB # systemctl restart mariadb.service //重启MariaDB # systemctl enable mariadb.service //设置开机启动
查看进程
# netstat -tupln
为root账户设置密码
# mysql_secure_installation 回车,根据提示输入Y 输入2次密码,回车 根据提示一路输入Y 本篇演示一下用命令mysql_secure_installation来安全初始化我们的MySql服务器。 01 #/usr/bin/mysql_secure_installation 02 NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL 03 SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY! 04 05 In order to log into MySQL to secure it, we'll need the current 06 password for the root user. If you've just installed MySQL, and 07 you haven't set the root password yet, the password will be blank, 08 so you should just press enter here. 09 10 Enter current password for root (enter for none):<---输入现在的root密码,因为我们还没设置,直接回车 11 OK, successfully used password, moving on… 12 13 Setting the root password ensures that nobody can log into the MySQL 14 root user without the proper authorisation. 15 16 Set root password? [Y/n] Y <---是否设定root密码,当然设置了,输入Y回车 17 New password: <---输入root密码,并回车,输入的过程中不会有任何显示 18 Re-enter new password: <---再次输入root密码,并回车,输入的过程中不会有任何显示 19 Password updated successfully! 20 Reloading privilege tables.. 21 … Success! 22 23 By default, a MySQL installation has an anonymous user, allowing anyone 24 to log into MySQL without having to have a user account created for 25 them. This is intended only for testing, and to make the installation 26 go a bit smoother. You should remove them before moving into a 27 production environment. 28 29 Remove anonymous users? [Y/n] Y <---是否删除匿名用户,删除,输入Y回车 30 … Success! 31 32 Normally, root should only be allowed to connect from 'localhost'. This 33 ensures that someone cannot guess at the root password from the network. 34 35 Disallow root login remotely? [Y/n] Y <---是否删禁止root用户远程登录,当然禁止,输入Y回车 36 … Success! 37 38 By default, MySQL comes with a database named 'test' that anyone can 39 access. This is also intended only for testing, and should be removed 40 before moving into a production environment. 41 42 Remove test database and access to it? [Y/n] <---是否删除测试数据库test,删除,输入Y回车 43 - Dropping test database… 44 … Success! 45 - Removing privileges on test database… 46 … Success! 47 48 Reloading the privilege tables will ensure that all changes made so far 49 will take effect immediately. 50 51 Reload privilege tables now? [Y/n] Y <---刷新权限,输入Y回车 52 … Success! 53 54 Cleaning up… 55 56 All done! If you've completed all of the above steps, your MySQL 57 installation should now be secure. 58 59 Thanks for using MySQL! 最后出现:Thanks for using MySQL/MariaDB! MySql密码设置完成,重新启动 MySQL: # systemctl restart mariadb.service
出现@Can’t connect to local MySQL server through socket 问题解决
@Can't connect to local MySQL server through socket 问题解决 ERROR 2002 (HY000): Can't connect to local MySQL server 重启服务器 查看 /var/lib/mysql/ 下是否有 mysql.sock 文件! 如果没有 执行以下命令: #chown -R mysql:mysql /var/lib/mysql 服务器重启: #service mariadb restart 确保 /etc/my.conf 下 的socket = /tmp/mysql.sock 位置配置正确
数据备份/导入/导出
//数据备份,导出指定的库 -f后面为指定库 > 后面为需要保存的位置 # mysqldump -uroot -p'xxx' -f xxx_db > /root/xxx.sql # mysqldump -uroot -p'xxx' -f xxx_db > /root/xxx.sql //可将命令写入crontab中以达到自动备份的效果 //导入数据,导入到指定的库 # mysql -uroot -p -f xxx_db < /root/2019-08_xxx.sql