这里介绍ubuntu发行版本上使用apt命令安装的步骤。不同发行版本安装方法有所不同。一、安装mysq安装前首先切换到管理员身份sudo su然后再执行命令安装apt install mysql-serverstustu-virtual-machine:~$ sudo su [sudo] stu 的密码 rootstu-virtual-machine:/home/stu# apt install mysql-server二、初始化配置初始化配置使用命令mysql_secure_installation1.设置数据库不进行密码强校验rootstu-virtual-machine:/home/stu# mysql_secure_installation Securing the MySQL server deployment. Connecting to MySQL using a blank password. VALIDATE PASSWORD COMPONENT can be used to test passwords and improve security. It checks the strength of password and allows the users to set only those passwords which are secure enough. Would you like to setup VALIDATE PASSWORD component? Press y|Y for Yes, any other key for No: N (输入N,不进行密码的强校验)2.设置root管理员密码注意是数据库的管理员的。密码不回显根据提示输入两遍Please set the password for root here. New password: (此处密码不回显) Re-enter new password: (此处密码不回显)3.设置是否要删除匿名用户这里不删除By default, a MySQL installation has an anonymous user, allowing anyone to log into MySQL 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? (Press y|Y for Yes, any other key for No) : N ... skipping.4.设置是否允许root用户远程登录这里设置允许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? (Press y|Y for Yes, any other key for No) : N ... skipping.5.是否删除test库这里选择不删除By default, MySQL 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? (Press y|Y for Yes, any other key for No) : N ... skipping.6.设置修改的权限立即生效此时所有配置初始化完成Reloading the privilege tables will ensure that all changes made so far will take effect immediately. Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y Success. All done! rootstu-virtual-machine:/home/stu#三、数据库服务启动停止1.检查服务器状态命令为service mysql status或者systemctl status mysql.servicerootstu-virtual-machine:/home/stu# service mysql status ● mysql.service - MySQL Community Server Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled) Active: active (running) since Mon 2021-06-21 13:36:08 CST; 24min ago Main PID: 4672 (mysqld) Status: Server is operational Tasks: 39 (limit: 4617) Memory: 335.5M CGroup: /system.slice/mysql.service └─4672 /usr/sbin/mysqld 6月 21 13:36:07 stu-virtual-machine systemd[1]: Starting MySQL Community Server... 6月 21 13:36:08 stu-virtual-machine systemd[1]: Started MySQL Community Server. rootstu-virtual-machine:/home/stu#2.重启|停止|启动数据库的命令如果不是管理员需要在命令前面加上sudo重启/etc/init.d/mysqlrestart停止/etc/init.d/mysqlstop启动/etc/init.d/mysqlstart或者执行如下命令rootstu-virtual-machine:/home/stu# service mysql restartrootstu-virtual-machine:/home/stu# service mysql stoprootstu-virtual-machine:/home/stu# service mysql start3.连接数据库命令mysql -uroot -prootstu-virtual-machine:/home/stu# mysql -uroot -p Enter password: (此处输入设置的密码如果没有密码直接回车) Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 8 Server version: 8.0.25-0ubuntu0.20.04.1 (Ubuntu) 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 quit (退出数据库客户端的命令)四、配置文件位置配置文件在如下位置需要修改可以用vi打开更改rootstu-virtual-machine:/home/stu# vi /etc/mysql/mysql.conf.d/mysqld.cnf例如需远程登录,则需要将bind-address改为0.0.0.0如下1 # Instead of skip-networking the default is now to listen only on 2 # localhost which is more compatible and is not less secure. 3 bind-address 127.0.0.1 改为0.0.0.0五、安装c/c开发库安装开发c/c的库命令apt install libmysqlclient-devstustu-virtual-machine:~$ sudo su [sudo] stu 的密码 rootstu-virtual-machine:/home/stu# apt install libmysqlclient-dev测试c语言连接数据库这里连接test库,没有自己创建注意‘111111’是数据库密码需要输入自己的。#include stdio.h #include mysql/mysql.h int main() { MYSQL connect;//mysql连接对象 mysql_init(connect); //连接到mysql if(mysql_real_connect(connect,localhost,root,111111,test,0,NULL,0)) { printf(连接mysql成功\n); } else { printf(err:%s\n,mysql_error(connect)); printf(连接mysql失败\n); } //关闭mysql连接 mysql_close(connect); return 0; }编译时需要指定-l指定mysqlclient这个库gcc-omysqltest mysqltest.c-lmysqlclientstustu-virtual-machine:~/lg$ gcc -o mysqltest mysqltest.c -lmysqlclient stustu-virtual-machine:~/lg$运行程序时要注意有些情况使系统用管理员身份运行可以成功rootstu-virtual-machine:/home/stu/lg# ./mysqltest 连接mysql成功 rootstu-virtual-machine:/home/stu/lg#但普通用户可能出现失败此时需要更改mysql数据库管理员密码并设置好加密方式。要注意一个是 linux系统管理员root还有一个是Mysql数据库的管理员名字也是root。不要混淆。stustu-virtual-machine:~/lg$ ./mysqltest err:Access denied for user rootlocalhost 连接mysql失败 stustu-virtual-machine:~/lg$六、用户管理与授权1.查看用户信息mysql select user,host,plugin from mysql.user;2.创建用户示例mysql create userstulocalhostidentified byIabc_123456;3.创建用户指定加密方式示例mysql create userstu1localhostidentified WITH mysql_native_password byIabc_123456;4.更新用户密码注意密码强度大小写数字ALTER userrootlocalhostIDENTIFIED WITH mysql_native_password BYIabc_123456;5.授权用户对那些数据库的那些表可以进行操作示例指定user_name用户可以从任意地点登录访问所有数据库的所有表GRANT ALL PRIVILEGES ON *.* TOuser_name%identified by密码GRANT ALL ON database_name.table_name TOuser_namelocalhost6.删除用户drop usernamelocalhost;
Ubuntu快速安装MySQL全攻略
这里介绍ubuntu发行版本上使用apt命令安装的步骤。不同发行版本安装方法有所不同。一、安装mysq安装前首先切换到管理员身份sudo su然后再执行命令安装apt install mysql-serverstustu-virtual-machine:~$ sudo su [sudo] stu 的密码 rootstu-virtual-machine:/home/stu# apt install mysql-server二、初始化配置初始化配置使用命令mysql_secure_installation1.设置数据库不进行密码强校验rootstu-virtual-machine:/home/stu# mysql_secure_installation Securing the MySQL server deployment. Connecting to MySQL using a blank password. VALIDATE PASSWORD COMPONENT can be used to test passwords and improve security. It checks the strength of password and allows the users to set only those passwords which are secure enough. Would you like to setup VALIDATE PASSWORD component? Press y|Y for Yes, any other key for No: N (输入N,不进行密码的强校验)2.设置root管理员密码注意是数据库的管理员的。密码不回显根据提示输入两遍Please set the password for root here. New password: (此处密码不回显) Re-enter new password: (此处密码不回显)3.设置是否要删除匿名用户这里不删除By default, a MySQL installation has an anonymous user, allowing anyone to log into MySQL 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? (Press y|Y for Yes, any other key for No) : N ... skipping.4.设置是否允许root用户远程登录这里设置允许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? (Press y|Y for Yes, any other key for No) : N ... skipping.5.是否删除test库这里选择不删除By default, MySQL 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? (Press y|Y for Yes, any other key for No) : N ... skipping.6.设置修改的权限立即生效此时所有配置初始化完成Reloading the privilege tables will ensure that all changes made so far will take effect immediately. Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y Success. All done! rootstu-virtual-machine:/home/stu#三、数据库服务启动停止1.检查服务器状态命令为service mysql status或者systemctl status mysql.servicerootstu-virtual-machine:/home/stu# service mysql status ● mysql.service - MySQL Community Server Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled) Active: active (running) since Mon 2021-06-21 13:36:08 CST; 24min ago Main PID: 4672 (mysqld) Status: Server is operational Tasks: 39 (limit: 4617) Memory: 335.5M CGroup: /system.slice/mysql.service └─4672 /usr/sbin/mysqld 6月 21 13:36:07 stu-virtual-machine systemd[1]: Starting MySQL Community Server... 6月 21 13:36:08 stu-virtual-machine systemd[1]: Started MySQL Community Server. rootstu-virtual-machine:/home/stu#2.重启|停止|启动数据库的命令如果不是管理员需要在命令前面加上sudo重启/etc/init.d/mysqlrestart停止/etc/init.d/mysqlstop启动/etc/init.d/mysqlstart或者执行如下命令rootstu-virtual-machine:/home/stu# service mysql restartrootstu-virtual-machine:/home/stu# service mysql stoprootstu-virtual-machine:/home/stu# service mysql start3.连接数据库命令mysql -uroot -prootstu-virtual-machine:/home/stu# mysql -uroot -p Enter password: (此处输入设置的密码如果没有密码直接回车) Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 8 Server version: 8.0.25-0ubuntu0.20.04.1 (Ubuntu) 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 quit (退出数据库客户端的命令)四、配置文件位置配置文件在如下位置需要修改可以用vi打开更改rootstu-virtual-machine:/home/stu# vi /etc/mysql/mysql.conf.d/mysqld.cnf例如需远程登录,则需要将bind-address改为0.0.0.0如下1 # Instead of skip-networking the default is now to listen only on 2 # localhost which is more compatible and is not less secure. 3 bind-address 127.0.0.1 改为0.0.0.0五、安装c/c开发库安装开发c/c的库命令apt install libmysqlclient-devstustu-virtual-machine:~$ sudo su [sudo] stu 的密码 rootstu-virtual-machine:/home/stu# apt install libmysqlclient-dev测试c语言连接数据库这里连接test库,没有自己创建注意‘111111’是数据库密码需要输入自己的。#include stdio.h #include mysql/mysql.h int main() { MYSQL connect;//mysql连接对象 mysql_init(connect); //连接到mysql if(mysql_real_connect(connect,localhost,root,111111,test,0,NULL,0)) { printf(连接mysql成功\n); } else { printf(err:%s\n,mysql_error(connect)); printf(连接mysql失败\n); } //关闭mysql连接 mysql_close(connect); return 0; }编译时需要指定-l指定mysqlclient这个库gcc-omysqltest mysqltest.c-lmysqlclientstustu-virtual-machine:~/lg$ gcc -o mysqltest mysqltest.c -lmysqlclient stustu-virtual-machine:~/lg$运行程序时要注意有些情况使系统用管理员身份运行可以成功rootstu-virtual-machine:/home/stu/lg# ./mysqltest 连接mysql成功 rootstu-virtual-machine:/home/stu/lg#但普通用户可能出现失败此时需要更改mysql数据库管理员密码并设置好加密方式。要注意一个是 linux系统管理员root还有一个是Mysql数据库的管理员名字也是root。不要混淆。stustu-virtual-machine:~/lg$ ./mysqltest err:Access denied for user rootlocalhost 连接mysql失败 stustu-virtual-machine:~/lg$六、用户管理与授权1.查看用户信息mysql select user,host,plugin from mysql.user;2.创建用户示例mysql create userstulocalhostidentified byIabc_123456;3.创建用户指定加密方式示例mysql create userstu1localhostidentified WITH mysql_native_password byIabc_123456;4.更新用户密码注意密码强度大小写数字ALTER userrootlocalhostIDENTIFIED WITH mysql_native_password BYIabc_123456;5.授权用户对那些数据库的那些表可以进行操作示例指定user_name用户可以从任意地点登录访问所有数据库的所有表GRANT ALL PRIVILEGES ON *.* TOuser_name%identified by密码GRANT ALL ON database_name.table_name TOuser_namelocalhost6.删除用户drop usernamelocalhost;